X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Frfc6020%2Feffective%2FEffectiveStatementBase.java;h=4fabc2e4f14f957d0a9038be6870d9a1d59ca05d;hb=c24d6e2f39acbb11e22b5676bb7481ed52bec461;hp=eee68c622567404ac69ead02d9f89a17c8a7d5c4;hpb=e99bf7da4cf4f715e6d899a8c41a8df2853e3055;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java index eee68c6225..4fabc2e4f1 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java @@ -12,101 +12,87 @@ import com.google.common.base.Predicates; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import java.util.ArrayList; import java.util.Collection; -import java.util.LinkedList; 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> implements EffectiveStatement { - private final List> substatements; - private final StatementSource statementSource; - private final StatementDefinition statementDefinition; - private final A argument; - private final D declaredInstance; - - public EffectiveStatementBase(final StmtContext ctx) { - this.statementDefinition = ctx.getPublicDefinition(); - this.argument = ctx.getStatementArgument(); - this.statementSource = ctx.getStatementSource(); - - Collection> declaredSubstatements = ctx.declaredSubstatements(); - Collection> effectiveSubstatements = ctx.effectiveSubstatements(); - - Collection> substatementsInit = new LinkedList<>(); - for(StatementContextBase declaredSubstatement : declaredSubstatements) { + /** + * Constructor. + * + * @param ctx + * context of statement. + */ + protected EffectiveStatementBase(final StmtContext ctx) { + final Collection> effectiveSubstatements = ctx.effectiveSubstatements(); + final Collection> substatementsInit = new ArrayList<>(); + + final Collection> 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> initSubstatements( + final Collection> substatementsInit) { + return Collections2.transform(Collections2.filter(substatementsInit, + StmtContext::isSupportedToBuildEffective), StatementContextBase::buildEffective); } @Override - public > V get(final Class namespace, final K identifier) { + public final > V get(@Nonnull final Class namespace, @Nonnull final K identifier) { throw new UnsupportedOperationException("Not implemented yet."); } @Override - public > Map getAll(final Class namespace) { + public final > Map getAll(@Nonnull final Class namespace) { throw new UnsupportedOperationException("Not implemented yet."); } + @Nonnull @Override - public Collection> effectiveSubstatements() { + public final Collection> effectiveSubstatements() { return substatements; } protected final > S firstEffective(final Class type) { - Optional> possible = Iterables.tryFind(substatements, + final Optional> possible = Iterables.tryFind(substatements, Predicates.instanceOf(type)); return possible.isPresent() ? type.cast(possible.get()) : null; } protected final S firstSchemaNode(final Class type) { - Optional> possible = Iterables.tryFind(substatements, + final Optional> possible = Iterables.tryFind(substatements, Predicates.instanceOf(type)); return possible.isPresent() ? type.cast(possible.get()) : null; } @@ -117,20 +103,18 @@ public abstract class EffectiveStatementBase> } protected final T firstSubstatementOfType(final Class type) { - Optional> possible = Iterables.tryFind(substatements, + final Optional> possible = Iterables.tryFind(substatements, Predicates.instanceOf(type)); return possible.isPresent() ? type.cast(possible.get()) : null; } protected final R firstSubstatementOfType(final Class type, final Class returnType) { - Optional> possible = Iterables.tryFind(substatements, + final Optional> 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> possible = Iterables.tryFind(substatements, - Predicates.instanceOf(type)); - return possible.isPresent() ? possible.get() : null; + return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull(); } }