2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.api.meta;
10 import static java.util.Objects.requireNonNull;
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;
21 * Represents declared statement.
23 * @param <A> Argument type ({@link Void} if statement does not have argument.)
25 public interface DeclaredStatement<A> extends ModelStatement<A> {
27 * Returns statement argument as was present in original source.
29 * @return statement argument as was present in original source or null, if statement does not take argument.
31 @Nullable String rawArgument();
34 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
37 * @return Collection of statements, which were explicitly declared in source of model.
39 @NonNull Collection<? extends DeclaredStatement<?>> declaredSubstatements();
42 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
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
50 default <T extends DeclaredStatement<?>> @NonNull Collection<? extends T> declaredSubstatements(
51 final Class<T> type) {
53 return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
57 * Returns a {@link DeclarationReference} associated with this statement, if available.
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.
64 * The default implementation returns {@link Optional#empty()}.
66 * @return A {@link DeclarationReference} associated with this statement or {@link Optional#empty()}.
69 default @NonNull Optional<DeclarationReference> declarationReference() {
70 return Optional.empty();
74 * Find the first effective substatement of specified type.
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
82 default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
83 @NonNull final Class<T> type) {
85 return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
89 * Find the first declared substatement of specified type and return its value.
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
98 default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
99 @NonNull final Class<T> type) {
100 return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
104 * Find all declared substatements of specified type and return them as a stream.
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
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);