BUG-7464: remove TrieMap.nonReadOnly() 13/49913/8
authorRobert Varga <rovarga@cisco.com>
Sun, 1 Jan 2017 18:22:24 +0000 (19:22 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 10 Jan 2017 19:12:11 +0000 (20:12 +0100)
This is a superfluous utility method, remove it and align
call sites.

Change-Id: I65266640bc74b2548b56a4712534b135bb92bcf0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/INode.java
third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java

index 2e8efe65241282beb0e125e9e6a232d032ffa2b7..2be06f0ad507f61a52860681e1652abba99c3da4 100644 (file)
@@ -88,7 +88,7 @@ final class INode<K, V> extends BasicNode {
             // ==> if `ctr.gen` = `gen` then they are both equal to G.
             // ==> otherwise, we know that either `ctr.gen` > G, `gen` < G,
             // or both
-            if ((ctr.gen == gen) && ct.nonReadOnly()) {
+            if (ctr.gen == gen && !ct.isReadOnly()) {
                 // try to commit
                 if (m.CAS_PREV(prev, null)) {
                     return m;
@@ -415,17 +415,17 @@ final class INode<K, V> extends BasicNode {
 
     private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
             final TrieMap<K, V> ct, final K k, final int hc) {
-        if (ct.nonReadOnly()) {
-            // used to be throw RestartException
-            clean(parent, ct, lev - 5);
-            return RESTART;
-        }
+        if (ct.isReadOnly()) {
+            if (tn.hc == hc && ct.equal(tn.k, k)) {
+                return tn.v;
+            }
 
-        if (tn.hc == hc && ct.equal(tn.k, k)) {
-            return tn.v;
+            return null;
         }
 
-        return null;
+        // used to be throw RestartException
+        clean(parent, ct, lev - 5);
+        return RESTART;
     }
 
     /**
index f0083d39fac4e1d60eef50ebf1c66d7c45a7a33a..8a2b674ef90642c8cbf67c9af59fcaf4dd5b6d41 100644 (file)
@@ -15,6 +15,7 @@
  */
 package org.opendaylight.yangtools.triemap;
 
+import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
@@ -88,10 +89,8 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
         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");
-        }
+    final boolean CAS_ROOT(final Object ov, final Object nv) {
+        Preconditions.checkState(!readOnly, "Attempted to modify a read-only snapshot");
         return ROOT_UPDATER.compareAndSet (this, ov, nv);
     }
 
@@ -227,7 +226,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
      * otherwise. Used by Map-type methods for quick check.
      */
     private void ensureReadWrite() {
-        if (isReadOnly()) {
+        if (readOnly) {
             throw new UnsupportedOperationException("Attempted to modify a read-only view");
         }
     }
@@ -236,10 +235,6 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
         return readOnly;
     }
 
-    boolean nonReadOnly() {
-        return !readOnly;
-    }
-
     /* public methods */
 
     /**
@@ -279,7 +274,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
      */
     public TrieMap<K, V> readOnlySnapshot() {
         // Is it a snapshot of a read-only snapshot?
-        if (isReadOnly()) {
+        if (readOnly) {
             return this;
         }
 
@@ -379,12 +374,8 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
      *
      * @return
      */
-    Iterator<Entry<K, V>> iterator () {
-        if (!nonReadOnly()) {
-            return readOnlySnapshot().readOnlyIterator();
-        }
-
-        return new TrieMapIterator<> (0, this);
+    Iterator<Entry<K, V>> iterator() {
+        return readOnly ? new TrieMapReadOnlyIterator<>(0, this) : new TrieMapIterator<>(0, this);
     }
 
     /***
@@ -393,12 +384,8 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
      *
      * @return
      */
-    Iterator<Entry<K, V>> readOnlyIterator () {
-        if (nonReadOnly()) {
-            return readOnlySnapshot().readOnlyIterator();
-        }
-
-        return new TrieMapReadOnlyIterator<>(0, this);
+    Iterator<Entry<K, V>> readOnlyIterator() {
+        return new TrieMapReadOnlyIterator<>(0, readOnly ? this : readOnlySnapshot());
     }
 
     private int cachedSize() {
@@ -408,11 +395,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
 
     @Override
     public int size() {
-        if (nonReadOnly()) {
-            return readOnlySnapshot().size ();
-        }
-
-        return cachedSize();
+        return readOnly ? cachedSize() : readOnlySnapshot().size();
     }
 
     @Override
@@ -759,7 +742,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
         outputStream.defaultWriteObject();
 
         final Map<K, V> ro = readOnlySnapshot();
-        outputStream.writeBoolean(isReadOnly());
+        outputStream.writeBoolean(readOnly);
         outputStream.writeInt(ro.size());
 
         for (Entry<K, V> e : ro.entrySet()) {