Fix StringStringCodec length check
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / ChoiceSchemaNode.java
index 4c243a971036f7c73369bb8b0df685ed69b2967d..6d7a0bd1d96682b61a480c7e2a59254691b5da72 100644 (file)
@@ -7,52 +7,85 @@
  */
 package org.opendaylight.yangtools.yang.model.api;
 
-import java.util.Set;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
 
 /**
- * A ChoiceSchemaNode defines a set of alternatives. It consists of a number of
- * branches defined as ChoiceCaseSchemaNode objects.
+ * A ChoiceSchemaNode defines a set of alternatives. It consists of a number of branches defined as
+ * ChoiceCaseSchemaNode objects.
  */
-public interface ChoiceSchemaNode extends DataSchemaNode, AugmentationTarget {
+public interface ChoiceSchemaNode extends DataSchemaNode, AugmentationTarget, MandatoryAware,
+        EffectiveStatementEquivalent<ChoiceEffectiveStatement> {
     /**
-     * Returns cases of choice.
+     * Returns cases of choice, keyed by their {@link SchemaNode#getQName()}. Returned map does not contain null keys
+     * nor values.
      *
-     * @return set of ChoiceCaseNode objects defined in this node which
-     *         represents set of arguments of the YANG <code>case</code>
-     *         substatement of the <code>choice</code> statement
+     * @return set of ChoiceCaseNode objects defined in this node which represents set of arguments of the YANG
+     *         <code>case</code> substatement of the <code>choice</code> statement.
      */
-    Set<ChoiceCaseNode> getCases();
+    Collection<? extends CaseSchemaNode> getCases();
 
     /**
-     *
      * Returns the concrete case according to specified Q name.
      *
-     * @param name
-     *            QName of seeked Choice Case Node
-     * @return child case node of this Choice if child with given name is
-     *         present, <code>null</code> otherwise
+     * @param qname
+     *            QName of sought Choice Case Node
+     * @return child case node of this Choice if child with given name is present, empty otherwise.
+     * @throws NullPointerException if qname is null
      */
-    ChoiceCaseNode getCaseNodeByName(QName name);
+    default Optional<? extends CaseSchemaNode> findCase(final QName qname) {
+        requireNonNull(qname);
+        return getCases().stream().filter(node -> qname.equals(node.getQName())).findFirst();
+    }
 
     /**
-     * Returns the concrete case according to specified name.
+     * Returns the concrete cases according to specified name, disregarding their namespace.
      *
-     * @param name
-     *            name of seeked child as String
-     * @return child case node (or local name of case node) of this Choice if
-     *         child with given name is present, <code>null</code> otherwise
+     * @param localname
+     *            local name of sought child as String
+     * @return child case nodes matching specified local name, empty list if no match is found.
+     * @throws NullPointerException if localname is null
      */
-    ChoiceCaseNode getCaseNodeByName(String name);
+    @Beta
+    default List<? extends CaseSchemaNode> findCaseNodes(final String localname) {
+        return getCases().stream().filter(node -> localname.equals(node.getQName().getLocalName()))
+                .collect(ImmutableList.toImmutableList());
+    }
 
     /**
+     * Find a specific data schema child, if present. This method searches among its {@link CaseSchemaNode}s,
+     * potentially recursing to nested choices.
      *
-     * Returns name of case which is in the choice specified as default
-     *
-     * @return string with the name of case which is specified in the argument
-     *         of the YANG <code>default</code> substatement of
-     *         <code>choice</code> statement.
+     * @param qname
+     *            QName of sought data schema node
+     * @return Matching node, or empty if no match is found
+     * @throws NullPointerException if qname is null
      */
-    String getDefaultCase();
+    @Beta
+    default Optional<DataSchemaNode> findDataSchemaChild(final QName qname) {
+        requireNonNull(qname);
+        for (CaseSchemaNode caseNode : getCases()) {
+            final Optional<DataSchemaNode> child = caseNode.findDataChildByName(qname);
+            if (child.isPresent()) {
+                return child;
+            }
+        }
+
+        return Optional.empty();
+    }
 
+    /**
+     * Returns name of case which is in the choice specified as default.
+     *
+     * @return string with the name of case which is specified in the argument of the YANG <code>default</code>
+     *         substatement of <code>choice</code> statement.
+     */
+    Optional<CaseSchemaNode> getDefaultCase();
 }