Add credits AddingCredits.java
public static class ClientsCreditsInfo {private int offset = 0;
private Map<String, Integer> credits = new HashMap<>();
private TreeMap<Integer, Set<String>> inverseCredits = new TreeMap<>();
public boolean insert(String s, int c) {
if (!credits.containsKey(s)) {
credits.put(s, c - offset);
Set<String> set = inverseCredits.get(c - offset);
if (set == null) {
set = new HashSet<>();
inverseCredits.put(c - offset, set);
}
set.add(s);
return true;
}
return false;
}
public boolean remove(String s) {
Integer creditsIt = credits.get(s);
if (creditsIt != null) {
inverseCredits.get(creditsIt).remove(s);
if (inverseCredits.get(creditsIt).isEmpty()) {
inverseCredits.remove(creditsIt);
}
credits.remove(s);
return true;
}
return false;
}
public int lookup(String s) {
Integer it = credits.get(s);
return it == null ? -1 : it + offset;
}
public void addAll(int C) {
offset += C;
}
public String max() {
return inverseCredits.isEmpty() ? "" : inverseCredits.lastEntry()
.getValue().iterator().next();
}
}