Reuse NamespaceAccess where possible 18/105318/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 7 Apr 2023 15:20:47 +0000 (17:20 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 7 Apr 2023 15:42:19 +0000 (17:42 +0200)
We have a few places where we can end up looking up NamespaceAccess for
a second time. Clean those up and also improve NamespaceAccess to expose
the underlying ParserNamespace.

Change-Id: I43e582a3c6db359d1f81819ac0445ac8b33e85bc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/BehaviourNamespaceAccess.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/BuildGlobalContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/NamespaceAccess.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java

index 5028df6c70cd4234140fb5438c5783bc047bf88b..c918946a96c1db46267a13bafb75c44ee4a944b7 100644 (file)
@@ -19,6 +19,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage.GlobalStorage;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 
 /**
  * A {@link NamespaceAccess} backed by a {@link NamespaceBehaviour}. Also holds reference to {@link BuildGlobalContext}.
@@ -36,6 +37,11 @@ final class BehaviourNamespaceAccess<K, V> extends NamespaceAccess<K, V> {
         this.behaviour = requireNonNull(behaviour);
     }
 
+    @Override
+    ParserNamespace<K, V> namespace() {
+        return behaviour.namespace();
+    }
+
     @Override
     V valueFrom(final NamespaceStorage storage, final K key) {
         return behaviour.getFrom(globalStorage, storage, key);
index 02d55eb5cc7b9ccd4d76e6da0b007de34e73da4f..ef7c5422878485416b03b404c5fb1ffc0cf9906c 100644 (file)
@@ -84,9 +84,9 @@ final class BuildGlobalContext extends AbstractNamespaceStorage implements Globa
             final ImmutableMap<ValidationBundleType, Collection<?>> supportedValidation) {
         this.supports = requireNonNull(supports, "BuildGlobalContext#supports cannot be null");
 
-        final var behavior = accessNamespace(ValidationBundles.NAMESPACE);
+        final var access = accessNamespace(ValidationBundles.NAMESPACE);
         for (var validationBundle : supportedValidation.entrySet()) {
-            behavior.valueTo(this, validationBundle.getKey(), validationBundle.getValue());
+            access.valueTo(this, validationBundle.getKey(), validationBundle.getValue());
         }
 
         supportedVersions = ImmutableSet.copyOf(
index b55ef40485d6c4470b53c4a4bff23315416d3687..9d257f81c184dd6aacbff6bdfb0da26b6504f773 100644 (file)
@@ -15,6 +15,7 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 
 abstract class NamespaceAccess<K, V> {
     abstract static class KeyedValueAddedListener<K> {
@@ -40,6 +41,8 @@ abstract class NamespaceAccess<K, V> {
         boolean onValueAdded(@NonNull K key, @NonNull V value);
     }
 
+    abstract @NonNull ParserNamespace<K, V> namespace();
+
     abstract @Nullable V valueFrom(@NonNull NamespaceStorage storage, K key);
 
     abstract void valueTo(@NonNull NamespaceStorage storage, K key, V value);
index e11a54addf3b1b08f0627620613cebbe70e31846..30918d4e761b4cc68cfbf47edf989cbe7d67e6a1 100644 (file)
@@ -66,6 +66,11 @@ final class SourceSpecificContext implements NamespaceStorage, Mutable {
             this.statementDefinitions = requireNonNull(statementDefinitions);
         }
 
+        @Override
+        ParserNamespace<QName, StatementSupport<?, ?, ?>> namespace() {
+            return StatementSupport.NAMESPACE;
+        }
+
         @Override
         StatementSupport<?, ?, ?> valueFrom(final NamespaceStorage storage, final QName key) {
             return statementDefinitions.getSupport(key);
index ae3438d67d61da0c390e440856527e1906b04374..b87e59d5fac57592cd0b5400ef7c6e87ad4ae428 100644 (file)
@@ -256,7 +256,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     // FIXME: this should be propagated through a correct constructor
     @Deprecated
     final void setCompletedPhase(final ModelProcessingPhase completedPhase) {
-        this.executionOrder = completedPhase.executionOrder();
+        executionOrder = completedPhase.executionOrder();
     }
 
     @Override
@@ -590,62 +590,57 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         return definition;
     }
 
-    final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> type, final K key,
+    final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> namespace, final K key,
             final OnNamespaceItemAdded listener) {
-        final Object potential = namespaceItem(type, key);
+        final var access = accessNamespace(namespace);
+        final var potential = access.valueFrom(this, key);
         if (potential != null) {
-            LOG.trace("Listener on {} key {} satisfied immediately", type, key);
-            listener.namespaceItemAdded(this, type, key, potential);
+            LOG.trace("Listener on {} key {} satisfied immediately", namespace, key);
+            listener.namespaceItemAdded(this, namespace, key, potential);
             return;
         }
 
-        accessNamespace(type).addListener(new KeyedValueAddedListener<>(this, key) {
+        access.addListener(new KeyedValueAddedListener<>(this, key) {
             @Override
             void onValueAdded(final Object value) {
-                listener.namespaceItemAdded(StatementContextBase.this, type, key, value);
+                listener.namespaceItemAdded(StatementContextBase.this, namespace, key, value);
             }
         });
     }
 
-    final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> type,
+    final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> namespace,
             final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion,
             final OnNamespaceItemAdded listener) {
-        final var namespaceAccess = accessNamespace(type);
-        final var entry = namespaceAccess.entryFrom(this, criterion);
+        final var access = accessNamespace(namespace);
+        final var entry = access.entryFrom(this, criterion);
         if (entry != null) {
-            LOG.debug("Listener on {} criterion {} found a pre-existing match: {}", type, criterion, entry);
-            waitForPhase(entry.getValue(), type, phase, criterion, listener);
+            LOG.debug("Listener on {} criterion {} found a pre-existing match: {}", namespace, criterion, entry);
+            waitForPhase(entry.getValue(), access, phase, criterion, listener);
             return;
         }
 
-        namespaceAccess.addListener((key, value) -> {
+        access.addListener((key, value) -> {
             if (criterion.match(key)) {
-                LOG.debug("Listener on {} criterion {} matched added key {}", type, criterion, key);
-                waitForPhase(value, type, phase, criterion, listener);
+                LOG.debug("Listener on {} criterion {} matched added key {}", namespace, criterion, key);
+                waitForPhase(value, access, phase, criterion, listener);
                 return true;
             }
             return false;
         });
     }
 
-    final <K, V> void selectMatch(final ParserNamespace<K, V> type, final NamespaceKeyCriterion<K> criterion,
-            final OnNamespaceItemAdded listener) {
-        final var match = accessNamespace(type).entryFrom(this, criterion);
-        if (match == null) {
-            throw new IllegalStateException(
-                "Failed to find a match for criterion %s in namespace %s node %s".formatted(criterion, type, this));
-        }
-        listener.namespaceItemAdded(StatementContextBase.this, type, match.getKey(), match.getValue());
-    }
-
-    final <K, V> void waitForPhase(final Object value, final ParserNamespace<K, V> type,
+    private <K, V> void waitForPhase(final Object value, final NamespaceAccess<K, V> access,
             final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion,
             final OnNamespaceItemAdded listener) {
-        ((StatementContextBase<?, ? ,?>) value).addPhaseCompletedListener(phase,
-            (context, phaseCompleted) -> {
-                selectMatch(type, criterion, listener);
-                return true;
-            });
+        ((StatementContextBase<?, ?, ?>) value).addPhaseCompletedListener(phase, (context, phaseCompleted) -> {
+            final var match = access.entryFrom(this, criterion);
+            if (match == null) {
+                throw new IllegalStateException("Failed to find a match for criterion %s in namespace %s node %s"
+                    .formatted(criterion, access.namespace(), this));
+            }
+            listener.namespaceItemAdded(this, access.namespace(), match.getKey(), match.getValue());
+            return true;
+        });
     }
 
     private static <T> Multimap<ModelProcessingPhase, T> newMultimap() {