BUG-7464: Encapsupate special objects. 18/49918/8
authorRobert Varga <rovarga@cisco.com>
Sun, 1 Jan 2017 23:17:28 +0000 (00:17 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 10 Jan 2017 19:12:11 +0000 (20:12 +0100)
These really are singletons, hence making them type-safe
as enums makes sense, as it expresses their nature.

Change-Id: I3f1b7f953bcb36ea7d72b594bfedcd4782e6767e
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/LookupResult.java [new file with mode: 0644]
third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/PresencePredicate.java [new file with mode: 0644]
third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/TrieMap.java

index d39583e04cd1389b0a412bf6ba1ad0e98e28f19d..752dfb9874ed9a4b1e24c77901c653d02e564b60 100644 (file)
  */
 package org.opendaylight.yangtools.triemap;
 
+import static org.opendaylight.yangtools.triemap.LookupResult.RESTART;
+import static org.opendaylight.yangtools.triemap.PresencePredicate.ABSENT;
+import static org.opendaylight.yangtools.triemap.PresencePredicate.PRESENT;
+
 import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 
 final class INode<K, V> extends BasicNode {
-
-    static final Object KEY_PRESENT = new Object ();
-    static final Object KEY_ABSENT = new Object ();
-
-    /**
-     * Virtual result for lookup methods indicating that the lookup needs to be restarted. This is a faster version
-     * of throwing a checked exception to control the restart.
-     */
-    static final Object RESTART = new Object();
-
     @SuppressWarnings("rawtypes")
     private static final AtomicReferenceFieldUpdater<INode, MainNode> MAINNODE_UPDATER =
             AtomicReferenceFieldUpdater.newUpdater(INode.class, MainNode.class, "mainnode");
@@ -257,7 +251,7 @@ final class INode<K, V> extends BasicNode {
                             }
 
                             return null;
-                        } else if (cond == INode.KEY_ABSENT) {
+                        } else if (cond == ABSENT) {
                             if (sn.hc == hc && ct.equal(sn.k, k)) {
                                 return Optional.of(sn.v);
                             }
@@ -270,7 +264,7 @@ final class INode<K, V> extends BasicNode {
                             }
 
                             return null;
-                        } else if (cond == INode.KEY_PRESENT) {
+                        } else if (cond == PRESENT) {
                             if (sn.hc == hc && ct.equal(sn.k, k)) {
                                 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
                                     return Optional.of(sn.v);
@@ -291,7 +285,7 @@ final class INode<K, V> extends BasicNode {
                             return Optional.empty();
                         }
                     }
-                } else if (cond == null || cond == INode.KEY_ABSENT) {
+                } else if (cond == null || cond == ABSENT) {
                     final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
                     final CNode<K, V> ncnode = rn.insertedAt (pos, flag, new SNode<>(k, v, hc), gen);
                     if (GCAS(cn, ncnode, ct)) {
@@ -299,8 +293,6 @@ final class INode<K, V> extends BasicNode {
                     }
 
                     return null;
-                } else if (cond == INode.KEY_PRESENT) {
-                    return Optional.empty();
                 } else {
                     return Optional.empty();
                 }
@@ -316,7 +308,7 @@ final class INode<K, V> extends BasicNode {
                         return optv;
                     }
                     return null;
-                } else if (cond == INode.KEY_ABSENT) {
+                } else if (cond == ABSENT) {
                     final Optional<V> t = ln.get(k);
                     if (t.isPresent()) {
                         return t;
@@ -325,7 +317,7 @@ final class INode<K, V> extends BasicNode {
                         return Optional.empty();
                     }
                     return null;
-                } else if (cond == INode.KEY_PRESENT) {
+                } else if (cond == PRESENT) {
                     final Optional<V> t = ln.get(k);
                     if (!t.isPresent()) {
                         return t;
@@ -403,7 +395,6 @@ final class INode<K, V> extends BasicNode {
                         continue;
                     }
 
-                    // used to be throw RestartException
                     return RESTART;
                 } else if (sub instanceof SNode) {
                     // 2) singleton node
@@ -438,7 +429,6 @@ final class INode<K, V> extends BasicNode {
             return null;
         }
 
-        // used to be throw RestartException
         clean(parent, ct, lev - 5);
         return RESTART;
     }
diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/LookupResult.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/LookupResult.java
new file mode 100644 (file)
index 0000000..b9535e9
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.opendaylight.yangtools.triemap;
+
+/**
+ * Virtual result for lookup methods indicating that the lookup needs to be restarted. This is a faster version
+ * of throwing a checked exception to control restart.
+ *
+ * @author Robert Varga
+ */
+enum LookupResult {
+    RESTART;
+}
diff --git a/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/PresencePredicate.java b/third-party/triemap/src/main/java/org/opendaylight/yangtools/triemap/PresencePredicate.java
new file mode 100644 (file)
index 0000000..297b52b
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.opendaylight.yangtools.triemap;
+
+/**
+ * Presence predicate. These are specialized objects passed down from callers to indicate an assertion about
+ * whether a mapping is required.
+ *
+ * @author Robert Varga
+ */
+enum PresencePredicate {
+    ABSENT,
+    PRESENT;
+}
index 44cccbf185b57c9b9e35cd85a1c16c219970be41..b9071d480d3e10d99c1f49c4a65330819d468bd6 100644 (file)
@@ -17,6 +17,9 @@ package org.opendaylight.yangtools.triemap;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
+import static org.opendaylight.yangtools.triemap.LookupResult.RESTART;
+import static org.opendaylight.yangtools.triemap.PresencePredicate.ABSENT;
+import static org.opendaylight.yangtools.triemap.PresencePredicate.PRESENT;
 
 import com.google.common.base.Verify;
 import com.google.common.collect.Iterators;
@@ -185,7 +188,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
         do {
             // Keep looping as long as RESTART is being indicated
             res = RDCSS_READ_ROOT().rec_lookup(k, hc, 0, null, this);
-        } while (INode.RESTART.equals(res));
+        } while (res == RESTART);
 
         return (V) res;
     }
@@ -314,7 +317,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
     public V putIfAbsent(final K key, final V value) {
         ensureReadWrite();
         final K k = checkNotNull(key);
-        return insertifhc(k, computeHash(k), checkNotNull(value), INode.KEY_ABSENT).orElse(null);
+        return insertifhc(k, computeHash(k), checkNotNull(value), ABSENT).orElse(null);
     }
 
     @Override
@@ -335,7 +338,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
     public V replace(final K key, final V value) {
         ensureReadWrite();
         final K k = checkNotNull(key);
-        return insertifhc (k, computeHash(k), checkNotNull(value), INode.KEY_PRESENT).orElse(null);
+        return insertifhc (k, computeHash(k), checkNotNull(value), PRESENT).orElse(null);
     }
 
     /***