b4d253de7522c8e59995e0d07aa8cfd1e639e1d7
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / DeclaredStatement.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.Collections2;
14 import java.util.Collection;
15 import java.util.List;
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 import org.opendaylight.yangtools.yang.common.Empty;
21
22 /**
23  * Represents declared statement.
24  *
25  * @param <A> Argument type ({@link Empty} if statement does not have argument.)
26  */
27 public non-sealed interface DeclaredStatement<A> extends ModelStatement<A> {
28     /**
29      * Returns statement argument as was present in original source.
30      *
31      * @return statement argument as was present in original source or null, if statement does not take argument.
32      */
33     @Nullable String rawArgument();
34
35     /**
36      * Returns collection of explicitly declared child statements, while preserving its original ordering from original
37      * source.
38      *
39      * @return Collection of statements, which were explicitly declared in source of model.
40      */
41     @NonNull List<? extends DeclaredStatement<?>> declaredSubstatements();
42
43     /**
44      * Returns collection of explicitly declared child statements, while preserving its original ordering from original
45      * source.
46      *
47      * @param <T> substatement type
48      * @param type {@link DeclaredStatement} type
49      * @return Collection of statements, which were explicitly declared in source of model.
50      * @throws NullPointerException if {@code type} is null
51      */
52     default <T extends DeclaredStatement<?>> @NonNull Collection<? extends T> declaredSubstatements(
53             final Class<T> type) {
54         requireNonNull(type);
55         return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
56     }
57
58     /**
59      * Returns a {@link DeclarationReference} associated with this statement, if available.
60      *
61      * @apiNote
62      *     This method does not contribute any semantic information and is provided purely as a conduit for
63      *     implementation-specific information where a statement instance came from.
64      *
65      * @implSpec
66      *     The default implementation returns {@link Optional#empty()}.
67      *
68      * @return A {@link DeclarationReference} associated with this statement or {@link Optional#empty()}.
69      */
70     @Beta
71     default @NonNull Optional<DeclarationReference> declarationReference() {
72         return Optional.empty();
73     }
74
75     /**
76      * Find the first effective substatement of specified type.
77      *
78      * @param <T> substatement type
79      * @param type {@link DeclaredStatement} type
80      * @return First declared substatement, or empty if no match is found.
81      * @throws NullPointerException if {@code type} is null
82      */
83     @Beta
84     default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
85             @NonNull final Class<T> type) {
86         requireNonNull(type);
87         return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
88     }
89
90     /**
91      * Find the first declared substatement of specified type and return its value.
92      *
93      * @param <T> substatement type
94      * @param <V> substatement argument type
95      * @param type {@link DeclaredStatement} type
96      * @return First declared substatement's argument, or empty if no match is found.
97      * @throws NullPointerException if {@code type} is null
98      */
99     @Beta
100     default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
101             @NonNull final Class<T> type) {
102         return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
103     }
104
105     /**
106      * Find all declared substatements of specified type and return them as a stream.
107      *
108      * @param <T> substatement type
109      * @param type {@link DeclaredStatement} type
110      * @return A stream of all declared substatements of specified type.
111      * @throws NullPointerException if {@code type} is null
112      */
113     @Beta
114     default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
115             @NonNull final Class<T> type) {
116         requireNonNull(type);
117         return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);
118     }
119 }