Fixup yang-model-api javadoc warnings
[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      * Find the first effective substatement of specified type.
58      *
59      * @param <T> substatement type
60      * @param type {@link DeclaredStatement} type
61      * @return First declared substatement, or empty if no match is found.
62      * @throws NullPointerException if {@code type} is null
63      */
64     @Beta
65     default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
66             @NonNull final Class<T> type) {
67         requireNonNull(type);
68         return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
69     }
70
71     /**
72      * Find the first declared substatement of specified type and return its value.
73      *
74      * @param <T> substatement type
75      * @param <V> substatement argument type
76      * @param type {@link DeclaredStatement} type
77      * @return First declared substatement's argument, or empty if no match is found.
78      * @throws NullPointerException if {@code type} is null
79      */
80     @Beta
81     default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
82             @NonNull final Class<T> type) {
83         return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
84     }
85
86     /**
87      * Find all declared substatements of specified type and return them as a stream.
88      *
89      * @param <T> substatement type
90      * @param type {@link DeclaredStatement} type
91      * @return A stream of all declared substatements of specified type.
92      * @throws NullPointerException if {@code type} is null
93      */
94     @Beta
95     default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
96             @NonNull final Class<T> type) {
97         requireNonNull(type);
98         return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);
99     }
100 }