Introduce DataNodeContainer.findDataChildByName()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / CaseShorthandImpl.java
index ce54d65427ca47f3ccf9521e09e5a714ed4d4129..35b3a41be956f68c0d569a32afd014e058facb24 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
@@ -7,17 +7,18 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 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.Optional;
 import java.util.Set;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
@@ -30,31 +31,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();
-        this.path = Preconditions.checkNotNull(caseShorthandNode.getPath().getParent());
-        this.description = caseShorthandNode.getDescription();
-        this.reference = caseShorthandNode.getReference();
-        this.status = caseShorthandNode.getStatus();
 
+    CaseShorthandImpl(final DataSchemaNode caseShorthandNode) {
+        this.caseShorthandNode = requireNonNull(caseShorthandNode);
+        this.path = requireNonNull(caseShorthandNode.getPath().getParent());
+        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,102 +54,93 @@ 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();
     }
 
+    @Nonnull
     @Override
     public QName getQName() {
-        return qName;
+        return caseShorthandNode.getQName();
     }
 
+    @Nonnull
     @Override
     public SchemaPath getPath() {
         return path;
     }
 
+    @Nonnull
     @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();
     }
 
+    @Nonnull
     @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();
-    }
-
-    @Override
-    public DataSchemaNode getDataChildByName(final QName name) {
-        if (qName.equals(name)) {
-            return caseShorthandNode;
-        } else {
-            return null;
-        }
+        return ImmutableSet.of();
     }
 
     @Override
-    public DataSchemaNode getDataChildByName(final String name) {
-        if (qName.getLocalName().equals(name)) {
-            return caseShorthandNode;
-        } else {
-            return null;
-        }
+    public Optional<DataSchemaNode> findDataChildByName(final QName name) {
+        return name.equals(getQName()) ? Optional.of(caseShorthandNode) : Optional.empty();
     }
 
     @Override
     public Set<UsesNode> getUses() {
-        return Collections.emptySet();
+        return ImmutableSet.of();
     }
 
     @Override
-    public Set<AugmentationSchema> getAvailableAugmentations() {
-        return Collections.emptySet();
+    public Set<AugmentationSchemaNode> getAvailableAugmentations() {
+        return ImmutableSet.of();
     }
 
     @Override
     public Optional<? extends SchemaNode> getOriginal() {
-        return Optional.absent();
+        return Optional.ofNullable(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,16 +157,21 @@ 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
     public String toString() {
-        StringBuilder sb = new StringBuilder(CaseShorthandImpl.class.getSimpleName());
-        sb.append("[");
-        sb.append("qname=");
-        sb.append(qName);
-        sb.append("]");
-        return sb.toString();
+        return CaseShorthandImpl.class.getSimpleName() + "[" + "qname=" + getQName() + "]";
+    }
+
+    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;
     }
 }