d3b190ce4a7a5e3c1c2b49427b300a041f2e22a0
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveStatementBase.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
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;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.LinkedList;
20 import java.util.Map;
21 import java.util.NoSuchElementException;
22
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;
33
34 abstract public class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
35
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;
41
42     public EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
43         this.statementDefinition = ctx.getPublicDefinition();
44         this.argument = ctx.getStatementArgument();
45         this.statementSource = ctx.getStatementSource();
46
47         Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx.declaredSubstatements();
48         Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
49
50         Collection<StatementContextBase<?, ?, ?>> substatementsInit = new LinkedList<>();
51
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());
58             } else {
59                 substatementsInit.add(declaredSubstatement);
60             }
61         }
62
63         substatementsInit.addAll(effectiveSubstatements);
64
65         this.substatements = FluentIterable.from(substatementsInit).filter(StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE)
66                 .transform(StmtContextUtils.buildEffective()).toList();
67         declaredInstance = ctx.buildDeclared();
68     }
69
70     @Override
71     public StatementDefinition statementDefinition() {
72         return statementDefinition;
73     }
74
75     @Override
76     public A argument() {
77         return argument;
78     }
79
80     @Override
81     public StatementSource getStatementSource() {
82         return statementSource;
83     }
84
85     @Override
86     public D getDeclared() {
87         return declaredInstance;
88     }
89
90     @Override
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.");
93     }
94
95     @Override
96     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
97         throw new UnsupportedOperationException("Not implemented yet.");
98     }
99
100     @Override
101     public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
102         return substatements;
103     }
104
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;
109     }
110
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;
115     }
116
117     @SuppressWarnings("unchecked")
118     protected final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
119         Collection<T> result = null;
120
121         try {
122             result = Collection.class.cast(Collections2.filter(substatements, Predicates.instanceOf(type)));
123         } catch (NoSuchElementException e) {
124             result = Collections.emptyList();
125         }
126         return result;
127     }
128
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;
133     }
134
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;
139     }
140 }