diff options
Diffstat (limited to 'guava/src/com/google/common/collect/AbstractBiMap.java')
-rw-r--r-- | guava/src/com/google/common/collect/AbstractBiMap.java | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/guava/src/com/google/common/collect/AbstractBiMap.java b/guava/src/com/google/common/collect/AbstractBiMap.java index 44ab8c7..2a94f88 100644 --- a/guava/src/com/google/common/collect/AbstractBiMap.java +++ b/guava/src/com/google/common/collect/AbstractBiMap.java @@ -49,7 +49,7 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> implements BiMap<K, V>, Serializable { private transient Map<K, V> delegate; - transient AbstractBiMap<V, K> inverse; + private transient AbstractBiMap<V, K> inverse; /** Package-private constructor for creating a map-backed bimap. */ AbstractBiMap(Map<K, V> forward, Map<V, K> backward) { @@ -67,20 +67,6 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> } /** - * Returns its input, or throws an exception if this is not a valid key. - */ - K checkKey(@Nullable K key) { - return key; - } - - /** - * Returns its input, or throws an exception if this is not a valid value. - */ - V checkValue(@Nullable V value) { - return value; - } - - /** * Specifies the delegate maps going in each direction. Called by the * constructor and by subclasses during deserialization. */ @@ -100,24 +86,22 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> // Query Operations (optimizations) - @Override public boolean containsValue(@Nullable Object value) { + @Override public boolean containsValue(Object value) { return inverse.containsKey(value); } // Modification Operations - @Override public V put(@Nullable K key, @Nullable V value) { + @Override public V put(K key, V value) { return putInBothMaps(key, value, false); } @Override - public V forcePut(@Nullable K key, @Nullable V value) { + public V forcePut(K key, V value) { return putInBothMaps(key, value, true); } private V putInBothMaps(@Nullable K key, @Nullable V value, boolean force) { - checkKey(key); - checkValue(value); boolean containedKey = containsKey(key); if (containedKey && Objects.equal(value, get(key))) { return value; @@ -140,7 +124,7 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> inverse.delegate.put(newValue, key); } - @Override public V remove(@Nullable Object key) { + @Override public V remove(Object key) { return containsKey(key) ? removeFromBothMaps(key) : null; } @@ -207,7 +191,27 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> } @Override public Iterator<K> iterator() { - return Maps.keyIterator(entrySet().iterator()); + final Iterator<Entry<K, V>> iterator = delegate.entrySet().iterator(); + return new Iterator<K>() { + Entry<K, V> entry; + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + @Override + public K next() { + entry = iterator.next(); + return entry.getKey(); + } + @Override + public void remove() { + checkState(entry != null); + V value = entry.getValue(); + iterator.remove(); + removeFromInverseMap(value); + } + }; } } @@ -230,7 +234,23 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> } @Override public Iterator<V> iterator() { - return Maps.valueIterator(entrySet().iterator()); + final Iterator<V> iterator = delegate.values().iterator(); + return new Iterator<V>() { + V valueToRemove; + + @Override public boolean hasNext() { + return iterator.hasNext(); + } + + @Override public V next() { + return valueToRemove = iterator.next(); + } + + @Override public void remove() { + iterator.remove(); + removeFromInverseMap(valueToRemove); + } + }; } @Override public Object[] toArray() { @@ -363,16 +383,6 @@ abstract class AbstractBiMap<K, V> extends ForwardingMap<K, V> * instances have inverse() methods that return the other. */ - @Override - K checkKey(K key) { - return inverse.checkValue(key); - } - - @Override - V checkValue(V value) { - return inverse.checkKey(value); - } - /** * @serialData the forward bimap */ |