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