2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Predicate;
12 import com.google.common.base.Predicates;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Iterables;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
20 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
21 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
31 private static final Predicate<StmtContext<?, ?, ?>> IS_UNKNOWN_STATEMENT_CONTEXT =
32 StmtContextUtils::isUnknownStatement;
33 private static final Predicate<StmtContext<?, ?, ?>> IS_NOT_UNKNOWN_STATEMENT_CONTEXT =
34 Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT);
36 private final List<? extends EffectiveStatement<?, ?>> substatements;
37 private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
39 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
47 * context of statement.
48 * @param buildUnknownSubstatements
49 * if it is false, the unknown substatements are omitted from
50 * build of effective substatements till the call of either
51 * effectiveSubstatements or getOmittedUnknownSubstatements
52 * method. The main purpose of this is to allow the build of
53 * recursive extension definitions.
55 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, final boolean buildUnknownSubstatements) {
57 final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
58 final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
60 final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
61 ctx.declaredSubstatements(), StmtContextUtils::areFeaturesSupported);
62 for (final StatementContextBase<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
63 if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
64 substatementsInit.add(declaredSubstatement);
65 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
66 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
67 .getEffectOfStatement());
69 substatementsInit.add(declaredSubstatement);
72 substatementsInit.addAll(effectiveSubstatements);
74 Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
75 StmtContext::isSupportedToBuildEffective);
76 if (!buildUnknownSubstatements) {
77 this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
78 IS_UNKNOWN_STATEMENT_CONTEXT));
79 substatementsToBuild = Collections2.filter(substatementsToBuild, IS_NOT_UNKNOWN_STATEMENT_CONTEXT);
81 this.unknownSubstatementsToBuild = ImmutableList.of();
84 this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, StatementContextBase::buildEffective));
87 Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
88 return Collections2.transform(unknownSubstatementsToBuild, StatementContextBase::buildEffective);
92 public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
93 throw new UnsupportedOperationException("Not implemented yet.");
97 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
98 throw new UnsupportedOperationException("Not implemented yet.");
102 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
103 if (unknownSubstatementsToBuild.isEmpty()) {
104 return substatements;
106 return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
110 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
111 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
112 Predicates.instanceOf(type));
113 return possible.isPresent() ? type.cast(possible.get()) : null;
116 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
117 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
118 Predicates.instanceOf(type));
119 return possible.isPresent() ? type.cast(possible.get()) : null;
122 @SuppressWarnings("unchecked")
123 protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
124 return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
127 protected final <T> T firstSubstatementOfType(final Class<T> type) {
128 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
129 Predicates.instanceOf(type));
130 return possible.isPresent() ? type.cast(possible.get()) : null;
133 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
134 final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
135 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
136 return possible.isPresent() ? returnType.cast(possible.get()) : null;
139 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
140 return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();