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.Predicates;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.FluentIterable;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Iterables;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.LinkedList;
21 import java.util.NoSuchElementException;
23 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
34 abstract public class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
36 private final ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
37 private final StatementSource statementSource;
38 private final StatementDefinition statementDefinition;
39 private final A argument;
40 private final D declaredInstance;
42 public EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
43 this.statementDefinition = ctx.getPublicDefinition();
44 this.argument = ctx.getStatementArgument();
45 this.statementSource = ctx.getStatementSource();
47 Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx.declaredSubstatements();
48 Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
50 Collection<StatementContextBase<?, ?, ?>> substatementsInit = new LinkedList<>();
52 for(StatementContextBase<?, ?, ?> declaredSubstatement : declaredSubstatements) {
53 if(declaredSubstatement.getPublicDefinition() == Rfc6020Mapping.USES) {
54 substatementsInit.add(declaredSubstatement);
55 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
56 ((StatementContextBase<?, ?, ?>)ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
57 .getEffectOfStatement());
59 substatementsInit.add(declaredSubstatement);
63 substatementsInit.addAll(effectiveSubstatements);
65 this.substatements = FluentIterable.from(substatementsInit).filter(StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE)
66 .transform(StmtContextUtils.buildEffective()).toList();
67 declaredInstance = ctx.buildDeclared();
71 public StatementDefinition statementDefinition() {
72 return statementDefinition;
81 public StatementSource getStatementSource() {
82 return statementSource;
86 public D getDeclared() {
87 return declaredInstance;
91 public <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
92 throw new UnsupportedOperationException("Not implemented yet.");
96 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
97 throw new UnsupportedOperationException("Not implemented yet.");
101 public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
102 return substatements;
105 protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
106 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
107 Predicates.instanceOf(type));
108 return possible.isPresent() ? type.cast(possible.get()) : null;
111 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
112 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
113 Predicates.instanceOf(type));
114 return possible.isPresent() ? type.cast(possible.get()) : null;
117 @SuppressWarnings("unchecked")
118 protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
119 Collection<T> result = null;
122 result = Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
123 } catch (NoSuchElementException e) {
124 result = Collections.emptyList();
129 protected final <T> T firstSubstatementOfType(final Class<T> type) {
130 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
131 Predicates.instanceOf(type));
132 return possible.isPresent() ? type.cast(possible.get()) : null;
135 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
136 Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
137 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
138 return possible.isPresent() ? returnType.cast(possible.get()) : null;