Expose EffectiveModuleStatement prefix mapping
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / EffectiveStatement.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.model.api.meta;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableMap;
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.stream.Stream;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.eclipse.jdt.annotation.NonNull;
21
22 /**
23  * Effective model statement which should be used to derive application behaviour.
24  *
25  * @param <A>
26  *            Argument type ({@link Void} if statement does not have argument.)
27  * @param <S>
28  *            Class representing declared version of this statement.
29  */
30 public interface EffectiveStatement<A, S extends DeclaredStatement<A>> extends ModelStatement<A> {
31
32     /**
33      * Returns statement, which was explicit declaration of this effective
34      * statement.
35      *
36      *
37      * @return statement, which was explicit declaration of this effective
38      *         statement or null if statement was inferred from context.
39      */
40     @Nullable
41     S getDeclared();
42
43     /**
44      * Returns value associated with supplied identifier.
45      *
46      * @param <K>
47      *            Identifier type
48      * @param <V>
49      *            Value type
50      * @param <N>
51      *            Namespace identifier type
52      * @param namespace
53      *            Namespace type
54      * @param identifier
55      *            Identifier of element.
56      * @return Value if present, null otherwise.
57      */
58     //<K, V, N extends IdentifierNamespace<? super K, ? extends V>> V
59     @Nullable
60     <K,V,N extends IdentifierNamespace<K, V>> V get(@Nonnull Class<N> namespace,@Nonnull  K identifier);
61
62     /**
63      * Returns all local values from supplied namespace.
64      *
65      * @param <K> Identifier type
66      * @param <V> Value type
67      * @param <N> Namespace identifier type
68      * @param namespace Namespace type
69      * @return Value if present, null otherwise.
70      */
71     @Nullable
72     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull Class<N> namespace);
73
74     /**
75      * Returns all local values from supplied namespace.
76      *
77      * @param <K> Identifier type
78      * @param <V> Value type
79      * @param <N> Namespace identifier type
80      * @param namespace Namespace type
81      * @return Key-value mappings, empty if the namespace does not exist.
82      */
83     default <K, V, N extends IdentifierNamespace<K, V>> @NonNull Map<K, V> findAll(@NonNull final Class<N> namespace) {
84         final Map<K, V> map = getAll(requireNonNull(namespace));
85         return map == null ? ImmutableMap.of() : map;
86     }
87
88     /**
89      * Returns a collection of all effective substatements.
90      *
91      * @return collection of all effective substatements.
92      */
93     @Nonnull Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements();
94
95     /**
96      * Find the first effective substatement of specified type.
97      *
98      * @return First effective substatement, or empty if no match is found.
99      */
100     @Beta
101     default <T extends EffectiveStatement<?, ?>> Optional<T> findFirstEffectiveSubstatement(
102             @Nonnull final Class<T> type) {
103         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
104     }
105
106     /**
107      * Find the first effective substatement of specified type and return its value.
108      *
109      * @return First effective substatement's argument, or empty if no match is found.
110      */
111     @Beta
112     default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
113             @Nonnull final Class<T> type) {
114         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast)
115                 .map(EffectiveStatement::argument);
116     }
117
118     /**
119      * Find all effective substatements of specified type and return them as a stream.
120      *
121      * @return A stream of all effective substatements of specified type.
122      */
123     @Beta
124     default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(@Nonnull final Class<T> type) {
125         return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
126     }
127 }