2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.collect.ImmutableMap;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.util.AbstractMap;
19 import java.util.AbstractSet;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.ConcurrentModificationException;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.NoSuchElementException;
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.eclipse.jdt.annotation.Nullable;
34 * A mutable version of {@link ImmutableOffsetMap}. It inherits the set of mappings from the immutable version and
35 * allows updating/removing existing mappings. New mappings are stored in a dedicated {@link LinkedHashMap} to preserve
36 * insertion order. It also tracks the need to duplicate the backing array, so the sequence of
38 * ImmutableOffsetMap<K, V> source;
39 * ImmutableOffsetMap<K, V> result = source.createMutableClone().immutableCopy();
41 * results in source and result sharing the backing objects.
43 * <p>This map does not support null keys nor values.
45 * @param <K> the type of keys maintained by this map
46 * @param <V> the type of mapped values
49 public abstract class MutableOffsetMap<K, V> extends AbstractMap<K, V> implements Cloneable, ModifiableMapPhase<K, V> {
50 static final class Ordered<K, V> extends MutableOffsetMap<K, V> {
52 super(new LinkedHashMap<>());
55 Ordered(final Map<K, V> source) {
56 super(OffsetMapCache.orderedOffsets(source.keySet()), source, new LinkedHashMap<>());
59 Ordered(final Map<K, Integer> offsets, final V[] objects) {
60 super(offsets, objects, new LinkedHashMap<>());
64 Object removedObject() {
69 UnmodifiableMapPhase<K, V> modifiedMap(final List<K> keys, final V[] values) {
70 return new ImmutableOffsetMap.Ordered<>(OffsetMapCache.orderedOffsets(keys), values);
74 UnmodifiableMapPhase<K, V> unmodifiedMap(final Map<K, Integer> offsetMap, final V[] values) {
75 return new ImmutableOffsetMap.Ordered<>(offsetMap, values);
79 SharedSingletonMap<K, V> singletonMap() {
80 return SharedSingletonMap.orderedCopyOf(this);
84 static final class Unordered<K, V> extends MutableOffsetMap<K, V> {
86 super(new HashMap<>());
89 Unordered(final Map<K, V> source) {
90 super(OffsetMapCache.unorderedOffsets(source.keySet()), source, new HashMap<>());
93 Unordered(final Map<K, Integer> offsets, final V[] objects) {
94 super(offsets, objects, new HashMap<>());
98 Object removedObject() {
103 UnmodifiableMapPhase<K, V> modifiedMap(final List<K> keys, final V[] values) {
104 final Map<K, Integer> offsets = OffsetMapCache.unorderedOffsets(keys);
105 return new ImmutableOffsetMap.Unordered<>(offsets, OffsetMapCache.adjustedArray(offsets, keys, values));
109 UnmodifiableMapPhase<K, V> unmodifiedMap(final Map<K, Integer> offsetMap, final V[] values) {
110 return new ImmutableOffsetMap.Unordered<>(offsetMap, values);
114 SharedSingletonMap<K, V> singletonMap() {
115 return SharedSingletonMap.unorderedCopyOf(this);
119 private static final Object[] EMPTY_ARRAY = new Object[0];
120 private static final Object REMOVED = new Object();
121 private final Map<K, Integer> offsets;
122 private HashMap<K, V> newKeys;
123 private Object[] objects;
124 private int removed = 0;
126 // Fail-fast iterator guard, see java.util.ArrayList for reference.
127 @SuppressFBWarnings("VO_VOLATILE_INCREMENT")
128 private transient volatile int modCount;
129 private boolean needClone = true;
131 MutableOffsetMap(final Map<K, Integer> offsets, final V[] objects, final HashMap<K, V> newKeys) {
132 verify(newKeys.isEmpty());
133 this.offsets = requireNonNull(offsets);
134 this.objects = requireNonNull(objects);
135 this.newKeys = requireNonNull(newKeys);
138 @SuppressWarnings("unchecked")
139 MutableOffsetMap(final HashMap<K, V> newKeys) {
140 this(ImmutableMap.of(), (V[]) EMPTY_ARRAY, newKeys);
143 @SuppressWarnings("unchecked")
144 MutableOffsetMap(final Map<K, Integer> offsets, final Map<K, V> source, final HashMap<K, V> newKeys) {
145 this(offsets, (V[]) new Object[offsets.size()], newKeys);
147 for (Entry<K, V> e : source.entrySet()) {
148 objects[offsets.get(e.getKey())] = requireNonNull(e.getValue());
151 this.needClone = false;
154 public static <K, V> MutableOffsetMap<K, V> orderedCopyOf(final Map<K, V> map) {
155 if (map instanceof Ordered) {
156 return ((Ordered<K, V>) map).clone();
158 if (map instanceof ImmutableOffsetMap) {
159 final ImmutableOffsetMap<K, V> om = (ImmutableOffsetMap<K, V>) map;
160 return new Ordered<>(om.offsets(), om.objects());
163 return new Ordered<>(map);
166 public static <K, V> MutableOffsetMap<K, V> unorderedCopyOf(final Map<K, V> map) {
167 if (map instanceof Unordered) {
168 return ((Unordered<K, V>) map).clone();
170 if (map instanceof ImmutableOffsetMap) {
171 final ImmutableOffsetMap<K, V> om = (ImmutableOffsetMap<K, V>) map;
172 return new Unordered<>(om.offsets(), om.objects());
175 return new Unordered<>(map);
178 public static <K, V> MutableOffsetMap<K, V> ordered() {
179 return new MutableOffsetMap.Ordered<>();
182 public static <K, V> MutableOffsetMap<K, V> unordered() {
183 return new MutableOffsetMap.Unordered<>();
186 abstract Object removedObject();
188 abstract UnmodifiableMapPhase<K, V> modifiedMap(List<K> keys, V[] values);
190 abstract UnmodifiableMapPhase<K, V> unmodifiedMap(Map<K, Integer> offsetMap, V[] values);
192 abstract SharedSingletonMap<K, V> singletonMap();
195 public final int size() {
196 return offsets.size() + newKeys.size() - removed;
200 public final boolean isEmpty() {
205 public final boolean containsKey(final Object key) {
206 final Integer offset = offsets.get(key);
207 if (offset != null) {
208 final Object obj = objects[offset];
209 if (!REMOVED.equals(obj)) {
214 return newKeys.containsKey(key);
218 public final V get(final Object key) {
219 final Integer offset = offsets.get(key);
220 if (offset != null) {
221 final Object obj = objects[offset];
224 * This is a bit tricky: Ordered will put REMOVED to removed objects to retain strict insertion order.
225 * Unordered will add null, indicating that the slot may be reused in future. Hence if we see a REMOVED
226 * marker, we need to fall back to checking with new keys.
228 if (!REMOVED.equals(obj)) {
229 @SuppressWarnings("unchecked")
230 final V ret = (V)obj;
235 return newKeys.get(key);
238 private void cloneArray() {
241 if (objects.length != 0) {
242 objects = objects.clone();
248 public final V put(final K key, final V value) {
249 requireNonNull(value);
250 final Integer offset = offsets.get(requireNonNull(key));
251 if (offset != null) {
252 final Object obj = objects[offset];
255 * Put which can potentially replace something in objects. Replacing an object does not cause iterators
256 * to be invalidated and does follow insertion order (since it is not a fresh insert). If the object has
257 * been removed, we fall back to newKeys.
259 if (!REMOVED.equals(obj)) {
260 @SuppressWarnings("unchecked")
261 final V ret = (V)obj;
264 objects[offset] = value;
274 final V ret = newKeys.put(key, value);
282 public final V remove(final Object key) {
283 final Integer offset = offsets.get(key);
284 if (offset != null) {
285 final Object obj = objects[offset];
288 * A previous remove() may have indicated that the objects slot cannot be reused. In that case we need
289 * to fall back to checking with newKeys.
291 if (!REMOVED.equals(obj)) {
294 @SuppressWarnings("unchecked")
295 final V ret = (V)obj;
296 objects[offset] = removedObject();
305 final V ret = newKeys.remove(key);
313 public final void clear() {
317 Arrays.fill(objects, removedObject());
318 removed = objects.length;
324 public final @NonNull Set<Entry<K, V>> entrySet() {
325 return new EntrySet();
329 public @NonNull Map<K, V> toUnmodifiableMap() {
330 if (removed == 0 && newKeys.isEmpty()) {
331 // Make sure next modification clones the array, as we leak it to the map we return.
334 // We have ended up with no removed objects, hence this cast is safe
335 @SuppressWarnings("unchecked")
336 final V[] values = (V[])objects;
339 * TODO: we could track the ImmutableOffsetMap from which this one was instantiated and if we do not
340 * perform any modifications, just return the original instance. The trade-off is increased complexity
341 * and an additional field in this class.
343 return unmodifiedMap(offsets, values);
346 final int s = size();
348 return ImmutableMap.of();
351 return singletonMap();
354 // Construct the set of keys
355 final List<K> keyset = new ArrayList<>(s);
357 if (removed != offsets.size()) {
358 for (Entry<K, Integer> e : offsets.entrySet()) {
359 final Object o = objects[e.getValue()];
360 if (o != null && !REMOVED.equals(o)) {
361 keyset.add(e.getKey());
366 keyset.addAll(offsets.keySet());
368 keyset.addAll(newKeys.keySet());
370 // Construct the values
371 @SuppressWarnings("unchecked")
372 final V[] values = (V[])new Object[keyset.size()];
375 if (removed != offsets.size()) {
376 for (Entry<K, Integer> e : offsets.entrySet()) {
377 final Object o = objects[e.getValue()];
378 if (o != null && !REMOVED.equals(o)) {
379 @SuppressWarnings("unchecked")
381 values[offset++] = v;
386 System.arraycopy(objects, 0, values, 0, offsets.size());
387 offset = offsets.size();
389 for (V v : newKeys.values()) {
390 values[offset++] = v;
393 return modifiedMap(keyset, values);
396 @SuppressWarnings("unchecked")
398 public MutableOffsetMap<K, V> clone() {
399 final MutableOffsetMap<K, V> ret;
402 ret = (MutableOffsetMap<K, V>) super.clone();
403 } catch (CloneNotSupportedException e) {
404 throw new IllegalStateException("Clone is expected to work", e);
407 ret.newKeys = (HashMap<K, V>) newKeys.clone();
408 ret.needClone = true;
413 public final int hashCode() {
416 for (Entry<K, Integer> e : offsets.entrySet()) {
417 final Object v = objects[e.getValue()];
419 result += e.getKey().hashCode() ^ v.hashCode();
423 return result + newKeys.hashCode();
427 public final boolean equals(final Object obj) {
431 if (!(obj instanceof Map)) {
435 if (obj instanceof ImmutableOffsetMap) {
436 final ImmutableOffsetMap<?, ?> om = (ImmutableOffsetMap<?, ?>) obj;
438 if (newKeys.isEmpty() && offsets.equals(om.offsets())) {
439 return Arrays.deepEquals(objects, om.objects());
441 } else if (obj instanceof MutableOffsetMap) {
442 final MutableOffsetMap<?, ?> om = (MutableOffsetMap<?, ?>) obj;
444 if (offsets.equals(om.offsets)) {
445 return Arrays.deepEquals(objects, om.objects) && newKeys.equals(om.newKeys);
449 // Fall back to brute map compare
450 final Map<?, ?> other = (Map<?, ?>)obj;
452 // Size and key sets have to match
453 if (size() != other.size() || !keySet().equals(other.keySet())) {
458 // Ensure all newKeys are present. Note newKeys is guaranteed to
459 // not contain null value.
460 for (Entry<K, V> e : newKeys.entrySet()) {
461 if (!e.getValue().equals(other.get(e.getKey()))) {
466 // Ensure all objects are present
467 for (Entry<K, Integer> e : offsets.entrySet()) {
468 final Object val = objects[e.getValue()];
469 if (val != null && !REMOVED.equals(val) && !val.equals(other.get(e.getKey()))) {
473 } catch (ClassCastException e) {
474 // Can be thrown by other.get() and indicate we have incompatible key types
482 public final @NonNull Set<K> keySet() {
487 final boolean needClone() {
492 final Object array() {
497 final Object newKeys() {
501 private final class EntrySet extends AbstractSet<Entry<K, V>> {
503 public @NonNull Iterator<Entry<K, V>> iterator() {
504 return new AbstractSetIterator<Entry<K, V>>() {
506 public Entry<K, V> next() {
507 final K key = nextKey();
508 return new SimpleEntry<>(key, get(key));
515 return MutableOffsetMap.this.size();
519 @SuppressWarnings("checkstyle:parameterName")
520 public boolean contains(final Object o) {
521 if (!(o instanceof Entry)) {
525 @SuppressWarnings("unchecked")
526 final Entry<K,V> e = (Entry<K,V>) o;
527 if (e.getValue() == null) {
531 return e.getValue().equals(MutableOffsetMap.this.get(e.getKey()));
535 @SuppressWarnings("checkstyle:parameterName")
536 public boolean add(final Entry<K, V> e) {
537 final V v = requireNonNull(e.getValue());
538 final V p = MutableOffsetMap.this.put(e.getKey(), v);
543 @SuppressWarnings("checkstyle:parameterName")
544 public boolean remove(final Object o) {
545 if (!(o instanceof Entry)) {
549 @SuppressWarnings("unchecked")
550 final Entry<K,V> e = (Entry<K,V>) o;
551 if (e.getValue() == null) {
555 final V v = MutableOffsetMap.this.get(e.getKey());
556 if (e.getValue().equals(v)) {
557 MutableOffsetMap.this.remove(e.getKey());
564 public void clear() {
565 MutableOffsetMap.this.clear();
569 private final class KeySet extends AbstractSet<K> {
571 public @NonNull Iterator<K> iterator() {
572 return new AbstractSetIterator<K>() {
582 return MutableOffsetMap.this.size();
586 private abstract class AbstractSetIterator<E> implements Iterator<E> {
587 private final Iterator<Entry<K, Integer>> oldIterator = offsets.entrySet().iterator();
588 private final Iterator<K> newIterator = newKeys.keySet().iterator();
589 private int expectedModCount = modCount;
590 private @Nullable K currentKey = null;
591 private @Nullable K nextKey;
593 AbstractSetIterator() {
597 private void updateNextKey() {
598 while (oldIterator.hasNext()) {
599 final Entry<K, Integer> e = oldIterator.next();
600 final Object obj = objects[e.getValue()];
601 if (obj != null && !REMOVED.equals(obj)) {
602 nextKey = e.getKey();
607 nextKey = newIterator.hasNext() ? newIterator.next() : null;
610 private void checkModCount() {
611 if (modCount != expectedModCount) {
612 throw new ConcurrentModificationException();
617 public final boolean hasNext() {
619 return nextKey != null;
623 public final void remove() {
625 checkState(currentKey != null);
626 final Integer offset = offsets.get(currentKey);
627 if (offset != null) {
629 objects[offset] = removedObject();
632 newIterator.remove();
635 expectedModCount = ++modCount;
639 protected final K nextKey() {
640 if (nextKey == null) {
641 throw new NoSuchElementException();
645 currentKey = nextKey;