*/
package org.opendaylight.yangtools.util;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
import com.google.common.collect.ImmutableMap;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
/**
* A mutable version of {@link ImmutableOffsetMap}. It inherits the set of mappings from the immutable version and
* </code>
* results in source and result sharing the backing objects.
*
- * This map does not support null keys nor values.
+ * <p>This map does not support null keys nor values.
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
public abstract class MutableOffsetMap<K, V> extends AbstractMap<K, V> implements Cloneable, ModifiableMapPhase<K, V> {
static final class Ordered<K, V> extends MutableOffsetMap<K, V> {
Ordered() {
- super(new LinkedHashMap<>());
}
Ordered(final Map<K, V> source) {
- super(OffsetMapCache.orderedOffsets(source.keySet()), source, new LinkedHashMap<>());
+ super(OffsetMapCache.orderedOffsets(source.keySet()), source);
}
- Ordered(final Map<K, Integer> offsets, final V[] objects) {
- super(offsets, objects, new LinkedHashMap<>());
+ Ordered(final ImmutableMap<K, Integer> offsets, final V[] objects) {
+ super(offsets, objects);
}
@Override
}
@Override
- UnmodifiableMapPhase<K, V> modifiedMap(final List<K> keys, final V[] objects) {
- return new ImmutableOffsetMap.Ordered<>(OffsetMapCache.orderedOffsets(keys), objects);
+ UnmodifiableMapPhase<K, V> modifiedMap(final List<K> keys, final V[] values) {
+ return new ImmutableOffsetMap.Ordered<>(OffsetMapCache.orderedOffsets(keys), values);
}
@Override
- UnmodifiableMapPhase<K, V> unmodifiedMap(final Map<K, Integer> offsets, final V[] objects) {
- return new ImmutableOffsetMap.Ordered<>(offsets, objects);
+ UnmodifiableMapPhase<K, V> unmodifiedMap(final ImmutableMap<K, Integer> offsetMap, final V[] values) {
+ return new ImmutableOffsetMap.Ordered<>(offsetMap, values);
}
@Override
SharedSingletonMap<K, V> singletonMap() {
return SharedSingletonMap.orderedCopyOf(this);
}
+
+ @Override
+ HashMap<K, V> createNewKeys() {
+ return new LinkedHashMap<>();
+ }
}
static final class Unordered<K, V> extends MutableOffsetMap<K, V> {
Unordered() {
- super(new HashMap<>());
}
Unordered(final Map<K, V> source) {
- super(OffsetMapCache.unorderedOffsets(source.keySet()), source, new HashMap<>());
+ super(OffsetMapCache.unorderedOffsets(source.keySet()), source);
}
- Unordered(final Map<K, Integer> offsets, final V[] objects) {
- super(offsets, objects, new HashMap<>());
+ Unordered(final ImmutableMap<K, Integer> offsets, final V[] objects) {
+ super(offsets, objects);
}
@Override
}
@Override
- UnmodifiableMapPhase<K, V> modifiedMap(final List<K> keys, final V[] objects) {
- final Map<K, Integer> offsets = OffsetMapCache.unorderedOffsets(keys);
- return new ImmutableOffsetMap.Unordered<>(offsets, OffsetMapCache.adjustedArray(offsets, keys, objects));
+ UnmodifiableMapPhase<K, V> modifiedMap(final List<K> keys, final V[] values) {
+ final ImmutableMap<K, Integer> offsets = OffsetMapCache.unorderedOffsets(keys);
+ return new ImmutableOffsetMap.Unordered<>(offsets, OffsetMapCache.adjustedArray(offsets, keys, values));
}
@Override
- UnmodifiableMapPhase<K, V> unmodifiedMap(final Map<K, Integer> offsets, final V[] objects) {
- return new ImmutableOffsetMap.Unordered<>(offsets, objects);
+ UnmodifiableMapPhase<K, V> unmodifiedMap(final ImmutableMap<K, Integer> offsetMap, final V[] values) {
+ return new ImmutableOffsetMap.Unordered<>(offsetMap, values);
}
@Override
SharedSingletonMap<K, V> singletonMap() {
return SharedSingletonMap.unorderedCopyOf(this);
}
+
+ @Override
+ HashMap<K, V> createNewKeys() {
+ return new HashMap<>();
+ }
}
private static final Object[] EMPTY_ARRAY = new Object[0];
private static final Object REMOVED = new Object();
- private final Map<K, Integer> offsets;
+
+ private final ImmutableMap<K, Integer> offsets;
private HashMap<K, V> newKeys;
private Object[] objects;
private int removed = 0;
+
+ // Fail-fast iterator guard, see java.util.ArrayList for reference.
+ @SuppressFBWarnings("VO_VOLATILE_INCREMENT")
private transient volatile int modCount;
private boolean needClone = true;
- MutableOffsetMap(final Map<K, Integer> offsets, final V[] objects, final HashMap<K, V> newKeys) {
- Verify.verify(newKeys.isEmpty());
- this.offsets = Preconditions.checkNotNull(offsets);
- this.objects = Preconditions.checkNotNull(objects);
- this.newKeys = Preconditions.checkNotNull(newKeys);
+ MutableOffsetMap(final ImmutableMap<K, Integer> offsets, final Object[] objects) {
+ this.offsets = requireNonNull(offsets);
+ this.objects = requireNonNull(objects);
}
- @SuppressWarnings("unchecked")
- MutableOffsetMap(final HashMap<K, V> newKeys) {
- this(ImmutableMap.of(), (V[]) EMPTY_ARRAY, newKeys);
+ MutableOffsetMap() {
+ this(ImmutableMap.of(), EMPTY_ARRAY);
}
- @SuppressWarnings("unchecked")
- MutableOffsetMap(final Map<K, Integer> offsets, final Map<K, V> source, final HashMap<K, V> newKeys) {
- this(offsets, (V[]) new Object[offsets.size()], newKeys);
+ MutableOffsetMap(final ImmutableMap<K, Integer> offsets, final Map<K, V> source) {
+ this(offsets, new Object[offsets.size()]);
for (Entry<K, V> e : source.entrySet()) {
- objects[offsets.get(e.getKey())] = Preconditions.checkNotNull(e.getValue());
+ objects[offsets.get(e.getKey())] = requireNonNull(e.getValue());
}
this.needClone = false;
}
- public static <K, V> MutableOffsetMap<K, V> orderedCopyOf(final Map<K, V> m) {
- if (m instanceof Ordered) {
- return ((Ordered<K, V>) m).clone();
- }
- if (m instanceof ImmutableOffsetMap) {
- final ImmutableOffsetMap<K, V> om = (ImmutableOffsetMap<K, V>) m;
+ /**
+ * Create a {@link MutableOffsetMap} of the specified map, retaining its iteration order.
+ *
+ * @param map input map
+ * @return MutableOffsetMap with the same iteration order
+ * @throws NullPointerException if {@code map} is null
+ */
+ public static <K, V> @NonNull MutableOffsetMap<K, V> orderedCopyOf(final Map<K, V> map) {
+ if (map instanceof Ordered) {
+ return ((Ordered<K, V>) map).clone();
+ }
+ if (map instanceof ImmutableOffsetMap) {
+ final ImmutableOffsetMap<K, V> om = (ImmutableOffsetMap<K, V>) map;
return new Ordered<>(om.offsets(), om.objects());
}
- return new Ordered<>(m);
+ return new Ordered<>(map);
}
- public static <K, V> MutableOffsetMap<K, V> unorderedCopyOf(final Map<K, V> m) {
- if (m instanceof Unordered) {
- return ((Unordered<K, V>) m).clone();
- }
- if (m instanceof ImmutableOffsetMap) {
- final ImmutableOffsetMap<K, V> om = (ImmutableOffsetMap<K, V>) m;
+ /**
+ * Create a {@link MutableOffsetMap} of the specified map, potentially with a different iteration order.
+ *
+ * @param map input map
+ * @return MutableOffsetMap with undefined iteration order
+ * @throws NullPointerException if {@code map} is null
+ */
+ public static <K, V> @NonNull MutableOffsetMap<K, V> unorderedCopyOf(final Map<K, V> map) {
+ if (map instanceof Unordered) {
+ return ((Unordered<K, V>) map).clone();
+ }
+ if (map instanceof ImmutableOffsetMap) {
+ final ImmutableOffsetMap<K, V> om = (ImmutableOffsetMap<K, V>) map;
return new Unordered<>(om.offsets(), om.objects());
}
- return new Unordered<>(m);
+ return new Unordered<>(map);
}
- public static <K, V> MutableOffsetMap<K, V> ordered() {
+ /**
+ * Create an empty {@link MutableOffsetMap} which has an iteration order matching the insertion order.
+ *
+ * @return MutableOffsetMap which preserves insertion order
+ */
+ public static <K, V> @NonNull MutableOffsetMap<K, V> ordered() {
return new MutableOffsetMap.Ordered<>();
}
- public static <K, V> MutableOffsetMap<K, V> unordered() {
+ /**
+ * Create an empty {@link MutableOffsetMap} which has unspecified iteration order.
+ *
+ * @return An MutableOffsetMap
+ */
+ public static <K, V> @NonNull MutableOffsetMap<K, V> unordered() {
return new MutableOffsetMap.Unordered<>();
}
abstract Object removedObject();
- abstract UnmodifiableMapPhase<K, V> modifiedMap(List<K> keys, V[] objects);
- abstract UnmodifiableMapPhase<K, V> unmodifiedMap(Map<K, Integer> offsets, V[] objects);
+
+ abstract UnmodifiableMapPhase<K, V> modifiedMap(List<K> keys, V[] values);
+
+ abstract UnmodifiableMapPhase<K, V> unmodifiedMap(ImmutableMap<K, Integer> offsetMap, V[] values);
+
abstract SharedSingletonMap<K, V> singletonMap();
@Override
public final int size() {
- return offsets.size() + newKeys.size() - removed;
+ return offsets.size() - removed + (newKeys == null ? 0 : newKeys.size());
}
@Override
}
}
- return newKeys.containsKey(key);
+ return newKeys != null && newKeys.containsKey(key);
}
@Override
}
}
- return newKeys.get(key);
+ return newKeys == null ? null : newKeys.get(key);
}
private void cloneArray() {
if (needClone) {
needClone = false;
- if (!EMPTY_ARRAY.equals(objects)) {
+ if (objects.length != 0) {
objects = objects.clone();
}
}
@Override
public final V put(final K key, final V value) {
- Preconditions.checkNotNull(value);
- final Integer offset = offsets.get(Preconditions.checkNotNull(key));
+ requireNonNull(value);
+ final Integer offset = offsets.get(requireNonNull(key));
if (offset != null) {
final Object obj = objects[offset];
}
}
+ if (newKeys == null) {
+ newKeys = createNewKeys();
+ }
final V ret = newKeys.put(key, value);
if (ret == null) {
modCount++;
}
}
+ if (newKeys == null) {
+ return null;
+ }
final V ret = newKeys.remove(key);
if (ret != null) {
modCount++;
@Override
public final void clear() {
if (size() != 0) {
- newKeys.clear();
+ if (newKeys != null) {
+ newKeys.clear();
+ }
cloneArray();
Arrays.fill(objects, removedObject());
removed = objects.length;
}
@Override
- public final Set<Entry<K, V>> entrySet() {
+ public final @NonNull Set<Entry<K, V>> entrySet() {
return new EntrySet();
}
@Override
- public Map<K, V> toUnmodifiableMap() {
- if (removed == 0 && newKeys.isEmpty()) {
+ public @NonNull Map<K, V> toUnmodifiableMap() {
+ if (removed == 0 && noNewKeys()) {
// Make sure next modification clones the array, as we leak it to the map we return.
needClone = true;
} else {
keyset.addAll(offsets.keySet());
}
- keyset.addAll(newKeys.keySet());
+ if (newKeys != null) {
+ keyset.addAll(newKeys.keySet());
+ }
// Construct the values
@SuppressWarnings("unchecked")
final V[] values = (V[])new Object[keyset.size()];
- int i = 0;
+ int offset = 0;
if (removed != 0) {
if (removed != offsets.size()) {
for (Entry<K, Integer> e : offsets.entrySet()) {
if (o != null && !REMOVED.equals(o)) {
@SuppressWarnings("unchecked")
final V v = (V) o;
- values[i++] = v;
+ values[offset++] = v;
}
}
}
} else {
System.arraycopy(objects, 0, values, 0, offsets.size());
- i = offsets.size();
+ offset = offsets.size();
}
- for (V v : newKeys.values()) {
- values[i++] = v;
+ if (newKeys != null) {
+ for (V v : newKeys.values()) {
+ values[offset++] = v;
+ }
}
return modifiedMap(keyset, values);
try {
ret = (MutableOffsetMap<K, V>) super.clone();
} catch (CloneNotSupportedException e) {
- throw new IllegalStateException("Clone is expected to work", e);
+ throw new IllegalStateException("Clone is expected to work", e);
}
- ret.newKeys = (HashMap<K, V>) newKeys.clone();
+ ret.newKeys = newKeys == null ? null : (HashMap<K, V>) newKeys.clone();
ret.needClone = true;
return ret;
}
}
}
- return result + newKeys.hashCode();
+ return newKeys != null ? result + newKeys.hashCode() : result;
}
@Override
- public final boolean equals(final Object o) {
- if (o == this) {
+ public final boolean equals(final Object obj) {
+ if (obj == this) {
return true;
}
- if (!(o instanceof Map)) {
+ if (!(obj instanceof Map)) {
return false;
}
- if (o instanceof ImmutableOffsetMap) {
- final ImmutableOffsetMap<?, ?> om = (ImmutableOffsetMap<?, ?>) o;
+ if (obj instanceof ImmutableOffsetMap) {
+ final ImmutableOffsetMap<?, ?> om = (ImmutableOffsetMap<?, ?>) obj;
- if (newKeys.isEmpty() && offsets.equals(om.offsets())) {
+ if (noNewKeys() && offsets.equals(om.offsets())) {
return Arrays.deepEquals(objects, om.objects());
}
- } else if (o instanceof MutableOffsetMap) {
- final MutableOffsetMap<?, ?> om = (MutableOffsetMap<?, ?>) o;
+ } else if (obj instanceof MutableOffsetMap) {
+ final MutableOffsetMap<?, ?> om = (MutableOffsetMap<?, ?>) obj;
if (offsets.equals(om.offsets)) {
- return Arrays.deepEquals(objects, om.objects) && newKeys.equals(om.newKeys);
+ return Arrays.deepEquals(objects, om.objects) && equalNewKeys(om);
}
}
// Fall back to brute map compare
- final Map<?, ?> other = (Map<?, ?>)o;
+ return mapEquals((Map<?, ?>)obj);
+ }
+
+ private boolean equalNewKeys(final MutableOffsetMap<?, ?> other) {
+ return noNewKeys() ? other.noNewKeys() : newKeys.equals(other.newKeys());
+ }
+ private boolean mapEquals(final Map<?, ?> other) {
// Size and key sets have to match
if (size() != other.size() || !keySet().equals(other.keySet())) {
return false;
}
try {
- // Ensure all newKeys are present. Note newKeys is guaranteed to
- // not contain null value.
- for (Entry<K, V> e : newKeys.entrySet()) {
- if (!e.getValue().equals(other.get(e.getKey()))) {
- return false;
+ if (newKeys != null) {
+ // Ensure all newKeys are present. Note newKeys is guaranteed to not contain a null value.
+ for (Entry<K, V> e : newKeys.entrySet()) {
+ if (!e.getValue().equals(other.get(e.getKey()))) {
+ return false;
+ }
}
}
// Ensure all objects are present
for (Entry<K, Integer> e : offsets.entrySet()) {
- final Object obj = objects[e.getValue()];
- if (obj != null && !REMOVED.equals(obj) && !obj.equals(other.get(e.getKey()))) {
+ final Object val = objects[e.getValue()];
+ if (val != null && !REMOVED.equals(val) && !val.equals(other.get(e.getKey()))) {
return false;
}
}
}
@Override
- public final Set<K> keySet() {
+ public final @NonNull Set<K> keySet() {
return new KeySet();
}
@VisibleForTesting
final Object newKeys() {
- return newKeys;
+ return newKeys != null ? newKeys : ImmutableMap.of();
+ }
+
+ abstract HashMap<K, V> createNewKeys();
+
+ private boolean noNewKeys() {
+ return newKeys == null || newKeys.isEmpty();
}
private final class EntrySet extends AbstractSet<Entry<K, V>> {
@Override
- public Iterator<Entry<K, V>> iterator() {
- return new AbstractSetIterator<Entry<K, V>>() {
+ public @NonNull Iterator<Entry<K, V>> iterator() {
+ return new AbstractSetIterator<>() {
@Override
public Entry<K, V> next() {
final K key = nextKey();
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public boolean contains(final Object o) {
if (!(o instanceof Entry)) {
return false;
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public boolean add(final Entry<K, V> e) {
- Preconditions.checkNotNull(e.getValue());
- final V p = MutableOffsetMap.this.put(e.getKey(), e.getValue());
- return !e.getValue().equals(p);
+ final V v = requireNonNull(e.getValue());
+ final V p = MutableOffsetMap.this.put(e.getKey(), v);
+ return !v.equals(p);
}
@Override
+ @SuppressWarnings("checkstyle:parameterName")
public boolean remove(final Object o) {
if (!(o instanceof Entry)) {
return false;
private final class KeySet extends AbstractSet<K> {
@Override
- public Iterator<K> iterator() {
- return new AbstractSetIterator<K>() {
+ public @NonNull Iterator<K> iterator() {
+ return new AbstractSetIterator<>() {
@Override
public K next() {
return nextKey();
private abstract class AbstractSetIterator<E> implements Iterator<E> {
private final Iterator<Entry<K, Integer>> oldIterator = offsets.entrySet().iterator();
- private final Iterator<K> newIterator = newKeys.keySet().iterator();
+ private final Iterator<K> newIterator = newKeys == null ? Collections.emptyIterator()
+ : newKeys.keySet().iterator();
private int expectedModCount = modCount;
- private K currentKey, nextKey;
+ private @Nullable K currentKey = null;
+ private @Nullable K nextKey;
AbstractSetIterator() {
updateNextKey();
@Override
public final void remove() {
- Preconditions.checkState(currentKey != null);
-
checkModCount();
+ checkState(currentKey != null);
final Integer offset = offsets.get(currentKey);
if (offset != null) {
cloneArray();