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.Predicates;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.FluentIterable;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.LinkedList;
19 import java.util.NoSuchElementException;
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.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 abstract public class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
32 implements EffectiveStatement<A, D> {
34 private final StmtContext<A, D, ?> stmtCtx;
35 private final ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
36 private final StatementSource statementSource;
37 private final StatementDefinition statementDefinition;
38 private D declaredInstance;
40 private final A argument;
42 public EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
45 this.statementDefinition = ctx.getPublicDefinition();
46 this.argument = ctx.getStatementArgument();
47 this.statementSource = ctx.getStatementSource();
49 Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx
50 .declaredSubstatements();
51 Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx
52 .effectiveSubstatements();
54 Collection<StatementContextBase<?, ?, ?>> substatementsInit = new LinkedList<>();
56 for(StatementContextBase<?, ?, ?> declaredSubstatement : declaredSubstatements) {
57 if(declaredSubstatement.getPublicDefinition() == Rfc6020Mapping.USES) {
58 substatementsInit.add(declaredSubstatement);
59 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
60 ((StatementContextBase<?, ?, ?>)ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
61 .getEffectOfStatement());
63 substatementsInit.add(declaredSubstatement);
67 substatementsInit.addAll(effectiveSubstatements);
69 this.substatements = FluentIterable.from(substatementsInit).filter(StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE)
70 .transform(StmtContextUtils.buildEffective()).toList();
74 public StatementDefinition statementDefinition() {
75 return statementDefinition;
84 public StatementSource getStatementSource() {
85 return statementSource;
89 public D getDeclared() {
90 if (declaredInstance == null) {
91 declaredInstance = stmtCtx.buildDeclared();
93 return declaredInstance;
97 public <K, V, N extends IdentifierNamespace<K, V>> V get(
98 final Class<N> namespace, final K identifier) {
99 return stmtCtx.getFromNamespace(namespace, identifier);
103 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(
104 final Class<N> namespace) {
105 return stmtCtx.getAllFromNamespace(namespace);
109 public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
110 return substatements;
113 public StmtContext<A, D, ?> getStatementContext() {
117 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(
118 final Class<S> type) {
121 result = type.cast(Iterables.find(substatements,
122 Predicates.instanceOf(type)));
123 } catch (NoSuchElementException e) {
129 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
132 result = type.cast(Iterables.find(substatements,
133 Predicates.instanceOf(type)));
134 } catch (NoSuchElementException e) {
140 @SuppressWarnings("unchecked")
141 protected final <T> Collection<T> allSubstatementsOfType(
142 final Class<T> type) {
143 Collection<T> result = null;
146 result = Collection.class.cast(Collections2.filter(substatements,
147 Predicates.instanceOf(type)));
148 } catch (NoSuchElementException e) {
149 result = Collections.emptyList();
154 protected final <T> T firstSubstatementOfType(final Class<T> type) {
157 result = type.cast(Iterables.find(substatements,
158 Predicates.instanceOf(type)));
159 } catch (NoSuchElementException e) {
165 protected final <R> R firstSubstatementOfType(final Class<?> type,
166 final Class<R> returnType) {
169 result = returnType.cast(Iterables.find(
171 Predicates.and(Predicates.instanceOf(type),
172 Predicates.instanceOf(returnType))));
173 } catch (NoSuchElementException e) {