1 package de.juplo.kafka.wordcount.top10;
8 @AllArgsConstructor(access = AccessLevel.PRIVATE)
13 public final static int MAX_ENTRIES = 10;
16 private Entry[] entries = new Entry[0];
18 public Ranking add(Entry newEntry)
20 if (entries.length == 0)
22 entries = new Entry[1];
23 entries[0] = newEntry;
27 List<Entry> list = new LinkedList<>(Arrays.asList(entries));
29 for (int i = 0; i < list.size(); i++)
31 Entry entry = list.get(i);
33 if (entry.getCounter() < newEntry.getCounter())
37 if (list.get(oldPosition).getCounter() > newEntry.getCounter())
39 throw new IllegalArgumentException("The ranking already contains an entry with a higher counting for " + newEntry);
43 // Entry for word already exists with the same counting! Nothing changed...
48 list.add(i, newEntry);
49 for (int j = i+1; j < list.size(); j++)
52 if(entry.getWord().equals(newEntry.getWord()))
58 if (list.size() > MAX_ENTRIES)
60 list = list.subList(0, MAX_ENTRIES);
62 entries = list.toArray(num -> new Entry[num]);
66 if (entry.getWord().equals(newEntry.getWord()))
70 if (oldPosition > -1 && list.get(oldPosition).getCounter() > newEntry.getCounter())
72 throw new IllegalArgumentException("The ranking already contains an entry with a higher counting for " + newEntry);
75 if (list.size() < MAX_ENTRIES)
78 entries = list.toArray(num -> new Entry[num]);
84 public Ranking validate() throws IllegalArgumentException
86 if (this.entries.length > MAX_ENTRIES)
87 throw new IllegalArgumentException("Invalid Ranking: a valid ranking cannot have more entries than " + MAX_ENTRIES );
89 Set<String> seenWords = new HashSet<>();
90 long lowesCounting = Long.MAX_VALUE;
92 for (int i=0; i<this.entries.length; i++)
94 Entry entry = this.entries[i];
96 if (seenWords.contains(entry.getWord()))
97 throw new IllegalArgumentException("Invalid Ranking: Multiple occurrences of word -> " + entry.getWord());
98 if (entry.getCounter() > lowesCounting)
99 throw new IllegalArgumentException("Invalid Ranking: Entries are not sorted correctly");
101 seenWords.add(entry.getWord());
102 lowesCounting = entry.getCounter();
109 public boolean equals(Object o)
115 if (!(o instanceof Ranking))
118 Ranking other = (Ranking)o;
120 if (other.entries.length != entries.length)
123 if (entries.length == 0)
127 Set<String> myWordsWithCurrentCount = new HashSet<>();
128 Set<String> otherWordsWithCurrentCount = new HashSet<>();
129 Entry myEntry = entries[i];
130 long currentCount = myEntry.getCounter();
131 myWordsWithCurrentCount.add(myEntry.getWord());
134 Entry otherEntry = other.entries[i];
135 if (otherEntry.getCounter() != currentCount)
137 otherWordsWithCurrentCount.add(otherEntry.getWord());
138 if (++i >= entries.length)
139 return myWordsWithCurrentCount.equals(otherWordsWithCurrentCount);
140 myEntry = entries[i];
141 if (myEntry.getCounter() != currentCount)
143 if (!myWordsWithCurrentCount.equals(otherWordsWithCurrentCount))
145 currentCount = myEntry.getCounter();
146 myWordsWithCurrentCount.clear();
147 otherWordsWithCurrentCount.clear();
149 myWordsWithCurrentCount.add(myEntry.getWord());
153 public static Ranking of(Entry... entries)
155 Ranking ranking = new Ranking(entries);