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;
19 import org.opendaylight.yangtools.yang.common.Empty;
22 * Represents declared statement.
24 * @param <A> Argument type ({@link Empty} if statement does not have argument.)
26 public interface DeclaredStatement<A> extends ModelStatement<A> {
28 * Returns statement argument as was present in original source.
30 * @return statement argument as was present in original source or null, if statement does not take argument.
32 @Nullable String rawArgument();
35 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
38 * @return Collection of statements, which were explicitly declared in source of model.
40 @NonNull Collection<? extends DeclaredStatement<?>> declaredSubstatements();
43 * Returns collection of explicitly declared child statements, while preserving its original ordering from original
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
51 default <T extends DeclaredStatement<?>> @NonNull Collection<? extends T> declaredSubstatements(
52 final Class<T> type) {
54 return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
58 * Returns a {@link DeclarationReference} associated with this statement, if available.
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.
65 * The default implementation returns {@link Optional#empty()}.
67 * @return A {@link DeclarationReference} associated with this statement or {@link Optional#empty()}.
70 default @NonNull Optional<DeclarationReference> declarationReference() {
71 return Optional.empty();
75 * Find the first effective substatement of specified type.
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
83 default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
84 @NonNull final Class<T> type) {
86 return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
90 * Find the first declared substatement of specified type and return its value.
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
99 default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
100 @NonNull final Class<T> type) {
101 return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
105 * Find all declared substatements of specified type and return them as a stream.
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
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);