Generalize findFirstEffectiveSubstatement()
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / EffectiveStatement.java
index 026a0a04093681f3b537f9408e1e6f5591c0ab47..7a8ab021b6c44fdf68e6564265130efc43bd1d3a 100644 (file)
  */
 package org.opendaylight.yangtools.yang.model.api.meta;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
 import java.util.Collection;
 import java.util.Map;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import java.util.Optional;
+import java.util.stream.Stream;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 
 /**
  * Effective model statement which should be used to derive application behaviour.
  *
- * @param <A>
- *            Argument type ({@link Void} if statement does not have argument.)
- * @param <S>
- *            Class representing declared version of this statement.
+ * @param <A> Argument type ({@link Void} if statement does not have argument.)
+ * @param <D> Class representing declared version of this statement.
  */
-public interface EffectiveStatement<A, S extends DeclaredStatement<A>> extends ModelStatement<A> {
-
+public interface EffectiveStatement<A, D extends DeclaredStatement<A>> extends ModelStatement<A> {
     /**
      * Returns statement, which was explicit declaration of this effective
      * statement.
      *
-     *
      * @return statement, which was explicit declaration of this effective
      *         statement or null if statement was inferred from context.
      */
-    @Nullable
-    S getDeclared();
+    @Nullable D getDeclared();
 
     /**
+     * Returns value associated with supplied identifier.
      *
-     * Returns value associated with supplied identifier
-     *
-     * @param <K>
-     *            Identifier type
-     * @param <V>
-     *            Value type
-     * @param <N>
-     *            Namespace identifier type
-     * @param namespace
-     *            Namespace type
-     * @param identifier
-     *            Identifier of element.
-     * @return Value if present, null otherwise.
-     *
-     *
+     * @param <K> Identifier type
+     * @param <V> Value type
+     * @param <N> Namespace identifier type
+     * @param namespace Namespace type
+     * @param identifier Identifier of element.
+     * @return Value if present
      */
-    @Nullable
-    <K, V, N extends IdentifierNamespace<? super K, ? extends V>> V get(@Nonnull Class<N> namespace,@Nonnull  K identifier);
+    //<K, V, N extends IdentifierNamespace<? super K, ? extends V>> V
+    <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends V> get(@NonNull Class<N> namespace,
+            @NonNull K identifier);
 
     /**
+     * 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.
+     * @throws NullPointerException if namespace is null
+     */
+    <K, V, N extends IdentifierNamespace<K, V>> @NonNull 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 Value if present, null otherwise.
+     * @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.
+     * @throws NullPointerException if namespace is null
+     * @deprecated Use {@link #getAll(Class)} instead
      */
-    @Nullable
-    <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(@Nonnull Class<N> namespace);
+    @Deprecated(forRemoval = true)
+    default <K, V, N extends IdentifierNamespace<K, V>> @NonNull Map<K, V> findAll(final @NonNull Class<N> namespace) {
+        return getAll(requireNonNull(namespace));
+    }
 
     /**
+     * Returns a collection of all effective substatements.
      *
-     * Returns iteration 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> Optional<T> findFirstEffectiveSubstatement(final @NonNull 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 iteration of all effective substatements.
+     * @return First effective substatement's argument, or empty if no match is found.
      */
-    Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements();
+    @Beta
+    default <V, T extends EffectiveStatement<V, ?>> Optional<V> findFirstEffectiveSubstatementArgument(
+            final @NonNull Class<T> type) {
+        return findFirstEffectiveSubstatement(type).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(final @NonNull Class<T> type) {
+        return effectiveSubstatements().stream().filter(type::isInstance).map(type::cast);
+    }
 }