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.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
30 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
32 private static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
33 new Predicate<StmtContext<?,?,?>>() {
35 public boolean apply(final StmtContext<?, ?, ?> input) {
36 return input.isSupportedToBuildEffective();
40 private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
41 new Predicate<StmtContext<?,?,?>>() {
43 public boolean apply(final StmtContext<?, ?, ?> input) {
44 return StmtContextUtils.isUnknownStatement(input);
48 private static final Predicate<StmtContext<?, ?, ?>> ARE_FEATURES_SUPPORTED =
49 new Predicate<StmtContext<?, ?, ?>>() {
52 public boolean apply(StmtContext<?, ?, ?> input) {
53 return StmtContextUtils.areFeaturesSupported(input);
57 private final List<? extends EffectiveStatement<?, ?>> substatements;
58 private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
60 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
68 * context of statement.
69 * @param buildUnknownSubstatements
70 * if it is false, the unknown substatements are omitted from
71 * build of effective substatements till the call of either
72 * effectiveSubstatements or getOmittedUnknownSubstatements
73 * method. The main purpose of this is to allow the build of
74 * recursive extension definitions.
76 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
78 final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
79 final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
81 for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
82 if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
83 substatementsInit.add(declaredSubstatement);
84 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
85 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
86 .getEffectOfStatement());
88 substatementsInit.add(declaredSubstatement);
91 substatementsInit.addAll(effectiveSubstatements);
93 Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
94 IS_SUPPORTED_TO_BUILD_EFFECTIVE);
95 if (!buildUnknownSubstatements) {
96 this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
97 IS_UNKNOWN_STATEMENT_CONTEXT));
98 substatementsToBuild = Collections2.filter(substatementsToBuild,
99 Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
101 this.unknownSubstatementsToBuild = ImmutableList.of();
104 substatementsToBuild = Collections2.filter(substatementsToBuild, ARE_FEATURES_SUPPORTED);
106 Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
107 this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
110 Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
111 Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
112 return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
116 public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
117 throw new UnsupportedOperationException("Not implemented yet.");
121 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
122 throw new UnsupportedOperationException("Not implemented yet.");
126 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
127 if (unknownSubstatementsToBuild.isEmpty()) {
128 return substatements;
130 return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
134 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
135 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
136 Predicates.instanceOf(type));
137 return possible.isPresent() ? type.cast(possible.get()) : null;
140 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
141 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
142 Predicates.instanceOf(type));
143 return possible.isPresent() ? type.cast(possible.get()) : null;
146 @SuppressWarnings("unchecked")
147 protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
148 return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
151 protected final <T> T firstSubstatementOfType(final Class<T> type) {
152 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
153 Predicates.instanceOf(type));
154 return possible.isPresent() ? type.cast(possible.get()) : null;
157 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
158 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
159 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
160 return possible.isPresent() ? returnType.cast(possible.get()) : null;
163 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
164 return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();