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.List;
16 import java.util.Optional;
17 import java.util.stream.Stream;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.yang.common.Empty;
23 * Represents declared statement.
25 * @param <A> Argument type ({@link Empty} if statement does not have argument.)
27 public non-sealed interface DeclaredStatement<A> extends ModelStatement<A> {
29 * Returns statement argument as was present in original source.
31 * @return statement argument as was present in original source or null, if statement does not take argument.
33 @Nullable String rawArgument();
36 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
39 * @return Collection of statements, which were explicitly declared in source of model.
41 @NonNull List<? extends DeclaredStatement<?>> declaredSubstatements();
44 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
47 * @param <T> substatement type
48 * @param type {@link DeclaredStatement} type
49 * @return Collection of statements, which were explicitly declared in source of model.
50 * @throws NullPointerException if {@code type} is null
52 default <T extends DeclaredStatement<?>> @NonNull Collection<? extends T> declaredSubstatements(
53 final Class<T> type) {
55 return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
59 * Returns a {@link DeclarationReference} associated with this statement, if available.
62 * This method does not contribute any semantic information and is provided purely as a conduit for
63 * implementation-specific information where a statement instance came from.
66 * The default implementation returns {@link Optional#empty()}.
68 * @return A {@link DeclarationReference} associated with this statement or {@link Optional#empty()}.
71 default @NonNull Optional<DeclarationReference> declarationReference() {
72 return Optional.empty();
76 * Find the first effective substatement of specified type.
78 * @param <T> substatement type
79 * @param type {@link DeclaredStatement} type
80 * @return First declared substatement, or empty if no match is found.
81 * @throws NullPointerException if {@code type} is null
84 default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
85 @NonNull final Class<T> type) {
87 return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
91 * Find the first declared substatement of specified type and return its value.
93 * @param <T> substatement type
94 * @param <V> substatement argument type
95 * @param type {@link DeclaredStatement} type
96 * @return First declared substatement's argument, or empty if no match is found.
97 * @throws NullPointerException if {@code type} is null
100 default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
101 @NonNull final Class<T> type) {
102 return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
106 * Find all declared substatements of specified type and return them as a stream.
108 * @param <T> substatement type
109 * @param type {@link DeclaredStatement} type
110 * @return A stream of all declared substatements of specified type.
111 * @throws NullPointerException if {@code type} is null
114 default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
115 @NonNull final Class<T> type) {
116 requireNonNull(type);
117 return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);