Suppress UPM_UNCALLED_PRIVATE_METHOD
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / 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.rfc7950.stmt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.Set;
22 import java.util.function.Predicate;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30
31 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
32     private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
33
34     /**
35      * Constructor.
36      *
37      * @param ctx context of statement.
38      */
39     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
40         final Collection<StmtContext<?, ?, ?>> substatementsInit = new ArrayList<>();
41
42         /*
43          * This dance is required to ensure that effects of 'uses' nodes are applied in the same order as
44          * the statements were defined -- i.e. if we have something like this:
45          *
46          * container foo {
47          *   uses bar;
48          *   uses baz;
49          * }
50          *
51          * grouping baz {
52          *   leaf baz {
53          *     type string;
54          *   }
55          * }
56          *
57          * grouping bar {
58          *   leaf bar {
59          *     type string;
60          *   }
61          * }
62          *
63          * The reactor would first inline 'uses baz' as that definition is the first one completely resolved and then
64          * inline 'uses bar'. Here we are iterating in declaration order re-inline the statements.
65          *
66          * TODO: this really should be handled by UsesStatementSupport such that 'uses baz' would have a prerequisite
67          *       of a resolved 'uses bar'.
68          */
69         Set<StmtContext<?, ?, ?>> filteredStatements = null;
70         for (final StmtContext<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
71             if (declaredSubstatement.isSupportedByFeatures()) {
72                 substatementsInit.add(declaredSubstatement);
73
74                 final Collection<? extends StmtContext<?, ?, ?>> effect = declaredSubstatement.getEffectOfStatement();
75                 if (!effect.isEmpty()) {
76                     if (filteredStatements == null) {
77                         filteredStatements = new HashSet<>();
78                     }
79                     filteredStatements.addAll(effect);
80                     substatementsInit.addAll(effect);
81                 }
82             }
83         }
84
85         if (filteredStatements != null) {
86             for (StmtContext<?, ?, ?> stmt : ctx.effectiveSubstatements()) {
87                 if (!filteredStatements.contains(stmt)) {
88                     substatementsInit.add(stmt);
89                 }
90             }
91         } else {
92             substatementsInit.addAll(ctx.effectiveSubstatements());
93         }
94
95         this.substatements = ImmutableList.copyOf(initSubstatements(ctx, substatementsInit));
96     }
97
98     @Beta
99     protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(final StmtContext<A, D, ?> ctx,
100             final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
101         return initSubstatements(substatementsInit);
102     }
103
104     /**
105      * Create a set of substatements. This method is split out so it can be overridden in
106      * ExtensionEffectiveStatementImpl to leak a not-fully-initialized instance.
107      *
108      * @param substatementsInit proposed substatements
109      * @return Filtered substatements
110      */
111     protected Collection<? extends EffectiveStatement<?, ?>> initSubstatements(
112             final Collection<? extends StmtContext<?, ?, ?>> substatementsInit) {
113         return Collections2.transform(Collections2.filter(substatementsInit,
114             StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective);
115     }
116
117     @Override
118     public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
119             final K identifier) {
120         return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
121     }
122
123     @Override
124     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
125         final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
126         return ret.isPresent() ? ret.get() : ImmutableMap.of();
127     }
128
129     /**
130      * Return the statement-specific contents of specified namespace, if available.
131      *
132      * @param namespace Requested namespace
133      * @return Namespace contents, if available.
134      */
135     @Beta
136     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
137             final @NonNull Class<N> namespace) {
138         return Optional.empty();
139     }
140
141     @Override
142     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
143         return substatements;
144     }
145
146     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
147         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
148     }
149
150     @SuppressWarnings("unchecked")
151     public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
152         return Collection.class.cast(Collections2.filter(substatements, type::isInstance));
153     }
154
155     protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
156         return substatements.stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
157     }
158
159     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
160         return substatements.stream()
161                 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
162                 .findFirst().map(returnType::cast).orElse(null);
163     }
164
165     protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
166         return substatements.stream().filter(type::isInstance).findFirst().orElse(null);
167     }
168 }