BUG-7464: Reduce instanceof checks to null checks
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / TrieMap.java
index ce9409e8c0999ede623be99e17cda8344c373352..3b45b7ed1d3b62e8ee0b738ef7a77b18e2b5d347 100644 (file)
@@ -114,7 +114,7 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
     }
 
     public TrieMap (final Hashing<K> hashf, final Equiv<K> ef) {
-        this(INode.newRootNode(), hashf, ef, false);
+        this(newRootNode(), hashf, ef, false);
     }
 
     public TrieMap () {
@@ -155,6 +155,11 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
     // } while (obj != TrieMapSerializationEnd);
     // }
 
+    private static <K,V> INode<K,V> newRootNode() {
+        final Gen gen = new Gen();
+        return new INode<>(gen, new CNode<>(gen));
+    }
+
     final boolean CAS_ROOT (final Object ov, final Object nv) {
         if (isReadOnly()) {
             throw new IllegalStateException("Attempted to modify a read-only snapshot");
@@ -269,17 +274,15 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
         }
     }
 
-    private Object lookuphc (final K k, final int hc) {
+    private Object lookuphc(final K k, final int hc) {
         while (true) {
-            INode<K, V> r = RDCSS_READ_ROOT ();
-            Object res = r.rec_lookup (k, hc, 0, null, r.gen, this);
-            if (res == INodeBase.RESTART) {
-                // return lookuphc (k, hc);
-                // tailrec
-                continue;
-            } else {
+            final INode<K, V> r = RDCSS_READ_ROOT ();
+            final Object res = r.rec_lookup(k, hc, 0, null, r.gen, this);
+            if (!INode.RESTART.equals(res)) {
                 return res;
             }
+
+            // Tail recursion: lookuphc(k, hc)
         }
     }
 
@@ -392,7 +395,7 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
     final public void clear () {
         while (true) {
             INode<K, V> r = RDCSS_READ_ROOT ();
-            if (!RDCSS_ROOT (r, r.gcasRead (this), INode.<K, V>newRootNode ())) {
+            if (!RDCSS_ROOT(r, r.gcasRead(this), newRootNode())) {
                 continue;
             }else{
                 return;
@@ -665,7 +668,7 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
                 }
 
                 lastReturned = r;
-                if(r instanceof Map.Entry) {
+                if (r != null) {
                     final Map.Entry<K, V> rr = r;
                     return nextEntry(rr);
                 }
@@ -905,7 +908,7 @@ public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
 
     private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
         inputStream.defaultReadObject();
-        this.root = INode.newRootNode();
+        this.root = newRootNode();
 
         final boolean ro = inputStream.readBoolean();
         final int size = inputStream.readInt();