Skip to content

Commit

Permalink
chore: final code version - meetup SouJava Florianopolis
Browse files Browse the repository at this point in the history
Signed-off-by: Maximillian Arruda <[email protected]>
  • Loading branch information
dearrudam committed Nov 10, 2023
1 parent 703b02b commit b4a91d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,21 @@ public Set<String> getWinners(
public Set<String> getLosers(
@QueryParam("name") String name
) {
// TODO
throw new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE);
if (name != null)
return playoffs.findByLoserNameLike(name)
.stream()
.map(g -> g.loser().name())
.collect(Collectors.toSet());
return playoffs.listPlayoffsWithWinnerAndLoser()
.stream()
.map(g -> g.loser().name())
.collect(Collectors.toSet());
}

@GET
@Path("/ranking")
@WithSpan
public Ranking getRanking(){
return Ranking.winnerRanking(playoffs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package br.org.soujava.coffewithjava.jnopo;

import io.opentelemetry.instrumentation.annotations.WithSpan;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public record Ranking(Map<String, Integer> players) {
@WithSpan
public static Ranking winnerRanking(Playoffs playoffs) {
return of(playoffs, g -> g.winner().name());
}

private static Ranking of(Playoffs playoffs, Function<GameMatch, String> groupingFunction) {
var data =
playoffs.listPlayoffsWithWinnerAndLoser()
.stream()
.collect(
Collectors.groupingBy(
groupingFunction,
Collectors.collectingAndThen(Collectors.toList(), Collection::size))
).entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

return new Ranking(data);
}
}

0 comments on commit b4a91d9

Please sign in to comment.