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.ImmutableMap;
14 import java.util.Collection;
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;
22 * Effective model statement which should be used to derive application behaviour.
24 * @param <A> Argument type ({@link Void} if statement does not have argument.)
25 * @param <D> Class representing declared version of this statement.
27 public interface EffectiveStatement<A, D extends DeclaredStatement<A>> extends ModelStatement<A> {
29 * Returns statement, which was explicit declaration of this effective
32 * @return statement, which was explicit declaration of this effective
33 * statement or null if statement was inferred from context.
35 @Nullable D getDeclared();
38 * Returns value associated with supplied identifier.
40 * @param <K> Identifier type
41 * @param <V> Value type
42 * @param <N> Namespace identifier type
43 * @param namespace Namespace type
44 * @param identifier Identifier of element.
45 * @return Value if present, null otherwise.
47 //<K, V, N extends IdentifierNamespace<? super K, ? extends V>> V
48 // FIXME: 4.0.0: make this return an Optional, not a nullable
49 <K, V, N extends IdentifierNamespace<K, V>> @Nullable V get(@NonNull Class<N> namespace, @NonNull K identifier);
52 * Returns all local values from supplied namespace.
54 * @param <K> Identifier type
55 * @param <V> Value type
56 * @param <N> Namespace identifier type
57 * @param namespace Namespace type
58 * @return Value if present, null otherwise.
60 // FIXME: 4.0.0: make this contract return empty maps on non-presence
61 <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAll(@NonNull Class<N> namespace);
64 * Returns all local values from supplied namespace.
66 * @param <K> Identifier type
67 * @param <V> Value type
68 * @param <N> Namespace identifier type
69 * @param namespace Namespace type
70 * @return Key-value mappings, empty if the namespace does not exist.
72 // FIXME: 4.0.0: remove this in favor of fixed getAll()
73 default <K, V, N extends IdentifierNamespace<K, V>> @NonNull Map<K, V> findAll(final @NonNull Class<N> namespace) {
74 final Map<K, V> map = getAll(requireNonNull(namespace));
75 return map == null ? ImmutableMap.of() : map;
79 * Returns a collection of all effective substatements.
81 * @return collection of all effective substatements.
83 @NonNull Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements();
86 * Find the first effective substatement of specified type.
88 * @return First effective substatement, or empty if no match is found.
91 default <T extends EffectiveStatement<?, ?>> Optional<T> findFirstEffectiveSubstatement(
92 final @NonNull Class<T> type) {
93 return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
97 * Find the first effective substatement of specified type and return its value.
99 * @return First effective substatement's argument, or empty if no match is found.
102 default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
103 final @NonNull Class<T> type) {
104 return findFirstEffectiveSubstatement(type).map(EffectiveStatement::argument);
108 * Find all effective substatements of specified type and return them as a stream.
110 * @return A stream of all effective substatements of specified type.
113 default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(final @NonNull Class<T> type) {
114 return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);