*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
-import static java.util.Objects.requireNonNull;
-
import com.google.common.annotations.Beta;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.Map;
-import java.util.Optional;
import java.util.function.Predicate;
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> {
+/**
+ * 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;
/**
* @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(ctx, substatementsInit));
+ this.substatements = ImmutableList.copyOf(initSubstatements(ctx,
+ BaseStatementSupport.declaredSubstatements(ctx)));
}
@Beta
StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
}
- @Override
- public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
- final K identifier) {
- return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
- }
-
- @Override
- public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
- final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
- return ret.isPresent() ? ret.get() : ImmutableMap.of();
- }
-
- /**
- * Return the statement-specific contents of specified namespace, if available.
- *
- * @param namespace Requested namespace
- * @return Namespace contents, if available.
- */
- @Beta
- protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
- final @NonNull Class<N> namespace) {
- return Optional.empty();
- }
-
@Override
public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
return substatements;
}
- 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));
}
protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
- return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
+ 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);
}
}