DeclaredStatements can contain default implementations
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / meta / DeclaredStatement.java
index dbe80e96f21bbc9d4ae7259fbc9aa53781b88650..11167a01f7e45a6b90d79d589f8103f9ed674468 100644 (file)
@@ -7,18 +7,24 @@
  */
 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.Collections2;
 import java.util.Collection;
+import java.util.Optional;
+import java.util.stream.Stream;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+
 /**
- * Represents declared statement
+ * Represents declared statement.
  *
  * @param <A> Argument type ({@link Void} if statement does not have argument.)
  */
 public interface DeclaredStatement<A> extends ModelStatement<A> {
-
     /**
-     *
      * Returns statement argument as was present in original source.
      *
      * @return statement argument as was present in original source or null, if statement does not take argument.
@@ -26,13 +32,63 @@ public interface DeclaredStatement<A> extends ModelStatement<A> {
     @Nullable String rawArgument();
 
     /**
+     * Returns collection of explicitly declared child statements, while preserving its original ordering from original
+     * source.
      *
-     * Returns collection of explicitly declared child statements, while preserving its original
-     * ordering from original source.
-     *
-     * @return Collection of statements, which were explicitly declared in
-     *         source of model.
+     * @return Collection of statements, which were explicitly declared in source of model.
      */
     @Nonnull Collection<? extends DeclaredStatement<?>> declaredSubstatements();
 
+    /**
+     * Returns collection of explicitly declared child statements, while preserving its original ordering from original
+     * source.
+     *
+     * @param type {@link DeclaredStatement} type
+     * @return Collection of statements, which were explicitly declared in source of model.
+     * @throws NullPointerException if {@code type} is null
+     */
+    default <S extends DeclaredStatement<?>> @NonNull Collection<? extends S> declaredSubstatements(
+            final Class<S> type) {
+        requireNonNull(type);
+        return Collections2.transform(Collections2.filter(declaredSubstatements(), type::isInstance), type::cast);
+    }
+
+    /**
+     * Find the first effective substatement of specified type.
+     *
+     * @param type {@link DeclaredStatement} type
+     * @return First declared substatement, or empty if no match is found.
+     * @throws NullPointerException if {@code type} is null
+     */
+    @Beta
+    default <T extends DeclaredStatement<?>> @NonNull Optional<T> findFirstDeclaredSubstatement(
+            @NonNull final Class<T> type) {
+        requireNonNull(type);
+        return streamDeclaredSubstatements(type).filter(type::isInstance).findFirst().map(type::cast);
+    }
+
+    /**
+     * Find the first declared substatement of specified type and return its value.
+     *
+     * @return First declared substatement's argument, or empty if no match is found.
+     * @throws NullPointerException if {@code type} is null
+     */
+    @Beta
+    default <V, T extends DeclaredStatement<V>> @NonNull Optional<V> findFirstDeclaredSubstatementArgument(
+            @NonNull final Class<T> type) {
+        return findFirstDeclaredSubstatement(type).map(DeclaredStatement::argument);
+    }
+
+    /**
+     * Find all declared substatements of specified type and return them as a stream.
+     *
+     * @return A stream of all declared substatements of specified type.
+     * @throws NullPointerException if {@code type} is null
+     */
+    @Beta
+    default <T extends DeclaredStatement<?>> @NonNull Stream<T> streamDeclaredSubstatements(
+            @NonNull final Class<T> type) {
+        requireNonNull(type);
+        return declaredSubstatements().stream().filter(type::isInstance).map(type::cast);
+    }
 }