|
| 1 | +package graphql.kickstart.tools.util; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Objects; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import static java.util.Collections.unmodifiableMap; |
| 9 | + |
| 10 | +@SuppressWarnings({"NullableProblems", "SuspiciousMethodCalls"}) |
| 11 | +public class BiMap<K, V> implements Map<K, V> { |
| 12 | + private final Map<K, V> delegate; |
| 13 | + private final Map<V, K> inverse; |
| 14 | + |
| 15 | + public static <K, V> BiMap<K, V> create() { |
| 16 | + return new BiMap<>(new HashMap<>(), new HashMap<>()); |
| 17 | + } |
| 18 | + |
| 19 | + public static <K, V> BiMap<K, V> unmodifiableBiMap(BiMap<K, V> biMap) { |
| 20 | + return new BiMap<>(unmodifiableMap(biMap.delegate), unmodifiableMap(biMap.inverse)); |
| 21 | + } |
| 22 | + |
| 23 | + private BiMap(Map<K, V> delegate, Map<V, K> inverse) { |
| 24 | + this.delegate = delegate; |
| 25 | + this.inverse = inverse; |
| 26 | + } |
| 27 | + |
| 28 | + public BiMap<V, K> inverse() { |
| 29 | + return new BiMap<>(inverse, delegate); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void clear() { |
| 34 | + delegate.clear(); |
| 35 | + inverse.clear(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean containsKey(Object key) { |
| 40 | + return delegate.containsKey(key); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean containsValue(Object value) { |
| 45 | + return inverse.containsKey(value); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Set<Entry<K, V>> entrySet() { |
| 50 | + return delegate.entrySet(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public V get(Object key) { |
| 55 | + return delegate.get(key); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean isEmpty() { |
| 60 | + return delegate.isEmpty(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Set<K> keySet() { |
| 65 | + return delegate.keySet(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritDoc} |
| 70 | + * |
| 71 | + * @throws IllegalArgumentException if the given value is already bound to a different key in this bimap. |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public V put(K key, V value) { |
| 75 | + // if the key already exists, and the value is the same as the old, then nothing to do |
| 76 | + boolean containedKey = containsKey(key); |
| 77 | + if (containedKey && Objects.equals(value, get(key))) { |
| 78 | + return value; |
| 79 | + } |
| 80 | + |
| 81 | + // if the value already exists, then it's not ok |
| 82 | + if (containsValue(value)) { |
| 83 | + throw new IllegalArgumentException("value already present: " + value); |
| 84 | + } |
| 85 | + |
| 86 | + // put the value; if the key already exists, it replaces an existing value, and we have to remove it from the inverse as well |
| 87 | + V oldValue = delegate.put(key, value); |
| 88 | + if (containedKey) { |
| 89 | + inverse.remove(oldValue); |
| 90 | + } |
| 91 | + inverse.put(value, key); |
| 92 | + |
| 93 | + return oldValue; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * {@inheritDoc} |
| 98 | + * |
| 99 | + * @throws IllegalArgumentException if an attempt to {@code put} any entry fails. |
| 100 | + */ |
| 101 | + @Override |
| 102 | + public void putAll(Map<? extends K, ? extends V> m) { |
| 103 | + for (Entry<? extends K, ? extends V> entry : m.entrySet()) { |
| 104 | + put(entry.getKey(), entry.getValue()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public V remove(Object key) { |
| 110 | + if (!containsKey(key)) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + inverse.remove(delegate.get(key)); |
| 115 | + return delegate.remove(key); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public int size() { |
| 120 | + return delegate.size(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Set<V> values() { |
| 125 | + return inverse.keySet(); |
| 126 | + } |
| 127 | +} |
0 commit comments