diff --git a/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java b/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java index ea46ebb..02de454 100644 --- a/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java +++ b/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java @@ -173,34 +173,40 @@ return Collections.emptySet(); } - int start = position - 1; - int end = positions.size(); + int start = 0; + int end = positions.size() - 1; - int found; - while (true) { + int found = -1; + while (start <= end) { int middle = start + (end - start) / 2; ScoreData current = positions.get(middle); if (current.getPostition() == position) { found = middle; break; - } else if (start >= end - 1) { - plugin.getLogger().log(Level.SEVERE, "position not found but expected to be present: " + position); - plugin.getLogger().log(Level.SEVERE, positions.toString()); - return Collections.emptySet(); } else if (current.getPostition() > position) { - end = middle; + end = middle - 1; } else { - start = middle; + start = middle + 1; } } - start = end = found; - for (; start > 0 && positions.get(start - 1).getPostition() == position; start--) { - } - for (; end < positions.size() - 2 && positions.get(end + 1).getPostition() == position; end++) { + if (found == -1) { + plugin.getLogger().log(Level.SEVERE, "position not found but expected to be present: " + position); + plugin.getLogger().log(Level.SEVERE, positions.toString()); + return Collections.emptySet(); } - return positions.subList(start, end + 1).stream().map(ScoreData::getScorable).collect(Collectors.toSet()); + int startIndex = found; + int endIndex = found; + + while (startIndex > 0 && positions.get(startIndex - 1).getPostition() == position) { + startIndex--; + } + while (endIndex < positions.size() - 1 && positions.get(endIndex + 1).getPostition() == position) { + endIndex++; + } + + return positions.subList(startIndex, endIndex + 1).stream().map(ScoreData::getScorable).collect(Collectors.toSet()); } public ArrayList getByPositionRange(int start, int end) {