Move dataTree/schemaTree to AbstractEffectiveModule 49/87449/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Feb 2020 11:49:41 +0000 (12:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Feb 2020 12:51:39 +0000 (13:51 +0100)
There are no subclasses of AbstractSchemaEffectiveDocumentedNode
which would take advantage of the functionality here except
AbstractEffectiveModule. Remove the fields and utilites and tailor
them to the needs of AbstractEffectiveModule.

JIRA: YANGTOOLS-1065
Change-Id: I6b8be83295b30b307a0cb4ebaa994cecc0947a26
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractEffectiveModule.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractSchemaEffectiveDocumentedNode.java

index 8eeb12bf3f5ab92d25ded239f91892bc3dcf1b8b..cb977f527db2ec0c196e76807aeca6ed602d88c4 100644 (file)
@@ -13,6 +13,7 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.ImmutableSet;
 import java.net.URI;
@@ -20,6 +21,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
@@ -43,12 +45,15 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
 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;
 import org.opendaylight.yangtools.yang.model.api.stmt.ContactEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.OrganizationEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.compat.NotificationNodeContainerCompat;
@@ -79,12 +84,23 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
     private final ImmutableSet<DataSchemaNode> publicChildNodes;
     private final SemVer semanticVersion;
+    private final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace;
 
     protected AbstractEffectiveModule(
             final @NonNull StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx,
             final @NonNull String prefix) {
         super(ctx);
 
+        // This check is rather weird, but comes from our desire to lower memory footprint while providing both
+        // EffectiveStatements and SchemaNode interfaces -- which do not overlap completely where child lookups are
+        // concerned. This ensures that we have SchemaTree index available for use with child lookups.
+        final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTree =
+                createSchemaTreeNamespace(ctx.getStatementSourceReference(), effectiveSubstatements());
+        schemaTreeNamespace = ImmutableMap.copyOf(schemaTree);
+
+        // Data tree check, not currently used
+        createDataTreeNamespace(ctx.getStatementSourceReference(), schemaTree.values(), schemaTreeNamespace);
+
         this.name = argument();
         this.prefix = requireNonNull(prefix);
         this.yangVersion = findFirstEffectiveSubstatementArgument(YangVersionEffectiveStatement.class)
@@ -261,7 +277,8 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
     @Override
     @SuppressWarnings("checkstyle:hiddenField")
     public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
-        return findDataSchemaNode(name);
+        final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace.get(requireNonNull(name));
+        return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
     }
 
     @Override
@@ -274,6 +291,16 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String
         return Optional.ofNullable(semanticVersion);
     }
 
+    @Override
+    @SuppressWarnings("unchecked")
+    protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
+            final Class<N> namespace) {
+        if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
+            return Optional.of((Map<K, V>) schemaTreeNamespace);
+        }
+        return super.getNamespaceContents(namespace);
+    }
+
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this).omitNullValues()
index 11354c51288940ce689c99bc881d66743fbc5c5d..33c6a21ffa9439b607c6467dbc89ebabde5fe085 100644 (file)
@@ -7,27 +7,17 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
 
-import static com.google.common.base.Verify.verify;
-import static java.util.Objects.requireNonNull;
-
 import com.google.common.annotations.Beta;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import java.lang.invoke.VarHandle;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 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;
 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
@@ -45,60 +35,8 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReferenc
 @Beta
 public abstract class AbstractSchemaEffectiveDocumentedNode<A, D extends DeclaredStatement<A>>
         extends AbstractEffectiveDocumentedNode<A, D> {
-    private final ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace;
-    private final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace;
-
     protected AbstractSchemaEffectiveDocumentedNode(final StmtContext<A, D, ?> ctx) {
         super(ctx);
-
-        // This check is rather weird, but comes from our desire to lower memory footprint while providing both
-        // EffectiveStatements and SchemaNode interfaces -- which do not overlap completely where child lookups are
-        // concerned. This ensures that we have SchemaTree index available for use with child lookups.
-        final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
-        if (this instanceof SchemaTreeAwareEffectiveStatement || this instanceof DataNodeContainer) {
-            schemaTree = createSchemaTreeNamespace(ctx.getStatementSourceReference(),
-                effectiveSubstatements());
-        } else {
-            schemaTree = ImmutableMap.of();
-        }
-        schemaTreeNamespace = ImmutableMap.copyOf(schemaTree);
-
-        if (this instanceof DataTreeAwareEffectiveStatement && !schemaTree.isEmpty()) {
-            dataTreeNamespace = createDataTreeNamespace(ctx.getStatementSourceReference(), schemaTree.values(),
-                schemaTreeNamespace);
-        } else {
-            dataTreeNamespace = ImmutableMap.of();
-        }
-    }
-
-    @Override
-    @SuppressWarnings("unchecked")
-    protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
-            final Class<N> namespace) {
-        if (this instanceof SchemaTreeAwareEffectiveStatement
-                && SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
-            return Optional.of((Map<K, V>) schemaTreeNamespace);
-        }
-        if (this instanceof DataTreeAwareEffectiveStatement
-                && DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
-            return Optional.of((Map<K, V>) dataTreeNamespace);
-        }
-        return super.getNamespaceContents(namespace);
-    }
-
-    protected final <T> @NonNull ImmutableSet<T> derivedSet(final VarHandle vh, final @NonNull Class<T> clazz) {
-        final ImmutableSet<T> existing = (ImmutableSet<T>) vh.getAcquire(this);
-        return existing != null ? existing : loadSet(vh, clazz);
-    }
-
-    /**
-     * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
-     */
-    protected final Optional<DataSchemaNode> findDataSchemaNode(final QName name) {
-        // Only DataNodeContainer subclasses should be calling this method
-        verify(this instanceof DataNodeContainer);
-        final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace.get(requireNonNull(name));
-        return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
     }
 
     static @NonNull Map<QName, SchemaTreeEffectiveStatement<?>> createSchemaTreeNamespace(
@@ -131,13 +69,6 @@ public abstract class AbstractSchemaEffectiveDocumentedNode<A, D extends Declare
         return sameAsSchema ? (ImmutableMap) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
     }
 
-    @SuppressWarnings("unchecked")
-    private <T> @NonNull ImmutableSet<T> loadSet(final VarHandle vh, final @NonNull Class<T> clazz) {
-        final ImmutableSet<T> computed = ImmutableSet.copyOf(allSubstatementsOfType(clazz));
-        final Object witness = vh.compareAndExchangeRelease(this, null, computed);
-        return witness == null ? computed : (ImmutableSet<T>) witness;
-    }
-
     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map,
             final T child, final StatementSourceReference ref, final String tree) {
         final QName id = child.getIdentifier();