*/
package org.opendaylight.yangtools.triemap;
-import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Objects.requireNonNull;
import static org.opendaylight.yangtools.triemap.LookupResult.RESTART;
import com.google.common.annotations.Beta;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.AbstractMap;
-import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
-/***
+/**
* This is a port of Scala's TrieMap class from the Scala Collections library. This implementation does not support
* null keys nor null values.
*
private final Equivalence<? super K> equiv;
private AbstractEntrySet<K, V> entrySet;
+ private AbstractKeySet<K> keySet;
TrieMap(final Equivalence<? super K> equiv) {
this.equiv = equiv;
}
- public static <K, V> TrieMap<K, V> create() {
+ public static <K, V> MutableTrieMap<K, V> create() {
return new MutableTrieMap<>(Equivalence.equals());
}
/**
* Returns a snapshot of this TrieMap. This operation is lock-free and
- * linearizable.
+ * linearizable. Modification operations on this Map and the returned one
+ * are isolated from each other.
*
+ * <p>
* The snapshot is lazily updated - the first time some branch in the
* snapshot or this TrieMap are accessed, they are rewritten. This means
* that the work of rebuilding both the snapshot and this TrieMap is
* distributed across all the threads doing updates or accesses subsequent
* to the snapshot creation.
+ *
+ * @return A read-write TrieMap containing the contents of this map.
*/
public abstract TrieMap<K, V> mutableSnapshot();
* Returns a read-only snapshot of this TrieMap. This operation is lock-free
* and linearizable.
*
+ * <p>
* The snapshot is lazily updated - the first time some branch of this
* TrieMap are accessed, it is rewritten. The work of creating the snapshot
* is thus distributed across subsequent updates and accesses on this
* TrieMap by all threads. Note that the snapshot itself is never rewritten
- * unlike when calling the `snapshot` method, but the obtained snapshot
+ * unlike when calling {@link #mutableSnapshot()}, but the obtained snapshot
* cannot be modified.
*
+ * <p>
* This method is used by other methods such as `size` and `iterator`.
+ *
+ * @return A read-only TrieMap containing the contents of this map.
*/
public abstract ImmutableTrieMap<K, V> immutableSnapshot();
@Override
public final boolean containsValue(final Object value) {
- return super.containsValue(checkNotNull(value));
+ return super.containsValue(requireNonNull(value));
}
@Override
public final Set<Entry<K, V>> entrySet() {
- AbstractEntrySet<K, V> ret = entrySet;
- if (ret == null) {
- entrySet = ret = createEntrySet();
- }
- return ret;
+ final AbstractEntrySet<K, V> ret;
+ return (ret = entrySet) != null ? ret : (entrySet = createEntrySet());
+ }
+
+ @Override
+ public final Set<K> keySet() {
+ final AbstractKeySet<K> ret;
+ return (ret = keySet) != null ? ret : (keySet = createKeySet());
}
@Override
public final V get(final Object key) {
@SuppressWarnings("unchecked")
- final K k = (K) checkNotNull(key);
+ final K k = (K) requireNonNull(key);
return lookuphc(k, computeHash(k));
}
abstract AbstractEntrySet<K, V> createEntrySet();
+ abstract AbstractKeySet<K> createKeySet();
+
abstract boolean isReadOnly();
abstract INode<K, V> RDCSS_READ_ROOT(boolean abort);
/**
* Return an iterator over a TrieMap.
*
+ * <p>
* If this is a read-only snapshot, it would return a read-only iterator.
*
+ * <p>
* If it is the original TrieMap or a non-readonly snapshot, it would return
* an iterator that would allow for updates.
*
- * @return
+ * @return An iterator.
*/
- abstract Iterator<Entry<K, V>> iterator();
+ abstract AbstractIterator<K, V> iterator();
/* internal methods provided for subclasses */
/**
- * Return an iterator over a TrieMap.
- * This is a read-only iterator.
+ * Return an iterator over a TrieMap. This is a read-only iterator.
*
- * @return
+ * @return A read-only iterator.
*/
- final Iterator<Entry<K, V>> immutableIterator() {
- return new TrieMapReadOnlyIterator<>(0, immutableSnapshot());
+ final ImmutableIterator<K, V> immutableIterator() {
+ return new ImmutableIterator<>(immutableSnapshot());
}
@SuppressWarnings("null")
return opt.orElse(null);
}
- final int computeHash(final K k) {
- return equiv.hash(k);
+ final int computeHash(final K key) {
+ return equiv.hash(key);
}
final Object writeReplace() throws ObjectStreamException {
/* private implementation methods */
@SuppressWarnings("unchecked")
- private V lookuphc(final K k, final int hc) {
+ private V lookuphc(final K key, final int hc) {
Object res;
do {
// Keep looping as long as RESTART is being indicated
- res = RDCSS_READ_ROOT().rec_lookup(k, hc, 0, null, this);
+ res = RDCSS_READ_ROOT().recLookup(key, hc, 0, null, this);
} while (res == RESTART);
return (V) res;