Clean up TreeNode API
[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.Collection;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.stream.Collectors;
15 import java.util.stream.Stream;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.Empty;
19
20 /**
21  * Effective model statement which should be used to derive application behaviour.
22  *
23  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
24  * @param <D> Class representing declared version of this statement.
25  */
26 public non-sealed interface EffectiveStatement<A, D extends DeclaredStatement<A>> extends ModelStatement<A> {
27     /**
28      * Returns {@link StatementOrigin}, which denotes if statement was explicitly declared in original model or inferred
29      * during semantic processing of model.
30      *
31      * <p>
32      * Implementations are required to return a {@link StatementOrigin}, consistent with {@link #getDeclared()}
33      * nullness. This is what the default implementation does and hence this method should never be explicitly
34      * implemented -- unless there is significant cost to the {@link #getDeclared()} implementation.
35      *
36      * @return statement origin.
37      */
38     default @NonNull StatementOrigin statementOrigin() {
39         return getDeclared() != null ? StatementOrigin.DECLARATION : StatementOrigin.CONTEXT;
40     }
41
42     /**
43      * Returns statement, which was explicit declaration of this effective
44      * statement.
45      *
46      * @return statement, which was explicit declaration of this effective
47      *         statement or null if statement was inferred from context.
48      */
49     @Nullable D getDeclared();
50
51     /**
52      * Returns a collection of all effective substatements.
53      *
54      * @return collection of all effective substatements.
55      */
56     @NonNull List<? extends @NonNull EffectiveStatement<?, ?>> effectiveSubstatements();
57
58     /**
59      * Find the first effective substatement of specified type.
60      *
61      * @param <T> substatement type
62      * @param type substatement type
63      * @return First effective substatement, or empty if no match is found.
64      */
65     @Beta
66     default <T> Optional<T> findFirstEffectiveSubstatement(final @NonNull Class<T> type) {
67         return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
68     }
69
70     /**
71      * Find the first effective substatement of specified type and return its value.
72      *
73      * @param <T> substatement type
74      * @param <V> substatement argument type
75      * @param type substatement type
76      * @return First effective substatement's argument, or empty if no match is found.
77      */
78     @Beta
79     default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
80             final @NonNull Class<T> type) {
81         return findFirstEffectiveSubstatement(type).map(EffectiveStatement::argument);
82     }
83
84     /**
85      * Find all effective substatements of specified type and return them as a stream.
86      *
87      * @param <T> substatement type
88      * @param type substatement type
89      * @return A stream of all effective substatements of specified type.
90      */
91     @Beta
92     default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(final @NonNull Class<T> type) {
93         return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
94     }
95
96     @Beta
97     default <Z extends EffectiveStatement<?, ?>> @NonNull Collection<Z> collectEffectiveSubstatements(
98             final @NonNull Class<Z> stmt) {
99         return streamEffectiveSubstatements(stmt).collect(Collectors.toUnmodifiableList());
100     }
101 }