Introduce NamespaceStmtCtx 86/93986/11
authormiroslav.kovac <miroslav.kovac@pantheon.tech>
Tue, 1 Dec 2020 13:55:34 +0000 (14:55 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 3 Dec 2020 00:32:31 +0000 (01:32 +0100)
We have a few namespace access methods, make sure we encapsulate
them into a NamespaceStmtCtx and expose them from both
StmtContext and EffectiveStmtCtx.Current.

JIRA: YANGTOOLS-1185
Change-Id: I5c13406b96ab3735761749b900c244ad555f8730
Signed-off-by: miroslav.kovac <miroslav.kovac@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/BaseCurrentEffectiveStmtCtx.java
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/EffectiveStmtCtx.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/NamespaceStmtCtx.java [new file with mode: 0644]
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java

index f7bbc9c1ddc439cee81cf7d8be8c66e6cf1eff4c..cd8950a29405fce416e676ff04ba37c7ec941baa 100644 (file)
@@ -70,9 +70,19 @@ final class BaseCurrentEffectiveStmtCtx<A, D extends DeclaredStatement<A>> imple
     }
 
     @Override
-    public <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
-            final Class<@NonNull N> type, final T key) {
-        return delegate.getFromNamespace(type, key);
+    public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> localNamespace(final Class<@NonNull N> nsType) {
+        return delegate.localNamespace(nsType);
+    }
+
+    @Override
+    public <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> namespace(final Class<@NonNull N> nsType) {
+        return delegate.namespace(nsType);
+    }
+
+    @Override
+    public <K, V, T extends K, N extends IdentifierNamespace<K, V>> V namespaceItem(final Class<@NonNull N> type,
+            final T key) {
+        return delegate.namespaceItem(type, key);
     }
 
     @Override
@@ -116,12 +126,6 @@ final class BaseCurrentEffectiveStmtCtx<A, D extends DeclaredStatement<A>> imple
         return ret;
     }
 
