ca2132e06b47f5016af6dc8e987a051d857c17b9
[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 type {@link DeclaredStatement} type
46      * @return Collection of statements, which were explicitly declared in source of model.
47      * @throws NullPointerException if {@code type} is null
48      */
49     default <S extends DeclaredStatement<?>> @NonNull Collection<? extends S> declaredSubstatements(
50             final Class<S> type) {
51         requireNonNull(type);
52         return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
53     }
54
55     /**
56      * Find the first effective substatement of specified type.
57      *
58      * @param type {@link DeclaredStatement} type
59      * @return First declared substatement, or empty if no match is found.
60      * @throws NullPointerException if {@code type} is null
61      */
62     @Beta
63     default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
64             @NonNull final Class<T> type) {
65         requireNonNull(type);
66         return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
67     }
68
69     /**
70      * Find the first declared substatement of specified type and return its value.
71      *
72      * @return First declared substatement's argument, or empty if no match is found.
73      * @throws NullPointerException if {@code type} is null
74      */
75     @Beta
76     default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
77             @NonNull final Class<T> type) {
78         return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
79     }
80
81     /**
82      * Find all declared substatements of specified type and return them as a stream.
83      *
84      * @return A stream of all declared substatements of specified type.
85      * @throws NullPointerException if {@code type} is null
86      */
87     @Beta
88     default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
89             @NonNull final Class<T> type) {
90         requireNonNull(type);
91         return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);
92     }
93 }