sclass ExpiringMap2 extends AbstractMap {
Map> byKey;
new PriorityBlockingQueue> queue;
long standardExpiryTime; // ms
*() {}
*(long *standardExpiryTime) {}
void clean() {
Pair p;
while ((p = queue.peek()) != null && sysTime() >= p.a) {
p = queue.poll();
Pair v = byKey.get(p.a);
if (v != null && v.a == p.a) byKey.remove(p.a);
}
}
public B put(A a, B b) {
clean();
timeout = sysTime()+standardExpiryTime;
Pair p = byKey.get(a);
if (p != null) queue.remove(PairComparedByA(p.a, a));
byKey.put(a, pair(timeout, b));
queue.add(PairComparedByA(timeout, a));
}
public B remove(O a) {
clean();
Pair p = byKey.get(a);
if (p == null) null;
queue.remove(PairComparedByA(p.a, a));
byKey.remove(a);
ret p.b;
}
public B get(O a) {
clean();
ret pairB(byKey.get(a));
}
public Set> entrySet() {
clean();
ret mapValues(f pairB, byKey);
}
public int size() {
clean();
ret byKey.size();
}
}