Refactor augment statement implementation
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / EffectiveStatementBase.java
index 668c7d6e730e62b5a29aff5855d6f1f59825542c..77ceab24e06213cb0908451722abb10e766ae8bc 100644 (file)
@@ -7,28 +7,32 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
 
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.annotations.Beta;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
 import java.util.function.Predicate;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 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.parser.spi.meta.StmtContext;
 
-public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
+/**
+ * Stateful version of {@link AbstractEffectiveStatement}, which holds substatements in an {@link ImmutableList}.
+ *
+ * @param <A> Argument type ({@link Void} if statement does not have argument.)
+ * @param <D> Class representing declared version of this statement.
+ */
+// TODO: This class is problematic in that it interacts with its subclasses via methods which are guaranteed to allows
+//       atrocities like RecursiveObjectLeaker tricks. That should be avoided and pushed to caller in a way where
+//       this class is a pure holder taking {@code ImmutableList<? extends EffectiveStatement<?, ?>>} in the
+//       constructor.
+//
+//       From memory efficiency perspective, it is very common to have effective statements without any substatements,
+//       in which case 'substatements' field is redundant.
+public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
+        extends AbstractEffectiveStatement<A, D> {
     private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
 
     /**
@@ -37,68 +41,7 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
      * @param ctx context of statement.
      */
     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
-        final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
-
-        /*
-         * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
-         * the statements were defined -- i.e. if we have something like this:
-         *
-         * container foo {
-         *   uses bar;
-         *   uses baz;
-         * }
-         *
-         * grouping baz {
-         *   leaf baz {
-         *     type string;
-         *   }
-         * }
-         *
-         * grouping bar {
-         *   leaf bar {
-         *     type string;
-         *   }
-         * }
-         *
-         * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
-         * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
-         *
-         * TODO: this really should be handled by UsesStatementSupport such that 'uses baz' would have a prerequisite
-         *       of a resolved 'uses bar'.
-         */
-        Set<StmtContext<?, ?, ?>> filteredStatements = null;
-        for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
-            if (declaredSubstatement.isSupportedByFeatures()) {
-                substatementsInit.add(declaredSubstatement);
-
-                final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
-                if (!effect.isEmpty()) {
-                    if (filteredStatements == null) {
-                        filteredStatements = new HashSet<>();
-                    }
-                    filteredStatements.addAll(effect);
-                    substatementsInit.addAll(effect);
-                }
-            }
-        }
-
-        if (filteredStatements != null) {
-            for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
-                if (!filteredStatements.contains(stmt)) {
-                    substatementsInit.add(stmt);
-                }
-            }
-        } else {
-            substatementsInit.addAll(ctx.effectiveSubstatements());
-        }
-
-        this.substatements = ImmutableList.copyOf(initSubstatements(ctx, substatementsInit));
-    }
-
-    @Beta
-    protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(final StmtContext<A, D, ?> ctx,
-            final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
-        return initSubstatements(substatementsInit);
+        this.substatements = ImmutableList.copyOf(initSubstatements(BaseStatementSupport.declaredSubstatements(ctx)));
     }
 
     /**
@@ -114,55 +57,32 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
             StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
     }
 
-    @Override
-    public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
-            final K identifier) {
-        return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
-    }
-
-    @Override
-    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
-        final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
-        return ret.isPresent() ? ret.get() : ImmutableMap.of();
-    }
-
-    /**
-     * Return the statement-specific contents of specified namespace, if available.
-     *
-     * @param namespace Requested namespace
-     * @return Namespace contents, if available.
-     */
-    @Beta
-    protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
-            final @NonNull Class<N> namespace) {
-        return Optional.empty();
-    }
-
     @Override
     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
         return substatements;
     }
 
-    protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
-        return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
-    }
-
     @SuppressWarnings("unchecked")
     public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
-        return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
+        return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
     }
 
     protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
-        return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
+        return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
     }
 
     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
-        return substatements.stream()
+        return effectiveSubstatements().stream()
                 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
                 .findFirst().map(returnType::cast).orElse(null);
     }
 
     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
-        return substatements.stream().filter(type::isInstance).findFirst().orElse(null);
+        return effectiveSubstatements().stream().filter(type::isInstance).findFirst().orElse(null);
+    }
+
+    // FIXME: rename to 'getFirstEffectiveStatement()'
+    protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
+        return findFirstEffectiveSubstatement(type).orElse(null);
     }
 }