From b4a91d9d05622993895cf3093fa1fe7131a453fe Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Fri, 10 Nov 2023 00:27:42 -0300 Subject: [PATCH] chore: final code version - meetup SouJava Florianopolis Signed-off-by: Maximillian Arruda --- .../coffewithjava/jnopo/PlayoffsResource.java | 17 ++++++++-- .../soujava/coffewithjava/jnopo/Ranking.java | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/Ranking.java diff --git a/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/PlayoffsResource.java b/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/PlayoffsResource.java index 5536b54..8cb5dab 100644 --- a/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/PlayoffsResource.java +++ b/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/PlayoffsResource.java @@ -50,8 +50,21 @@ public Set getWinners( public Set 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); + } } diff --git a/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/Ranking.java b/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/Ranking.java new file mode 100644 index 0000000..4b35560 --- /dev/null +++ b/jnopo-talk/src/main/java/br/org/soujava/coffewithjava/jnopo/Ranking.java @@ -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 players) { + @WithSpan + public static Ranking winnerRanking(Playoffs playoffs) { + return of(playoffs, g -> g.winner().name()); + } + + private static Ranking of(Playoffs playoffs, Function 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); + } +}