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