Correct argument type documentation
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / meta / AbstractEffectiveStatement.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.model.spi.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.Collection;
15 import java.util.LinkedHashMap;
16 import java.util.Map;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.Empty;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
28
29 /**
30  * Baseline stateless implementation of an EffectiveStatement. This class adds a few default implementations and
31  * namespace dispatch, but does not actually force any state on its subclasses. This approach adds requirements for an
32  * implementation, but it leaves it up to the final class to provide object layout.
33  *
34  * <p>
35  * This finds immense value in catering the common case, for example effective statements which can, but typically
36  * do not, contain substatements.
37  *
38  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
39  * @param <D> Class representing declared version of this statement.
40  */
41 abstract class AbstractEffectiveStatement<A, D extends DeclaredStatement<A>>
42         extends AbstractModelStatement<A> implements EffectiveStatement<A, D> {
43     @Override
44     public final <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(final Class<N> namespace,
45             final K identifier) {
46         return Optional.ofNullable(getAll(namespace).get(requireNonNull(identifier)));
47     }
48
49     @Override
50     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
51         final Optional<? extends Map<K, V>> ret = getNamespaceContents(requireNonNull(namespace));
52         return ret.isPresent() ? ret.get() : ImmutableMap.of();
53     }
54
55     @Override
56     public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
57         return ImmutableList.of();
58     }
59
60     /**
61      * Return the statement-specific contents of specified namespace, if available.
62      *
63      * @param namespace Requested namespace
64      * @return Namespace contents, if available.
65      */
66     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
67             final @NonNull Class<N> namespace) {
68         return Optional.empty();
69     }
70
71     /**
72      * Utility method for recovering singleton lists squashed by {@link #maskList(ImmutableList)}.
73      *
74      * @param masked list to unmask
75      * @return Unmasked list
76      * @throws NullPointerException if masked is null
77      * @throws ClassCastException if masked object does not match EffectiveStatement
78      */
79     @SuppressWarnings({ "rawtypes", "unchecked" })
80     protected static final @NonNull ImmutableList<? extends @NonNull EffectiveStatement<?, ?>> unmaskList(
81             final @NonNull Object masked) {
82         return (ImmutableList) unmaskList(masked, EffectiveStatement.class);
83     }
84
85     // TODO: below methods need to find a better place, this is just a temporary hideout as their public class is on
86     //       its way out
87     protected static @NonNull Map<QName, SchemaTreeEffectiveStatement<?>> createSchemaTreeNamespace(
88             final Collection<? extends EffectiveStatement<?, ?>> substatements) {
89         final Map<QName, SchemaTreeEffectiveStatement<?>> schemaChildren = new LinkedHashMap<>();
90         substatements.stream().filter(SchemaTreeEffectiveStatement.class::isInstance)
91             .forEach(child -> putChild(schemaChildren, (SchemaTreeEffectiveStatement<?>) child, "schema"));
92         return schemaChildren;
93     }
94
95     protected static @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> createDataTreeNamespace(
96             final Collection<SchemaTreeEffectiveStatement<?>> schemaTreeStatements,
97             // Note: this dance is needed to not retain ImmutableMap$Values
98             final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace) {
99         final Map<QName, DataTreeEffectiveStatement<?>> dataChildren = new LinkedHashMap<>();
100         boolean sameAsSchema = true;
101
102         for (SchemaTreeEffectiveStatement<?> child : schemaTreeStatements) {
103             if (!indexDataTree(dataChildren, child)) {
104                 sameAsSchema = false;
105             }
106         }
107
108         // This is a mighty hack to lower memory usage: if we consumed all schema tree children as data nodes,
109         // the two maps are equal and hence we can share the instance.
110         return sameAsSchema ? (ImmutableMap) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
111     }
112
113     private static boolean indexDataTree(final Map<QName, DataTreeEffectiveStatement<?>> map,
114             final EffectiveStatement<?, ?> stmt) {
115         if (stmt instanceof DataTreeEffectiveStatement) {
116             putChild(map, (DataTreeEffectiveStatement<?>) stmt, "data");
117             return true;
118         } else if (stmt instanceof ChoiceEffectiveStatement) {
119             // For choice statements go through all their cases and fetch their data children
120             for (EffectiveStatement<?, ?> choiceChild : stmt.effectiveSubstatements()) {
121                 if (choiceChild instanceof CaseEffectiveStatement) {
122                     for (EffectiveStatement<?, ?> caseChild : choiceChild.effectiveSubstatements()) {
123                         indexDataTree(map, caseChild);
124                     }
125                 }
126             }
127         } else if (stmt instanceof CaseEffectiveStatement) {
128             // For case statements go through all their statements
129             for (EffectiveStatement<?, ?> child : stmt.effectiveSubstatements()) {
130                 indexDataTree(map, child);
131             }
132         }
133         return false;
134     }
135
136     private static <T extends SchemaTreeEffectiveStatement<?>> void putChild(final Map<QName, T> map, final T child,
137             final String tree) {
138         final QName id = child.getIdentifier();
139         final T prev = map.putIfAbsent(id, child);
140         if (prev != null) {
141             throw new SubstatementIndexingException(
142                 "Cannot add " + tree + " tree child with name " + id + ", a conflicting child already exists");
143         }
144     }
145 }