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