Fix sonar warnings is yang-parser-impl
[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.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;
18 import java.util.Map;
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;
30
31 abstract public class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
32         implements EffectiveStatement<A, D> {
33
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;
39
40     private final A argument;
41
42     public EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
43
44         this.stmtCtx = ctx;
45         this.statementDefinition = ctx.getPublicDefinition();
46         this.argument = ctx.getStatementArgument();
47         this.statementSource = ctx.getStatementSource();
48
49         Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx
50                 .declaredSubstatements();
51         Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx
52                 .effectiveSubstatements();
53
54         Collection<StatementContextBase<?, ?, ?>> substatementsInit = new LinkedList<>();
55
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());
62             } else {
63                 substatementsInit.add(declaredSubstatement);
64             }
65         }
66
67         substatementsInit.addAll(effectiveSubstatements);
68
69         this.substatements = FluentIterable.from(substatementsInit).filter(StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE)
70                 .transform(StmtContextUtils.buildEffective()).toList();
71     }
72
73     @Override
74     public StatementDefinition statementDefinition() {
75         return statementDefinition;
76     }
77
78     @Override
79     public A argument() {
80         return argument;
81     }
82
83     @Override
84     public StatementSource getStatementSource() {
85         return statementSource;
86     }
87
88     @Override
89     public D getDeclared() {
90         if (declaredInstance == null) {
91             declaredInstance = stmtCtx.buildDeclared();
92         }
93         return declaredInstance;
94     }
95
96     @Override
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);
100     }
101
102     @Override
103     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(
104             final Class<N> namespace) {
105         return stmtCtx.getAllFromNamespace(namespace);
106     }
107
108     @Override
109     public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
110         return substatements;
111     }
112
113     public StmtContext<A, D, ?> getStatementContext() {
114         return stmtCtx;
115     }
116
117     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(
118             final Class<S> type) {
119         S result = null;
120         try {
121             result = type.cast(Iterables.find(substatements,
122                     Predicates.instanceOf(type)));
123         } catch (NoSuchElementException e) {
124             result = null;
125         }
126         return result;
127     }
128
129     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
130         S result = null;
131         try {
132             result = type.cast(Iterables.find(substatements,
133                     Predicates.instanceOf(type)));
134         } catch (NoSuchElementException e) {
135             result = null;
136         }
137         return result;
138     }
139
140     @SuppressWarnings("unchecked")
141     protected final <T> Collection<T> allSubstatementsOfType(
142             final Class<T> type) {
143         Collection<T> result = null;
144
145         try {
146             result = Collection.class.cast(Collections2.filter(substatements,
147                     Predicates.instanceOf(type)));
148         } catch (NoSuchElementException e) {
149             result = Collections.emptyList();
150         }
151         return result;
152     }
153
154     protected final <T> T firstSubstatementOfType(final Class<T> type) {
155         T result = null;
156         try {
157             result = type.cast(Iterables.find(substatements,
158                     Predicates.instanceOf(type)));
159         } catch (NoSuchElementException e) {
160             result = null;
161         }
162         return result;
163     }
164
165     protected final <R> R firstSubstatementOfType(final Class<?> type,
166             final Class<R> returnType) {
167         R result = null;
168         try {
169             result = returnType.cast(Iterables.find(
170                     substatements,
171                     Predicates.and(Predicates.instanceOf(type),
172                             Predicates.instanceOf(returnType))));
173         } catch (NoSuchElementException e) {
174             result = null;
175         }
176         return result;
177     }
178
179 }