BUG-6497: Do not lose augmentation statement order
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / CaseShorthandImpl.java
index ce54d65427ca47f3ccf9521e09e5a714ed4d4129..6f19976571f76ce2235dfa91888c0fcaad84708f 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -10,9 +10,8 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
-import java.util.Arrays;
+import com.google.common.collect.ImmutableSet;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
@@ -30,31 +29,20 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
 
-public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
+final class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
 
     private final DataSchemaNode caseShorthandNode;
-    private final QName qName;
+    private final ChoiceCaseNode original;
     private final SchemaPath path;
-    private final String description;
-    private final String reference;
-    private final Status status;
     private final boolean augmenting;
-    private final boolean addedByUses;
-    private final ConstraintDefinition constraints;
-    private final List<UnknownSchemaNode> unknownNodes;
 
-    public CaseShorthandImpl(final DataSchemaNode caseShorthandNode) {
-        this.caseShorthandNode = caseShorthandNode;
-        this.qName = caseShorthandNode.getQName();
+    CaseShorthandImpl(final DataSchemaNode caseShorthandNode) {
+        this.caseShorthandNode = Preconditions.checkNotNull(caseShorthandNode);
         this.path = Preconditions.checkNotNull(caseShorthandNode.getPath().getParent());
-        this.description = caseShorthandNode.getDescription();
-        this.reference = caseShorthandNode.getReference();
-        this.status = caseShorthandNode.getStatus();
+        this.original = getOriginalIfPresent(caseShorthandNode);
 
+        // We need to cache this, as it will be reset
         this.augmenting = caseShorthandNode.isAugmenting();
-        this.addedByUses = caseShorthandNode.isAddedByUses();
-        this.constraints = caseShorthandNode.getConstraints();
-        this.unknownNodes = ImmutableList.copyOf(caseShorthandNode.getUnknownSchemaNodes());
     }
 
     @Override
@@ -64,22 +52,22 @@ public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
 
     @Override
     public boolean isAddedByUses() {
-        return addedByUses;
+        return caseShorthandNode.isAddedByUses();
     }
 
     @Override
     public boolean isConfiguration() {
-        return false;
+        return caseShorthandNode.isConfiguration();
     }
 
     @Override
     public ConstraintDefinition getConstraints() {
-        return constraints;
+        return caseShorthandNode.getConstraints();
     }
 
     @Override
     public QName getQName() {
-        return qName;
+        return caseShorthandNode.getQName();
     }
 
     @Override
@@ -89,42 +77,42 @@ public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
 
     @Override
     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-        return unknownNodes;
+        return ImmutableList.of();
     }
 
     @Override
     public String getDescription() {
-        return description;
+        return caseShorthandNode.getDescription();
     }
 
     @Override
     public String getReference() {
-        return reference;
+        return caseShorthandNode.getReference();
     }
 
     @Override
     public Status getStatus() {
-        return status;
+        return caseShorthandNode.getStatus();
     }
 
     @Override
     public Set<TypeDefinition<?>> getTypeDefinitions() {
-        return Collections.emptySet();
+        return ImmutableSet.of();
     }
 
     @Override
     public Collection<DataSchemaNode> getChildNodes() {
-        return Arrays.asList(caseShorthandNode);
+        return ImmutableList.of(caseShorthandNode);
     }
 
     @Override
     public Set<GroupingDefinition> getGroupings() {
-        return Collections.emptySet();
+        return ImmutableSet.of();
     }
 
     @Override
     public DataSchemaNode getDataChildByName(final QName name) {
-        if (qName.equals(name)) {
+        if (getQName().equals(name)) {
             return caseShorthandNode;
         } else {
             return null;
@@ -133,7 +121,7 @@ public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
 
     @Override
     public DataSchemaNode getDataChildByName(final String name) {
-        if (qName.getLocalName().equals(name)) {
+        if (getQName().getLocalName().equals(name)) {
             return caseShorthandNode;
         } else {
             return null;
@@ -142,24 +130,24 @@ public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
 
     @Override
     public Set<UsesNode> getUses() {
-        return Collections.emptySet();
+        return ImmutableSet.of();
     }
 
     @Override
     public Set<AugmentationSchema> getAvailableAugmentations() {
-        return Collections.emptySet();
+        return ImmutableSet.of();
     }
 
     @Override
     public Optional<? extends SchemaNode> getOriginal() {
-        return Optional.absent();
+        return Optional.fromNullable(original);
     }
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + Objects.hashCode(qName);
+        result = prime * result + Objects.hashCode(getQName());
         result = prime * result + Objects.hashCode(path);
         return result;
     }
@@ -176,7 +164,7 @@ public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
             return false;
         }
         CaseShorthandImpl other = (CaseShorthandImpl) obj;
-        return Objects.equals(qName, other.qName) && Objects.equals(path, other.path);
+        return Objects.equals(getQName(), other.getQName()) && Objects.equals(path, other.path);
     }
 
     @Override
@@ -184,8 +172,18 @@ public class CaseShorthandImpl implements ChoiceCaseNode, DerivableSchemaNode {
         StringBuilder sb = new StringBuilder(CaseShorthandImpl.class.getSimpleName());
         sb.append("[");
         sb.append("qname=");
-        sb.append(qName);
+        sb.append(getQName());
         sb.append("]");
         return sb.toString();
     }
+
+    private static ChoiceCaseNode getOriginalIfPresent(final SchemaNode caseShorthandNode) {
+        if (caseShorthandNode instanceof DerivableSchemaNode) {
+            final Optional<? extends SchemaNode> original = ((DerivableSchemaNode) caseShorthandNode).getOriginal();
+            if (original.isPresent()) {
+                return new CaseShorthandImpl((DataSchemaNode) original.get());
+            }
+        }
+        return null;
+    }
 }