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 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
49 default <S extends DeclaredStatement<?>> @NonNull Collection<? extends S> declaredSubstatements(
50 final Class<S> type) {
52 return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
56 * Find the first effective substatement of specified type.
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
63 default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
64 @NonNull final Class<T> type) {
66 return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
70 * Find the first declared substatement of specified type and return its value.
72 * @return First declared substatement's argument, or empty if no match is found.
73 * @throws NullPointerException if {@code type} is null
76 default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
77 @NonNull final Class<T> type) {
78 return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
82 * Find all declared substatements of specified type and return them as a stream.
84 * @return A stream of all declared substatements of specified type.
85 * @throws NullPointerException if {@code type} is null
88 default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
89 @NonNull final Class<T> type) {
91 return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);