+++ /dev/null
-package de.juplo.kafka.wordcount.top10;
-
-import lombok.Getter;
-import lombok.Setter;
-
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-
-@Getter
-@Setter
-public class Ranking
-{
- private Entry[] entries = new Entry[0];
-
- public void add(Entry newEntry)
- {
- if (entries.length == 0)
- {
- entries = new Entry[1];
- entries[0] = newEntry;
- return;
- }
-
- List<Entry> list = new LinkedList<>(Arrays.asList(entries));
- for (int i = 0; i < list.size(); i++)
- {
- Entry entry;
-
- entry = list.get(i);
- if (entry.getCount() <= newEntry.getCount())
- {
- list.add(i, newEntry);
- for (int j = i+1; j < list.size(); j++)
- {
- entry = list.get(j);
- if(entry.getWord().equals(newEntry.getWord()))
- {
- list.remove(j);
- break;
- }
- }
- if (list.size() > 10)
- {
- list = list.subList(0,10);
- }
- entries = list.toArray(num -> new Entry[num]);
- return;
- }
- }
- }
-}
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
+import java.util.Arrays;
import java.util.LinkedList;
+import java.util.List;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
.groupByKey(Grouped.keySerde(Serdes.String()))
.aggregate(
() -> Ranking.newBuilder().setEntries(new LinkedList<Entry>()).build(),
- (username, entry, ranking) -> {
- ranking.getEntries().add(entry);
+ (username, newEntry, ranking) -> {
+ List<Entry> entries = new LinkedList<>(ranking.getEntries());
+
+ if (entries.isEmpty())
+ {
+ entries.add(newEntry);
+ }
+ else
+ {
+ for (int i = 0; i < entries.size(); i++)
+ {
+ Entry entry = entries.get(i);
+ if (entry.getCount() <= newEntry.getCount())
+ {
+ entries.add(i, newEntry);
+ for (int j = i + 1; j < entries.size(); j++)
+ {
+ entry = entries.get(j);
+ if (entry.getWord().equals(newEntry.getWord()))
+ {
+ entries.remove(j);
+ break;
+ }
+ }
+ if (entries.size() > 10)
+ {
+ entries = entries.subList(0, 10);
+ }
+ }
+ }
+ }
+
+ ranking.setEntries(entries);
return ranking;
})
.toStream()