BUG-7464: improve keySet()/entrySet() bytecode
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / TrieMap.java
index b93fa4a367fea9b9043e0a4adcbb790a176e2c5b..5bcefcaa1e46f289e86bb6b606f9986d56cd8e93 100644 (file)
@@ -22,12 +22,11 @@ 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.
  *
@@ -45,6 +44,7 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
     private final Equivalence<? super K> equiv;
 
     private AbstractEntrySet<K, V> entrySet;
+    private AbstractKeySet<K> keySet;
 
     TrieMap(final Equivalence<? super K> equiv) {
         this.equiv = equiv;
@@ -58,6 +58,7 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
      * Returns a snapshot of this TrieMap. This operation is lock-free and
      * linearizable.
      *
+     * <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
@@ -70,6 +71,7 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
      * 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
@@ -77,6 +79,7 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
      * unlike when calling the `snapshot` method, but the obtained snapshot
      * cannot be modified.
      *
+     * <p>
      * This method is used by other methods such as `size` and `iterator`.
      */
     public abstract ImmutableTrieMap<K, V> immutableSnapshot();
@@ -93,11 +96,14 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
 
     @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
@@ -135,6 +141,8 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
 
     abstract AbstractEntrySet<K, V> createEntrySet();
 
+    abstract AbstractKeySet<K> createKeySet();
+
     abstract boolean isReadOnly();
 
     abstract INode<K, V> RDCSS_READ_ROOT(boolean abort);
@@ -142,25 +150,26 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
     /**
      * 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")
@@ -168,8 +177,8 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
         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 {
@@ -202,11 +211,11 @@ public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements Concurr
     /* 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().rec_lookup(key, hc, 0, null, this);
         } while (res == RESTART);
 
         return (V) res;