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