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_SUPPORTED_TO_BUILD_EFFECTIVE =
32 new Predicate<StmtContext<?,?,?>>() {
34 public boolean apply(final StmtContext<?, ?, ?> input) {
35 return input.isSupportedToBuildEffective();
39 private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
40 new Predicate<StmtContext<?,?,?>>() {
42 public boolean apply(final StmtContext<?, ?, ?> input) {
43 return StmtContextUtils.isUnknownStatement(input);
47 private final List<? extends EffectiveStatement<?, ?>> substatements;
53 * context of statement.
55 protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
56 final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
57 final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
59 for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
60 if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
61 substatementsInit.add(declaredSubstatement);
62 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
63 ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
64 .getEffectOfStatement());
66 substatementsInit.add(declaredSubstatement);
69 substatementsInit.addAll(effectiveSubstatements);
71 this.substatements = ImmutableList.copyOf(Collections2.transform(Collections2.filter(substatementsInit,
72 IS_SUPPORTED_TO_BUILD_EFFECTIVE), StmtContextUtils.buildEffective()));
76 public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
77 throw new UnsupportedOperationException("Not implemented yet.");
81 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
82 throw new UnsupportedOperationException("Not implemented yet.");
86 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
90 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
91 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
92 Predicates.instanceOf(type));
93 return possible.isPresent() ? type.cast(possible.get()) : null;
96 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
97 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
98 Predicates.instanceOf(type));
99 return possible.isPresent() ? type.cast(possible.get()) : null;
102 @SuppressWarnings("unchecked")
103 protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
104 return Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
107 protected final <T> T firstSubstatementOfType(final Class<T> type) {
108 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
109 Predicates.instanceOf(type));
110 return possible.isPresent() ? type.cast(possible.get()) : null;
113 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
114 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
115 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
116 return possible.isPresent() ? returnType.cast(possible.get()) : null;
119 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
120 return Iterables.tryFind(substatements, Predicates.instanceOf(type)).orNull();