Update ChoiceSchemaNode design
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextNode.java
index 1d8e133c79deaad16d9117cfe9cb1c8d44b50214..336ab56e942aa3857dd8a36267d0244f449def52 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.yangtools.yang.data.util;
 
-
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableSet;
 import java.util.Collections;
@@ -20,7 +19,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
@@ -54,7 +53,6 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
     }
 
     protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
-        super();
         this.identifier = identifier;
         if (schema instanceof DataSchemaNode) {
             this.dataSchemaNode = (DataSchemaNode) schema;
@@ -75,8 +73,8 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
         return Collections.singleton(identifier.getNodeType());
     }
 
-    @Nullable public abstract DataSchemaContextNode<?> getChild(final PathArgument child);
-              
+    @Nullable public abstract DataSchemaContextNode<?> getChild(PathArgument child);
+
     @Nullable public abstract DataSchemaContextNode<?> getChild(QName child);
 
     public abstract boolean isLeaf();
@@ -86,7 +84,7 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
         return dataSchemaNode;
     }
 
-    static final DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
+    static DataSchemaNode findChildSchemaNode(final DataNodeContainer parent, final QName child) {
         DataSchemaNode potential = parent.getDataChildByName(child);
         if (potential == null) {
             Iterable<ChoiceSchemaNode> choices = FluentIterable.from(
@@ -99,26 +97,26 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
     static DataSchemaContextNode<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
         DataSchemaNode result = findChildSchemaNode(schema, child);
         // We try to look up if this node was added by augmentation
-        if ((schema instanceof DataSchemaNode) && result.isAugmenting()) {
+        if (result != null && schema instanceof DataSchemaNode && result.isAugmenting()) {
             return fromAugmentation(schema, (AugmentationTarget) schema, result);
         }
         return fromDataSchemaNode(result);
     }
 
+    // FIXME: this looks like it should be a Predicate on a stream with findFirst()
     private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
-        ChoiceSchemaNode foundChoice = null;
-        choiceLoop: for (ChoiceSchemaNode choice : choices) {
-            for (ChoiceCaseNode caze : choice.getCases()) {
+        for (ChoiceSchemaNode choice : choices) {
+            // FIXME: this looks weird: what are we looking for again?
+            for (ChoiceCaseNode caze : choice.getCases().values()) {
                 if (findChildSchemaNode(caze, child) != null) {
-                    foundChoice = choice;
-                    break choiceLoop;
+                    return choice;
                 }
             }
         }
-        return foundChoice;
+        return null;
     }
 
-    public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchema augmentation) {
+    public static AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode augmentation) {
         ImmutableSet.Builder<QName> potentialChildren = ImmutableSet.builder();
         for (DataSchemaNode child : augmentation.getChildNodes()) {
             potentialChildren.add(child.getQName());
@@ -126,7 +124,7 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
         return new AugmentationIdentifier(potentialChildren.build());
     }
 
-    static DataNodeContainer augmentationProxy(final AugmentationSchema augmentation,
+    static DataNodeContainer augmentationProxy(final AugmentationSchemaNode augmentation,
             final DataNodeContainer schema) {
         Set<DataSchemaNode> children = new HashSet<>();
         for (DataSchemaNode augNode : augmentation.getChildNodes()) {
@@ -138,21 +136,16 @@ public abstract class DataSchemaContextNode<T extends PathArgument> implements I
     /**
      * Returns a DataContextNodeOperation for provided child node
      *
+     * <p>
      * If supplied child is added by Augmentation this operation returns a
      * DataContextNodeOperation for augmentation, otherwise returns a
      * DataContextNodeOperation for child as call for
      * {@link #fromDataSchemaNode(DataSchemaNode)}.
-     *
-     *
-     * @param parent
-     * @param parentAug
-     * @param child
-     * @return
      */
     @Nullable static DataSchemaContextNode<?> fromAugmentation(final DataNodeContainer parent,
             final AugmentationTarget parentAug, final DataSchemaNode child) {
-        AugmentationSchema augmentation = null;
-        for (AugmentationSchema aug : parentAug.getAvailableAugmentations()) {
+        AugmentationSchemaNode augmentation = null;
+        for (AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) {
             DataSchemaNode potential = aug.getDataChildByName(child.getQName());
             if (potential != null) {
                 augmentation = aug;