import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.List;
-import java.util.Map;
import java.util.function.Predicate;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+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.YangStmtMapping;
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;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
-public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
- private final List<? extends EffectiveStatement<?, ?>> substatements;
+/**
+ * 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;
/**
* Constructor.
*
- * @param ctx
- * context of statement.
+ * @param ctx context of statement.
*/
protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
- final Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
- final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
-
- final Collection<? extends StmtContext<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
- ctx.declaredSubstatements(), StmtContext::isSupportedByFeatures);
- for (final StmtContext<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
- if (YangStmtMapping.USES == declaredSubstatement.getPublicDefinition()) {
- substatementsInit.add(declaredSubstatement);
- substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
- ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
- .getEffectOfStatement());
- } else {
- substatementsInit.add(declaredSubstatement);
- }
- }
- substatementsInit.addAll(effectiveSubstatements);
-
- this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
+ this.substatements = ImmutableList.copyOf(initSubstatements(BaseStatementSupport.declaredSubstatements(ctx)));
}
/**
StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
}
- @Override
- 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(@Nonnull final Class<N> namespace) {
- throw new UnsupportedOperationException("Not implemented yet.");
- }
-
- @Nonnull
@Override
public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
return substatements;
}
- /**
- * Find first substatement of specified type.
- *
- * @param type Requested type
- * @return First matching substatement, or null if no match is found.
- *
- * @deprecated Use {@link #findFirstEffectiveSubstatement(Class)} instead.
- */
- @Deprecated
- public final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
- return findFirstEffectiveSubstatement(type).orElse(null);
- }
-
- 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));
}
- @Nullable protected final <T> T firstSubstatementOfType(final Class<T> type) {
- return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
+ protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
+ 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);
}
}