BUG-7464: fix INode.rec_lookup() return type 11/49911/8
authorRobert Varga <rovarga@cisco.com>
Sun, 1 Jan 2017 17:14:49 +0000 (18:14 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 10 Jan 2017 19:12:11 +0000 (20:12 +0100)
Undo a blanket check for Optional in TrieMap.lookup(),
which is caused by passing LNode's result back to the caller.

With Optional being used, this can be simplified by
unwrapping the result with orElse(null).

Change-Id: Ic70e65f76e7132db93e28d55141fef992ef00692
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
third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestDelete.java
third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestHashCollisions.java
third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestInsert.java
third-party/triemap/src/test/java/org/opendaylight/yangtools/triemap/TestMultiThreadInserts.java

index ab01536aad15eea9fe326eed90d10ffcb5352522..40bb592fecc8729c526e0cc8b82e58f45738c5e9 100644 (file)
@@ -394,7 +394,7 @@ final class INode<K, V> extends INodeBase<K, V> {
                 return cleanReadOnly((TNode<K, V>) m, lev, parent, ct, k, hc);
             } else if (m instanceof LNode) {
                 // 5) an l-node
-                return ((LNode<K, V>) m).get(k);
+                return ((LNode<K, V>) m).get(k).orElse(null);
             } else {
                 throw new IllegalStateException("Unhandled node " + m);
             }
@@ -406,7 +406,7 @@ final class INode<K, V> extends INodeBase<K, V> {
     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
+            // used to be throw RestartException
             clean(parent, ct, lev - 5);
             return RESTART;
         }
index 676cd7137c437500787f9129d4c316d4262b68ef..f0083d39fac4e1d60eef50ebf1c66d7c45a7a33a 100644 (file)
@@ -313,14 +313,7 @@ public final class TrieMap<K, V> extends AbstractMap<K, V> implements Concurrent
     }
 
     V lookup(final K k) {
-        final int hc = computeHash (k);
-//        return (V) lookuphc (k, hc);
-        final Object o = lookuphc (k, hc);
-        if (o instanceof Optional) {
-            return ((Optional<V>) o).orElse(null);
-        }
-
-        return (V)o;
+        return (V) lookuphc(k, computeHash(k));
     }
 
     @Override
