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 final List<? extends EffectiveStatement<?, ?>> substatements;
49 private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
51 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
59 * context of statement.
60 * @param buildUnknownSubstatements
61 * if it is false, the unknown substatements are omitted from
62 * build of effective substatements till the call of either
63 * effectiveSubstatements or getOmittedUnknownSubstatements
64 * method. The main purpose of this is to allow the build of
65 * recursive extension definitions.
67 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
69 final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
70 final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
72 for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
73 if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
74 substatementsInit.add(declaredSubstatement);
75 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
76 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
77 .getEffectOfStatement());
79 substatementsInit.add(declaredSubstatement);
82 substatementsInit.addAll(effectiveSubstatements);
84 Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
85 IS_SUPPORTED_TO_BUILD_EFFECTIVE);
86 if (!buildUnknownSubstatements) {
87 this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
88 IS_UNKNOWN_STATEMENT_CONTEXT));
89 substatementsToBuild = Collections2.filter(substatementsToBuild,
90 Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
92 this.unknownSubstatementsToBuild = ImmutableList.of();
95 Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
96 this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
99 Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
100 Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
101 return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
105 public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
106 throw new UnsupportedOperationException("Not implemented yet.");
110 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
111 throw new UnsupportedOperationException("Not implemented yet.");
115 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
116 if (unknownSubstatementsToBuild.isEmpty()) {
117 return substatements;
119 return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
123 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
124 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
125 Predicates.instanceOf(type));
126 return possible.isPresent() ? type.cast(possible.get()) : null;
129 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
130 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
131 Predicates.instanceOf(type));
132 return possible.isPresent() ? type.cast(possible.get()) : null;
135 @SuppressWarnings("unchecked")
136 protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
137 return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
140 protected final <T> T firstSubstatementOfType(final Class<T> type) {
141 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
142 Predicates.instanceOf(type));
143 return possible.isPresent() ? type.cast(possible.get()) : null;
146 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
147 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
148 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
149 return possible.isPresent() ? returnType.cast(possible.get()) : null;
152 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
153 return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();