-    @Override
-    public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V>
-            getAllFromCurrentStmtCtxNamespace(final Class<N> type) {
-        return delegate.getAllFromCurrentStmtCtxNamespace(type);
-    }
-
     @Override
     public A argument() {
         return delegate.argument();
index 2b2071052394c57da179dfd6f7e67b7638d0031f..1f4ba0355af8413940a69d862db369f4dd40e0ac 100644 (file)
@@ -240,19 +240,18 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     //
 
     @Override
-    public final <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
-            final Class<@NonNull N> type, final T key) {
+    public final <K, V, T extends K, N extends IdentifierNamespace<K, V>> V namespaceItem(final Class<@NonNull N> type,
+            final T key) {
         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
     }
 
     @Override
-    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
+    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> namespace(final Class<@NonNull N> type) {
         return getNamespace(type);
     }
 
     @Override
-    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
-            final Class<N> type) {
+    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> localNamespace(final Class<@NonNull N> type) {
         return getLocalNamespace(type);
     }
 
index 95791adf390c1c33ee7c67705a7dee9413da3b39..5e52a77f29810535d47315f7aa7e08243479b651 100644 (file)
@@ -11,7 +11,6 @@ import static com.google.common.base.Verify.verifyNotNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.VerifyException;
-import java.util.Map;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -19,7 +18,6 @@ import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 
 /**
  * Effective view of a {@link StmtContext} for the purposes of creating an {@link EffectiveStatement}.
@@ -79,17 +77,12 @@ public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immu
      * @param <D> Class representing declared version of this statement
      */
     @Beta
-    interface Current<A, D extends DeclaredStatement<A>> extends Parent, BoundStmtCtx<A> {
+    interface Current<A, D extends DeclaredStatement<A>> extends Parent, BoundStmtCtx<A>, NamespaceStmtCtx {
 
         @NonNull CommonStmtCtx root();
 
         @NonNull D declared();
 
-        <K, V, T extends K, N extends IdentifierNamespace<K, V>> @Nullable V getFromNamespace(Class<@NonNull N> type,
-            T key);
-
-        <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
-
         @Nullable EffectiveStatement<?, ?> original();
 
         /**
diff --git a/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/NamespaceStmtCtx.java b/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/NamespaceStmtCtx.java
new file mode 100644 (file)
index 0000000..dcfb541
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.parser.spi.meta;
+
+import com.google.common.annotations.Beta;
+import java.util.Map;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
+
+/**
+ * Support work with namespace content.
+ */
+@Beta
+public interface NamespaceStmtCtx extends CommonStmtCtx {
+    /**
+     * Return the selected namespace.
+     *
+     * @param <K> namespace key type
+     * @param <V> namespace value type
+     * @param <N> namespace type
+     * @param nsType namespace type class
+     * @return Namespace contents, if available
+     */
+    <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> namespace(Class<@NonNull N> nsType);
+
+    /**
+     * Return a value associated with specified key within a namespace.
+     *
+     * @param nsType Namespace type
+     * @param key Key
+     * @param <K> namespace key type
+     * @param <V> namespace value type
+     * @param <N> namespace type
+     * @param <T> key type
+     * @return Value, or null if there is no element
+     * @throws NamespaceNotAvailableException when the namespace is not available.
+     */
+    <K, V, T extends K, N extends IdentifierNamespace<K, V>> @Nullable V namespaceItem(Class<@NonNull N> nsType, T key);
+
+    <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> localNamespace(Class<@NonNull N> nsType);
+
+    // TODO: migrate users away
+    default <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
+            final Class<@NonNull N> nsType) {
+        return localNamespace(nsType);
+    }
+
+    /**
+     * Return the selected namespace.
+     *
+     * @param <K> namespace key type
+     * @param <V> namespace value type
+     * @param <N> namespace type
+     * @param nsType namespace type class
+     * @return Namespace contents, if available
+     */
+    // TODO: migrate users away
+    default <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> nsType) {
+        return namespace(nsType);
+    }
+
+    /**
+     * Return a value associated with specified key within a namespace.
+     *
+     * @param type Namespace type
+     * @param key Key
+     * @param <K> namespace key type
+     * @param <V> namespace value type
+     * @param <N> namespace type
+     * @param <T> key type
+     * @return Value, or null if there is no element
+     * @throws NamespaceNotAvailableException when the namespace is not available.
+     */
+    // TODO: migrate users away
+    default <K, V, T extends K, N extends IdentifierNamespace<K, V>>
+            @Nullable V getFromNamespace(final Class<@NonNull N> type, final T key) {
+        return namespaceItem(type, key);
+    }
+}
index 15fb8fb6888be08d272e047533390c8a0df03382..f8a537394ed2f886514390f319a2524a6bff8863 100644 (file)
@@ -14,7 +14,6 @@ import com.google.common.base.VerifyException;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Streams;
 import java.util.Collection;
-import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNull;
@@ -37,7 +36,7 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReferenc
  * @param <E> Effective Statement representation
  */
 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
-        extends BoundStmtCtx<A>, StmtContextCompat {
+        extends BoundStmtCtx<A>, NamespaceStmtCtx, StmtContextCompat {
     @Deprecated(forRemoval = true)
     default @NonNull StatementDefinition getPublicDefinition() {
         return publicDefinition();
@@ -120,25 +119,6 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     boolean isEnabledSemanticVersioning();
 
-    /**
-     * Return a value associated with specified key within a namespace.
-     *
-     * @param type Namespace type
-     * @param key Key
-     * @param <K> namespace key type
-     * @param <V> namespace value type
-     * @param <N> namespace type
-     * @param <T> key type
-     * @return Value, or null if there is no element
-     * @throws NamespaceNotAvailableException when the namespace is not available.
-     */
-    <K, V, T extends K, N extends IdentifierNamespace<K, V>> @Nullable V getFromNamespace(Class<@NonNull N> type,
-            T key);
-
-    <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
-
-    <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
-
     /**
      * Returns the model root for this statement.
      *