/** This is more expensive than normal. */ @Override public void clear() { // iterate over all keys in originalMap and set them to null in deltaMap for (K key : originalMap.keySet()) { deltaMap.put(key, ErasureUtils.<Collection<V>>uncheckedCast(removedValue)); } }
@Override public void add(K key, V value) { Collection<V> deltaC = deltaMap.get(key); if (deltaC == null) { deltaC = cf.newCollection(); Collection<V> originalC = originalMap.get(key); if (originalC != null) { deltaC.addAll(originalC); } deltaMap.put(key, deltaC); } deltaC.add(value); }
@Override public Collection<V> get(Object key) { // key could be not in original or in deltaMap // key could be not in original but in deltaMap // key could be in original but removed from deltaMap // key could be in original but mapped to something else in deltaMap Collection<V> deltaResult = deltaMap.get(key); if (deltaResult == null) { return originalMap.get(key); } if (deltaResult == removedValue) { return cf.newEmptyCollection(); } return deltaResult; }
@Override public boolean containsKey(Object key) { // key could be not in original or in deltaMap // key could be not in original but in deltaMap // key could be in original but removed from deltaMap // key could be in original but mapped to something else in deltaMap Object value = deltaMap.get(key); if (value == null) { return originalMap.containsKey(key); } if (value == removedValue) { return false; } return true; }
@Override public void removeMapping(K key, V value) { Collection<V> deltaC = deltaMap.get(key); if (deltaC == null) { Collection<V> originalC = originalMap.get(key); if (originalC != null && originalC.contains(value)) { deltaC = cf.newCollection(); deltaC.addAll(originalC); deltaMap.put(key, deltaC); } } if (deltaC != null) { deltaC.remove(value); } }