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