index e79ebc3362049d504769193bbb32b2fe526ab598..4f3cda74fb6d63ca143a6f27124488f27d25a5a8 100644 (file)
@@ -28,7 +28,7 @@ public class TestDelete {
 
         for (int i = 0; i < 10000; i++) {
             assertNull(bt.put(Integer.valueOf(i), Integer.valueOf(i)));
-            assertEquals(Integer.valueOf(i), bt.lookup(Integer.valueOf(i)));
+            assertEquals(Integer.valueOf(i), bt.get(Integer.valueOf(i)));
         }
 
         checkAddInsert(bt, 536);
@@ -38,7 +38,7 @@ public class TestDelete {
         for (int i = 0; i < 10000; i++) {
             boolean removed = null != bt.remove(Integer.valueOf(i));
             assertTrue(removed);
-            final Object lookup = bt.lookup (Integer.valueOf(i));
+            final Object lookup = bt.get(Integer.valueOf(i));
             assertNull(lookup);
         }
 
@@ -66,10 +66,10 @@ public class TestDelete {
     private static void checkAddInsert (final TrieMap<Integer, Integer> bt, final int k) {
         final Integer v = Integer.valueOf(k);
         bt.remove (v);
-        Object foundV = bt.lookup(v);
+        Integer foundV = bt.get(v);
         assertNull(foundV);
         assertNull(bt.put (v, v));
-        foundV = bt.lookup(v);
+        foundV = bt.get(v);
         assertEquals(v, foundV);
 
         assertEquals(v, bt.put(v, Integer.valueOf(-1)));
index a2b6f03c137263b4f1480cda63180788ed9b1b11..5ed9fd1bc6873c6549684672985ebe9f3c7e8b8f 100644 (file)
@@ -122,11 +122,11 @@ public class TestHashCollisions {
     }
 
     private static void removeChars (final TrieMap<Object, Object> bt) {
-        assertNotNull(bt.lookup('a'));
-        assertNotNull(bt.lookup('b'));
-        assertNotNull(bt.lookup('c'));
-        assertNotNull(bt.lookup('d'));
-        assertNotNull(bt.lookup('e'));
+        assertNotNull(bt.get('a'));
+        assertNotNull(bt.get('b'));
+        assertNotNull(bt.get('c'));
+        assertNotNull(bt.get('d'));
+        assertNotNull(bt.get('e'));
 
         assertNotNull(bt.remove('a'));
         assertNotNull(bt.remove('b'));
@@ -140,19 +140,19 @@ public class TestHashCollisions {
         assertNull(bt.remove('d'));
         assertNull(bt.remove('e'));
 
-        assertNull(bt.lookup('a'));
-        assertNull(bt.lookup('b'));
-        assertNull(bt.lookup('c'));
-        assertNull(bt.lookup('d'));
-        assertNull(bt.lookup('e'));
+        assertNull(bt.get('a'));
+        assertNull(bt.get('b'));
+        assertNull(bt.get('c'));
+        assertNull(bt.get('d'));
+        assertNull(bt.get('e'));
     }
 
     private static void removeStrings (final TrieMap<Object, Object> bt) {
-        assertNotNull(bt.lookup("a"));
-        assertNotNull(bt.lookup("b"));
-        assertNotNull(bt.lookup("c"));
-        assertNotNull(bt.lookup("d"));
-        assertNotNull(bt.lookup("e"));
+        assertNotNull(bt.get("a"));
+        assertNotNull(bt.get("b"));
+        assertNotNull(bt.get("c"));
+        assertNotNull(bt.get("d"));
+        assertNotNull(bt.get("e"));
 
         assertNotNull(bt.remove("a"));
         assertNotNull(bt.remove("b"));
@@ -166,30 +166,30 @@ public class TestHashCollisions {
         assertNull(bt.remove("d"));
         assertNull(bt.remove("e"));
 
-        assertNull(bt.lookup("a"));
-        assertNull(bt.lookup("b"));
-        assertNull(bt.lookup("c"));
-        assertNull(bt.lookup("d"));
-        assertNull(bt.lookup("e"));
+        assertNull(bt.get("a"));
+        assertNull(bt.get("b"));
+        assertNull(bt.get("c"));
+        assertNull(bt.get("d"));
+        assertNull(bt.get("e"));
     }
 
     private static void removeInts (final TrieMap<Object, Object> bt) {
         for (int i = 0; i < 128; i++) {
-            final Integer bigI = Integer.valueOf (i);
-            assertNotNull(bt.lookup(bigI));
+            final Integer bigI = Integer.valueOf(i);
+            assertNotNull(bt.get(bigI));
             assertNotNull(bt.remove(bigI));
             assertNull(bt.remove(bigI));
-            assertNull(bt.lookup(bigI));
+            assertNull(bt.get(bigI));
         }
     }
 
     private static void removeBytes (final TrieMap<Object, Object> bt) {
         for (byte i = 0; i < 128 && i >= 0; i++) {
-            final Byte bigB = Byte.valueOf (i);
-            assertNotNull(bt.lookup(bigB));
+            final Byte bigB = Byte.valueOf(i);
+            assertNotNull(bt.get(bigB));
             assertNotNull(bt.remove(bigB));
             assertNull(bt.remove(bigB));
-            assertNull(bt.lookup(bigB));
+            assertNull(bt.get(bigB));
         }
     }
 }
index 3989cc4f0204f2f12242c3e284416857458d5753..81f66e869dd6b319421d31efd9890575515f8d5f 100644 (file)
@@ -32,7 +32,7 @@ public class TestInsert {
 
         for (int i = 0; i < 10000; i++) {
             assertNull(bt.put(Integer.valueOf (i), Integer.valueOf(i)));
-            final Object lookup = bt.lookup(Integer.valueOf(i));
+            final Object lookup = bt.get(Integer.valueOf(i));
             assertEquals(Integer.valueOf(i), lookup);
         }
 
index ea687d5cfacab8fbd84d790ef0afc9ca82750bfd..b80a94921f7981f7d50ceeeef80ca13ded4ba6ca 100644 (file)
@@ -33,7 +33,7 @@ public class TestMultiThreadInserts {
             es.execute (() -> {
                 for (int j = 0; j < 500 * 1000; j++) {
                     if (j % nThreads == threadNo) {
-                        bt.put (Integer.valueOf (j), Integer.valueOf (j));
+                        bt.put (Integer.valueOf(j), Integer.valueOf(j));
                     }
                 }
             });
@@ -43,7 +43,7 @@ public class TestMultiThreadInserts {
         es.awaitTermination(5, TimeUnit.MINUTES);
 
         for (int j = 0; j < 500 * 1000; j++) {
-            final Object lookup = bt.lookup (Integer.valueOf (j));
+            final Object lookup = bt.get(Integer.valueOf(j));
             assertEquals(Integer.valueOf(j), lookup);
         }
     }