import java.util.Collection;
import java.util.List;
import java.util.Map;
+import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
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.model.api.meta.StatementDefinition;
-import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
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 final List<? extends EffectiveStatement<?, ?>> substatements;
- private final StatementSource statementSource;
- private final StatementDefinition statementDefinition;
- private final A argument;
- private final D declaredInstance;
-
- public EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
- this.statementDefinition = ctx.getPublicDefinition();
- this.argument = ctx.getStatementArgument();
- this.statementSource = ctx.getStatementSource();
-
- Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
-
- Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
- for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
+ /**
+ * Constructor.
+ *
+ * @param ctx
+ * context of statement.
+ */
+ protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
+ final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
+ final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
+
+ final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
+ ctx.declaredSubstatements(), StmtContextUtils::areFeaturesSupported);
+ for (final StatementContextBase<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
substatementsInit.add(declaredSubstatement);
substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
- ((StatementContextBase<?, ?, ?>)ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
+ ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
.getEffectOfStatement());
} else {
substatementsInit.add(declaredSubstatement);
}
}
-
substatementsInit.addAll(effectiveSubstatements);
- this.substatements = ImmutableList.copyOf(Collections2.transform(
- Collections2.filter(substatementsInit, StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE),
- StmtContextUtils.buildEffective()));
- declaredInstance = ctx.buildDeclared();
- }
-
- @Override
- public StatementDefinition statementDefinition() {
- return statementDefinition;
- }
-
- @Override
- public A argument() {
- return argument;
- }
-
- @Override
- public StatementSource getStatementSource() {
- return statementSource;
+ this.substatements = ImmutableList.copyOf(initSubstatements(substatementsInit));
}
- @Override
- public D getDeclared() {
- return declaredInstance;
+ /**
+ * 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 <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 <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 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
+ public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
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;
}
}
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;
}
protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
- Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
- Predicates.instanceOf(type));
- return possible.isPresent() ? possible.get() : null;
+ return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
}
}