// no null keys or null values
sclass BijectiveMap {
new Map forward;
new Map backward;
void put(A a, B b) {
assertNotNull(a);
assertNotNull(b);
A oldKey = backward.get(b);
if (eq(oldKey, a))
ret; // nothing to do
if (oldKey != null)
forward.remove(oldKey); // remove old mapping
// add/overwrite new mapping
forward.put(a, b);
backward.put(b, a);
}
void remove(A a) {
B b = forward.get(a);
if (b == null)
ret; // nothing to do
forward.remove(a);
backward.remove(b);
}
B get(A a) {
ret forward.get(a);
}
A inverseGet(B b) {
ret backward.get(b);
}
Set keySet() { ret immutableSet(forward.keySet()); }
Set valueSet() { ret immutableSet(backward.keySet()); }
int size() { ret forward.size(); }
Map forwardMap() { ret immutableMap(forward); }
Map backwardMap() { ret immutableMap(backward); }
}