Refactor globalOrStatementSpecific() 83/105283/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 6 Apr 2023 21:42:52 +0000 (23:42 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 6 Apr 2023 21:42:52 +0000 (23:42 +0200)
Current implementation elicits a nullness warning, which is not quite
there -- at the cost of verifyNotNull().

If we refactor checks we can actually make it more performant and
transparent. This is advanteous bacause a null parent implies current is
a GLOBAL storage -- and we therefore can just check the storage type for
STATEMENT_LOCAL.

As it turns out we can refactor the loop to make it obvious, in that we
manually check parent storage. This makes the code flow nicer and more
transparent -- there are no IDE-invisible non-null guarantees. A null
parent storage indicates GLOBAL storage, hence we can bail.

A further boon is that we can simplify the StorageType check to only
compare to STATEMENT_LOCAL and

Change-Id: I5a5b7faf312db6de7f1cebe654f18e9785f58bac
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/SchemaTreeNamespaceBehaviour.java

index 6e276b26fd66947832dfe9c3014cf68d4229f5e4..dd286c92aed9d10adcfa679c896b0a3469398552 100644 (file)
@@ -7,9 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi;
 
-import static com.google.common.base.Verify.verifyNotNull;
-import static java.util.Objects.requireNonNull;
-
 import java.util.Map;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
@@ -75,14 +72,18 @@ final class SchemaTreeNamespaceBehaviour<D extends DeclaredStatement<QName>, E e
     }
 
     private static NamespaceStorage globalOrStatementSpecific(final NamespaceStorage storage) {
-        NamespaceStorage current = requireNonNull(storage);
-        while (!isLocalOrGlobal(current.getStorageType())) {
-            current = verifyNotNull(current.getParentStorage());
-        }
-        return current;
-    }
+        var current = storage;
+        while (true) {
+            if (current.getStorageType() == StorageType.STATEMENT_LOCAL) {
+                return current;
+            }
 
-    private static boolean isLocalOrGlobal(final StorageType type) {
-        return type == StorageType.STATEMENT_LOCAL || type == StorageType.GLOBAL;
+            final var parent = current.getParentStorage();
+            if (parent == null) {
+                // Implies StorageType.GLOBAL
+                return current;
+            }
+            current = parent;
+        }
     }
 }
\ No newline at end of file