*/
package org.opendaylight.yangtools.yang.model.api.meta;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableMap;
import java.util.Collection;
import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
/**
* Effective model statement which should be used to derive application behaviour.
S getDeclared();
/**
- * Returns value associated with supplied identifier
+ * Returns value associated with supplied identifier.
*
* @param <K>
* Identifier type
/**
* Returns all local values from supplied namespace.
*
- * @param <K>
- * Identifier type
- * @param <V>
- * Value type
- * @param <N>
- * Namespace identifier type
- * @param namespace
- * Namespace type
+ * @param <K> Identifier type
+ * @param <V> Value type
+ * @param <N> Namespace identifier type
+ * @param namespace Namespace type
* @return Value if present, null otherwise.
*/
@Nullable
<K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull Class<N> namespace);
+ /**
+ * Returns all local values from supplied namespace.
+ *
+ * @param <K> Identifier type
+ * @param <V> Value type
+ * @param <N> Namespace identifier type
+ * @param namespace Namespace type
+ * @return Key-value mappings, empty if the namespace does not exist.
+ */
+ default <K, V, N extends IdentifierNamespace<K, V>> @NonNull Map<K, V> findAll(@NonNull final Class<N> namespace) {
+ final Map<K, V> map = getAll(requireNonNull(namespace));
+ return map == null ? ImmutableMap.of() : map;
+ }
+
/**
* Returns a collection of all effective substatements.
*
* @return collection of all effective substatements.
*/
@Nonnull Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements();
+
+ /**
+ * Find the first effective substatement of specified type.
+ *
+ * @return First effective substatement, or empty if no match is found.
+ */
+ @Beta
+ default <T extends EffectiveStatement<?, ?>> Optional<T> findFirstEffectiveSubstatement(
+ @Nonnull final Class<T> type) {
+ return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast);
+ }
+
+ /**
+ * Find the first effective substatement of specified type and return its value.
+ *
+ * @return First effective substatement's argument, or empty if no match is found.
+ */
+ @Beta
+ default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
+ @Nonnull final Class<T> type) {
+ return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast)
+ .map(EffectiveStatement::argument);
+ }
+
+ /**
+ * Find all effective substatements of specified type and return them as a stream.
+ *
+ * @return A stream of all effective substatements of specified type.
+ */
+ @Beta
+ default <T extends EffectiveStatement<?, ?>> Stream<T> streamEffectiveSubstatements(@Nonnull final Class<T> type) {
+ return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
+ }
}