-/**
+/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
+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.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
+import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
-import java.util.NoSuchElementException;
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;
-abstract public class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
+public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
- private final ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
- private final StatementSource statementSource;
- private final StatementDefinition statementDefinition;
- private final A argument;
- private final D declaredInstance;
+ 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);
+ }
+ };
- public EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
- this.statementDefinition = ctx.getPublicDefinition();
- this.argument = ctx.getStatementArgument();
- this.statementSource = ctx.getStatementSource();
+ private static final Predicate<StatementContextBase<?, ?, ?>> ARE_FEATURES_SUPPORTED = new Predicate<StatementContextBase<?, ?, ?>>() {
- Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx.declaredSubstatements();
- Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
+ @Override
+ public boolean apply(final StatementContextBase<?, ?, ?> input) {
+ return StmtContextUtils.areFeaturesSupported(input);
+ }
+ };
- Collection<StatementContextBase<?, ?, ?>> substatementsInit = new LinkedList<>();
+ private final List<? extends EffectiveStatement<?, ?>> substatements;
+ private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
- for(StatementContextBase<?, ?, ?> declaredSubstatement : declaredSubstatements) {
- if(declaredSubstatement.getPublicDefinition() == Rfc6020Mapping.USES) {
+ 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, final boolean buildUnknownSubstatements) {
+
+ final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
+ final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
+
+ final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
+ ctx.declaredSubstatements(), ARE_FEATURES_SUPPORTED);
+ 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 = FluentIterable.from(substatementsInit).filter(StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE)
- .transform(StmtContextUtils.buildEffective()).toList();
- declaredInstance = ctx.buildDeclared();
- }
-
- @Override
- public StatementDefinition statementDefinition() {
- return statementDefinition;
- }
-
- @Override
- public A argument() {
- return argument;
- }
+ 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();
+ }
- @Override
- public StatementSource getStatementSource() {
- return statementSource;
+ this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, StatementContextBase::buildEffective));
}
- @Override
- public D getDeclared() {
- return declaredInstance;
+ Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
+ return Collections2.transform(unknownSubstatementsToBuild, 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(final Class<N> namespace, 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(final Class<N> namespace) {
throw new UnsupportedOperationException("Not implemented yet.");
}
@Override
- public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
- return substatements;
+ public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
+ if (unknownSubstatementsToBuild.isEmpty()) {
+ return substatements;
+ } else {
+ return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
+ }
}
protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
- S result = null;
- try {
- result = type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
- } catch (NoSuchElementException e) {
- result = null;
- }
- return result;
+ 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) {
- S result = null;
- try {
- result = type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
- } catch (NoSuchElementException e) {
- result = null;
- }
- return result;
+ final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+ Predicates.instanceOf(type));
+ return possible.isPresent() ? type.cast(possible.get()) : null;
}
@SuppressWarnings("unchecked")
protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
- Collection<T> result = null;
-
- try {
- result = Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
- } catch (NoSuchElementException e) {
- result = Collections.emptyList();
- }
- return result;
+ return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
}
protected final <T> T firstSubstatementOfType(final Class<T> type) {
- T result = null;
- try {
- result = type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
- } catch (NoSuchElementException e) {
- result = null;
- }
- return result;
+ 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) {
- R result = null;
- try {
- result = returnType.cast(Iterables.find(substatements,
- Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType))));
- } catch (NoSuchElementException e) {
- result = null;
- }
- return result;
+ 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) {
+ return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();
}
}