Push out FIXMEs
[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 org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * Effective model statement which should be used to derive application behaviour.
23  *
24  * @param <A> Argument type ({@link Void} if statement does not have argument.)
25  * @param <D> Class representing declared version of this statement.
26  */
27 public interface EffectiveStatement<A, D extends DeclaredStatement<A>> extends ModelStatement<A> {
28     /**
29      * Returns statement, which was explicit declaration of this effective
30      * statement.
31      *
32      * @return statement, which was explicit declaration of this effective
33      *         statement or null if statement was inferred from context.
34      */
35     @Nullable D getDeclared();
36
37     /**
38      * Returns value associated with supplied identifier.
39      *
40      * @param <K> Identifier type
41      * @param <V> Value type
42      * @param <N> Namespace identifier type
43      * @param namespace Namespace type
44      * @param identifier Identifier of element.
45      * @return Value if present, null otherwise.
46      */
47     //<K, V, N extends IdentifierNamespace<? super K, ? extends V>> V
48     // FIXME: 4.0.0: make this return an Optional, not a nullable
49     <K, V, N extends IdentifierNamespace<K, V>> @Nullable V get(@NonNull Class<N> namespace, @NonNull K identifier);
50
51     /**
52      * Returns all local values from supplied namespace.
53      *
54      * @param <K> Identifier type
55      * @param <V> Value type
56      * @param <N> Namespace identifier type
57      * @param namespace Namespace type
58      * @return Value if present, null otherwise.
59      */
60     // FIXME: 4.0.0: make this contract return empty maps on non-presence
61     <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAll(@NonNull Class<N> namespace);
62
63     /**
64      * Returns all local values from supplied namespace.
65      *
66      * @param <K> Identifier type
67      * @param <V> Value type
68      * @param <N> Namespace identifier type
69      * @param namespace Namespace type
70      * @return Key-value mappings, empty if the namespace does not exist.
71      */
72     // FIXME: 4.0.0: remove this in favor of fixed getAll()
73     default <K, V, N extends IdentifierNamespace<K, V>> @NonNull Map<K, V> findAll(final @NonNull Class<N> namespace) {
74         final Map<K, V> map = getAll(requireNonNull(namespace));
75         return map == null ? ImmutableMap.of() : map;
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 extends EffectiveStatement<?, ?>> Optional<T> findFirstEffectiveSubstatement(
92             final @NonNull Class<T> type) {
93         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
94     }
95
96     /**
97      * Find the first effective substatement of specified type and return its value.
98      *
99      * @return First effective substatement's argument, or empty if no match is found.
100      */
101     @Beta
102     default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
103             final @NonNull Class<T> type) {
104         return findFirstEffectiveSubstatement(type).map(EffectiveStatement::argument);
105     }
106
107     /**
108      * Find all effective substatements of specified type and return them as a stream.
109      *
110      * @return A stream of all effective substatements of specified type.
111      */
112     @Beta
113     default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(final @NonNull Class<T> type) {
114         return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
115     }
116 }