BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ListSchemaNodeBuilder.java
index 994a979b20f1bf2a8822a0b58d9aeb840cf504d2..dbd0951cde02edbe761f0e420261daad2b1d3788 100644 (file)
@@ -7,36 +7,37 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
         DataSchemaNodeBuilder, AugmentationTargetBuilder {
     private ListSchemaNodeImpl instance;
     private boolean userOrdered;
-    private List<String> keys;
+    private Set<String> keys;
     private List<QName> keyDefinition;
     // SchemaNode args
     private SchemaPath schemaPath;
@@ -87,13 +88,11 @@ public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeConta
         instance.augmenting = augmenting;
         instance.addedByUses = addedByUses;
         instance.configuration = configuration;
-        instance.constraints = constraints.toInstance();
+        instance.constraints = constraints.build();
         instance.userOrdered = userOrdered;
 
         // KEY
-        if (keys == null) {
-            instance.keyDefinition = ImmutableList.of();
-        } else {
+        if (keys != null) {
             keyDefinition = new ArrayList<>();
             for (String key : keys) {
                 DataSchemaNode keyPart = instance.getDataChildByName(key);
@@ -101,9 +100,20 @@ public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeConta
                     throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
                             + key);
                 }
-                keyDefinition.add(keyPart.getQName());
+
+                if (!(keyPart instanceof LeafSchemaNode)) {
+                    throw new YangParseException(getModuleName(), getLine(), "List key : \"" + key
+                            + "\" does not reference any Leaf of the List");
+                }
+
+                final QName qname = keyPart.getQName();
+                if (!keyDefinition.contains(qname)) {
+                    keyDefinition.add(qname);
+                }
             }
             instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
+        } else {
+            instance.keyDefinition = ImmutableList.of();
         }
 
         // ORIGINAL NODE
@@ -151,11 +161,11 @@ public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeConta
         return augmentationBuilders;
     }
 
-    public List<String> getKeys() {
+    public Set<String> getKeys() {
         return keys;
     }
 
-    public void setKeys(final List<String> keys) {
+    public void setKeys(final Set<String> keys) {
         this.keys = keys;
     }
 
@@ -217,7 +227,7 @@ public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeConta
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
+        result = prime * result + Objects.hashCode(schemaPath);
         return result;
     }
 
@@ -255,123 +265,4 @@ public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeConta
         return "list " + qname.getLocalName();
     }
 
-    private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
-            ListSchemaNode, DerivableSchemaNode {
-        private final QName qname;
-        private final SchemaPath path;
-        private ImmutableList<QName> keyDefinition;
-        private boolean augmenting;
-        private boolean addedByUses;
-        private ListSchemaNode original;
-        private boolean configuration;
-        private ConstraintDefinition constraints;
-        private ImmutableSet<AugmentationSchema> augmentations;
-        private ImmutableList<UnknownSchemaNode> unknownNodes;
-        private boolean userOrdered;
-
-        private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
-            super(builder);
-            this.qname = qname;
-            this.path = path;
-        }
-
-        @Override
-        public QName getQName() {
-            return qname;
-        }
-
-        @Override
-        public SchemaPath getPath() {
-            return path;
-        }
-
-        @Override
-        public List<QName> getKeyDefinition() {
-            return keyDefinition;
-        }
-
-        @Override
-        public boolean isAugmenting() {
-            return augmenting;
-        }
-
-        @Override
-        public boolean isAddedByUses() {
-            return addedByUses;
-        }
-
-        @Override
-        public Optional<ListSchemaNode> getOriginal() {
-            return Optional.fromNullable(original);
-        }
-
-        @Override
-        public boolean isConfiguration() {
-            return configuration;
-        }
-
-        @Override
-        public ConstraintDefinition getConstraints() {
-            return constraints;
-        }
-
-        @Override
-        public Set<AugmentationSchema> getAvailableAugmentations() {
-            return augmentations;
-        }
-
-        @Override
-        public boolean isUserOrdered() {
-            return userOrdered;
-        }
-
-        @Override
-        public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-            return unknownNodes;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((qname == null) ? 0 : qname.hashCode());
-            result = prime * result + ((path == null) ? 0 : path.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(final Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
-            if (qname == null) {
-                if (other.qname != null) {
-                    return false;
-                }
-            } else if (!qname.equals(other.qname)) {
-                return false;
-            }
-            if (path == null) {
-                if (other.path != null) {
-                    return false;
-                }
-            } else if (!path.equals(other.path)) {
-                return false;
-            }
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            return "list " + qname.getLocalName();
-        }
-    }
-
 }