Expose EffectiveModuleStatement prefix mapping
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / EffectiveStatement.java
index e3ef1c5ffec00e3caeda76c382161546a10ef8a9..2c4ff1d3fbdcd6d7f8dd49c21d96a12b7d7fffa8 100644 (file)
@@ -7,10 +7,17 @@
  */
 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.
@@ -34,7 +41,7 @@ public interface EffectiveStatement<A, S extends DeclaredStatement<A>> extends M
     S getDeclared();
 
     /**
-     * Returns value associated with supplied identifier
+     * Returns value associated with supplied identifier.
      *
      * @param <K>
      *            Identifier type
@@ -55,23 +62,66 @@ public interface EffectiveStatement<A, S extends DeclaredStatement<A>> extends M
     /**
      * 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);
+    }
 }