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 java.util.Collection;
14 import java.util.Optional;
15 import java.util.stream.Stream;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.eclipse.jdt.annotation.NonNull;
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
35 * ordering from original source.
37 * @return Collection of statements, which were explicitly declared in
40 @Nonnull Collection<? extends DeclaredStatement<?>> declaredSubstatements();
43 * Find the first effective substatement of specified type.
45 * @param type {@link DeclaredStatement} type
46 * @return First declared substatement, or empty if no match is found.
49 default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
50 @NonNull final Class<T> type) {
52 return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
56 * Find the first declared substatement of specified type and return its value.
58 * @return First declared substatement's argument, or empty if no match is found.
59 * @throws NullPointerException if {@code type} is null
62 default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
63 @NonNull final Class<T> type) {
64 return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
68 * Find all declared substatements of specified type and return them as a stream.
70 * @return A stream of all declared substatements of specified type.
71 * @throws NullPointerException if {@code type} is null
74 default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
75 @NonNull final Class<T> type) {
77 return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);