Bug 6867: Rename of Rfc6020Mapping to YangStmtMapping
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveStatementBase.java
index 15888ae5906beac36a4f6a37fff79c854c09b1b7..7307011f70e174ee9ee46c46aa263368cb044c13 100644 (file)
@@ -7,9 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
@@ -18,7 +16,8 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
-import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 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;
@@ -28,58 +27,22 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 
 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
-
-    private static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
-            new Predicate<StmtContext<?,?,?>>() {
-        @Override
-        public boolean apply(final StmtContext<?, ?, ?> input) {
-            return input.isSupportedToBuildEffective();
-        }
-    };
-
-    private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
-            new Predicate<StmtContext<?,?,?>>() {
-        @Override
-        public boolean apply(final StmtContext<?, ?, ?> input) {
-            return StmtContextUtils.isUnknownStatement(input);
-        }
-    };
-
-    private static final Predicate<StmtContext<?, ?, ?>> ARE_FEATURES_SUPPORTED =
-            new Predicate<StmtContext<?, ?, ?>>() {
-
-                @Override
-                public boolean apply(StmtContext<?, ?, ?> input) {
-                    return StmtContextUtils.areFeaturesSupported(input);
-                }
-            };
-
     private final List<? extends EffectiveStatement<?, ?>> substatements;
-    private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
-
-    protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
-        this(ctx, true);
-    }
 
     /**
      * Constructor.
      *
      * @param ctx
      *            context of statement.
-     * @param buildUnknownSubstatements
-     *            if it is false, the unknown substatements are omitted from
-     *            build of effective substatements till the call of either
-     *            effectiveSubstatements or getOmittedUnknownSubstatements
-     *            method. The main purpose of this is to allow the build of
-     *            recursive extension definitions.
      */
-    protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
-
+    protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
         final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
 
-        for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
-            if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
+        final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
+                ctx.declaredSubstatements(), StmtContextUtils::areFeaturesSupported);
+        for (final StatementContextBase<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
+            if (declaredSubstatement.getPublicDefinition().equals(YangStmtMapping.USES)) {
                 substatementsInit.add(declaredSubstatement);
                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
                 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
@@ -90,55 +53,46 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
         }
         substatementsInit.addAll(effectiveSubstatements);
 
-        Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
-                IS_SUPPORTED_TO_BUILD_EFFECTIVE);
-        if (!buildUnknownSubstatements) {
-            this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
-                    IS_UNKNOWN_STATEMENT_CONTEXT));
-            substatementsToBuild = Collections2.filter(substatementsToBuild,
-                    Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
-        } else {
-            this.unknownSubstatementsToBuild = ImmutableList.of();
-        }
-
-        substatementsToBuild = Collections2.filter(substatementsToBuild, ARE_FEATURES_SUPPORTED);
-
-        Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
-        this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
+        this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
     }
 
-    Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
-        Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
-        return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
+    /**
+     * Create a set of substatements. This method is split out so it can be overridden in
+     * {@link ExtensionEffectiveStatementImpl} to leak a not-fully-initialized instance.
+     *
+     * @param substatementsInit proposed substatements
+     * @return Filtered substatements
+     */
+    Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
+            final Collection<StatementContextBase<?, ?, ?>> substatementsInit) {
+        return Collections2.transform(Collections2.filter(substatementsInit,
+            StmtContext::isSupportedToBuildEffective), StatementContextBase::buildEffective);
     }
 
     @Override
-    public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
+    public final <K, V, N extends IdentifierNamespace<K, V>> V get(@Nonnull final Class<N> namespace, @Nonnull final K identifier) {
         throw new UnsupportedOperationException("Not implemented yet.");
     }
 
     @Override
-    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
+    public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull final Class<N> namespace) {
         throw new UnsupportedOperationException("Not implemented yet.");
     }
 
+    @Nonnull
     @Override
     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
-        if (unknownSubstatementsToBuild.isEmpty()) {
-            return substatements;
-        } else {
-            return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
-        }
+        return substatements;
     }
 
     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.instanceOf(type));
         return possible.isPresent() ? type.cast(possible.get()) : null;
     }
 
     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.instanceOf(type));
         return possible.isPresent() ? type.cast(possible.get()) : null;
     }
@@ -149,13 +103,13 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
     }
 
     protected final <T> T firstSubstatementOfType(final Class<T> type) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.instanceOf(type));
         return possible.isPresent() ? type.cast(possible.get()) : null;
     }
 
     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
         return possible.isPresent() ? returnType.cast(possible.get()) : null;
     }