019034ffbd2f5f0d402b1c48c6b7f3699375420c
[yangtools.git] / model / 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.List;
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 import org.opendaylight.yangtools.yang.common.Empty;
18
19 /**
20  * Effective model statement which should be used to derive application behaviour.
21  *
22  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
23  * @param <D> Class representing declared version of this statement.
24  */
25 public non-sealed interface EffectiveStatement<A, D extends DeclaredStatement<A>> extends ModelStatement<A> {
26     /**
27      * Returns {@link StatementOrigin}, which denotes if statement was explicitly declared in original model or inferred
28      * during semantic processing of model.
29      *
30      * <p>
31      * Implementations are required to return a {@link StatementOrigin}, consistent with {@link #getDeclared()}
32      * nullness. This is what the default implementation does and hence this method should never be explicitly
33      * implemented -- unless there is significant cost to the {@link #getDeclared()} implementation.
34      *
35      * @return statement origin.
36      */
37     default @NonNull StatementOrigin statementOrigin() {
38         return getDeclared() != null ? StatementOrigin.DECLARATION : StatementOrigin.CONTEXT;
39     }
40
41     /**
42      * Returns statement, which was explicit declaration of this effective
43      * statement.
44      *
45      * @return statement, which was explicit declaration of this effective
46      *         statement or null if statement was inferred from context.
47      */
48     @Nullable D getDeclared();
49
50     /**
51      * Returns value associated with supplied identifier.
52      *
53      * @param <K> Identifier type
54      * @param <V> Value type
55      * @param <N> Namespace identifier type
56      * @param namespace Namespace type
57      * @param identifier Identifier of element.
58      * @return Value if present
59      */
60     <K, V, N extends IdentifierNamespace<K, V>> @NonNull Optional<V> get(@NonNull Class<N> namespace,
61         @NonNull K identifier);
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      * @throws NullPointerException if namespace is null
72      */
73     <K, V, N extends IdentifierNamespace<K, V>> @NonNull Map<K, V> getAll(@NonNull Class<N> namespace);
74
75     /**
76      * Returns a collection of all effective substatements.
77      *
78      * @return collection of all effective substatements.
79      */
80     @NonNull List<? extends @NonNull EffectiveStatement<?, ?>> effectiveSubstatements();
81
82     /**
83      * Find the first effective substatement of specified type.
84      *
85      * @param <T> substatement type
86      * @param type substatement type
87      * @return First effective substatement, or empty if no match is found.
88      */
89     @Beta
90     default <T> Optional<T> findFirstEffectiveSubstatement(final @NonNull Class<T> type) {
91         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
92     }
93
94     /**
95      * Find the first effective substatement of specified type and return its value.
96      *
97      * @param <T> substatement type
98      * @param <V> substatement argument type
99      * @param type substatement type
100      * @return First effective substatement's argument, or empty if no match is found.
101      */
102     @Beta
103     default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
104             final @NonNull Class<T> type) {
105         return findFirstEffectiveSubstatement(type).map(EffectiveStatement::argument);
106     }
107
108     /**
109      * Find all effective substatements of specified type and return them as a stream.
110      *
111      * @param <T> substatement type
112      * @param type substatement type
113      * @return A stream of all effective substatements of specified type.
114      */
115     @Beta
116     default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(final @NonNull Class<T> type) {
117         return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
118     }
119 }