BUG-7464: Eliminate else branches in INode.GCAS_Complete 78/49878/6
authorRobert Varga <rovarga@cisco.com>
Fri, 30 Dec 2016 15:16:29 +0000 (16:16 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 9 Jan 2017 14:17:12 +0000 (15:17 +0100)
Eclipse warnings of using else branch when the code
in that branch cannot be normally reached. Cleans up
the logic, showing more potential for tail recursion.

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

index 2a59f01afea4e73a3eda6f00738851f10ace1975..b9444056876d93c38ee35dc85b65e2405996a18e 100644 (file)
@@ -47,55 +47,50 @@ final class INode<K, V> extends INodeBase<K, V> {
         while (true) {
             if (m == null) {
                 return null;
-            } else {
-                // complete the GCAS
-                final MainNode<K, V> prev = /* READ */ m.READ_PREV();
-                INode<K, V> ctr = ct.readRoot(true);
+            }
 
-                if (prev == null) {
-                    return m;
+            // complete the GCAS
+            final MainNode<K, V> prev = /* READ */ m.READ_PREV();
+            final INode<K, V> ctr = ct.readRoot(true);
+            if (prev == null) {
+                return m;
+            }
+
+            if (prev instanceof FailedNode) {
+                // try to commit to previous value
+                FailedNode<K, V> fn = (FailedNode<K, V>) prev;
+                if (CAS(m, fn.READ_PREV())) {
+                    return fn.READ_PREV();
                 }
 
-                if (prev instanceof FailedNode) {
-                    // try to commit to previous value
-                    FailedNode<K, V> fn = (FailedNode<K, V>) prev;
-                    if (CAS(m, fn.READ_PREV())) {
-                        return fn.READ_PREV();
-                    } else {
-                        // Tailrec
-                        // return GCAS_Complete (/* READ */mainnode, ct);
-                        m = /* READ */ READ();
-                        continue;
-                    }
-                } else {
-                    // Assume that you've read the root from the generation
-                    // G.
-                    // Assume that the snapshot algorithm is correct.
-                    // ==> you can only reach nodes in generations <= G.
-                    // ==> `gen` is <= G.
-                    // We know that `ctr.gen` is >= G.
-                    // ==> 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()) {
-                        // try to commit
-                        if (m.CAS_PREV(prev, null)) {
-                            return m;
-                        } else {
-                            // return GCAS_Complete (m, ct);
-                            // tailrec
-                            continue;
-                        }
-                    } else {
-                        // try to abort
-                        m.CAS_PREV(prev, new FailedNode<>(prev));
-                        return GCAS_Complete(/* READ */ READ(), ct);
-                    }
+                // Tail recursion: return GCAS_Complete (/* READ */ READ(), ct);
+                m = /* READ */ READ();
+                continue;
+            }
+
+            // Assume that you've read the root from the generation G.
+            // Assume that the snapshot algorithm is correct.
+            // ==> you can only reach nodes in generations <= G.
+            // ==> `gen` is <= G.
+            // We know that `ctr.gen` is >= G.
+            // ==> 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()) {
+                // try to commit
+                if (m.CAS_PREV(prev, null)) {
+                    return m;
                 }
+
+                // Tail recursion: return GCAS_Complete (m, ct);
+                continue;
             }
+
+            // try to abort
+            m.CAS_PREV(prev, new FailedNode<>(prev));
+
+            // Tail recursion: return GCAS_Complete(/* READ */ READ(), ct);
+            m = /* READ */ READ();
         }
     }