BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / UsesNodeBuilderImpl.java
index abc1fe767a1748ef8363d371590d54dc0785c852..6e4e2909524d7d7abdccfa432ed3893534b32707 100644 (file)
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
-
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.parser.builder.api.AbstractBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.RefineBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractBuilder;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
 public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNodeBuilder {
-    private boolean isBuilt;
     private UsesNodeImpl instance;
-    private DataNodeContainerBuilder parent;
-    private final String groupingName;
+    private DataNodeContainerBuilder parentBuilder;
+    private final SchemaPath targetGroupingPath;
     private SchemaPath groupingPath;
     private GroupingDefinition groupingDefinition;
     private GroupingBuilder groupingBuilder;
     private boolean addedByUses;
-    private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();
-    private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<SchemaNodeBuilder>();
-    private final List<RefineHolder> refines = new ArrayList<RefineHolder>();
-
-    private Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();
-    private Set<GroupingBuilder> targetGroupings = new HashSet<>();
-    private Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();
-    private List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();
-
-    private final boolean isCopy;
-    private boolean dataCollected;
-
-    @Override
-    public boolean isCopy() {
-        return isCopy;
-    }
-
-    @Override
-    public boolean isDataCollected() {
-        return dataCollected;
-    }
-
-    @Override
-    public void setDataCollected(boolean dataCollected) {
-        this.dataCollected = dataCollected;
-    }
-
-    public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {
-        super(moduleName, line);
-        this.groupingName = groupingName;
-        isCopy = false;
-    }
+    private boolean augmenting;
+    private boolean resolved;
+    private final Set<AugmentationSchemaBuilder> augmentationBuilders = new HashSet<>();
+    private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<>();
+    private final List<RefineBuilder> refines = new ArrayList<>();
 
-    public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName, final boolean isCopy) {
+    public UsesNodeBuilderImpl(final String moduleName, final int line, final SchemaPath targetGroupingPath) {
         super(moduleName, line);
-        this.groupingName = groupingName;
-        this.isCopy = isCopy;
+        this.targetGroupingPath = targetGroupingPath;
     }
 
     @Override
     public UsesNode build() {
-        if (!isBuilt) {
-            instance = new UsesNodeImpl(groupingPath);
-            instance.setAddedByUses(addedByUses);
+        if (instance != null) {
+            return instance;
+        }
 
-            // AUGMENTATIONS
-            final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
-            for (AugmentationSchemaBuilder builder : addedAugments) {
-                augments.add(builder.build());
-            }
-            instance.setAugmentations(augments);
+        instance = new UsesNodeImpl(groupingPath);
+        instance.setAddedByUses(addedByUses);
 
-            // REFINES
-            final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<SchemaPath, SchemaNode>();
-            for (SchemaNodeBuilder refineBuilder : refineBuilders) {
-                SchemaNode refineNode = refineBuilder.build();
-                refineNodes.put(refineNode.getPath(), refineNode);
+        // AUGMENTATIONS
+        final Set<AugmentationSchema> augments = new HashSet<>();
+        for (AugmentationSchemaBuilder builder : augmentationBuilders) {
+            if (!builder.isUnsupportedTarget()) {
+                augments.add(builder.build());
             }
-            instance.setRefines(refineNodes);
+        }
+        instance.augmentations = ImmutableSet.copyOf(augments);
 
-            // UNKNOWN NODES
-            List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
-            for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
-                unknownNodes.add(b.build());
-            }
-            instance.setUnknownSchemaNodes(unknownNodes);
+        // REFINES
+        final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<>();
+        for (SchemaNodeBuilder refineBuilder : refineBuilders) {
+            SchemaNode refineNode = refineBuilder.build();
+            refineNodes.put(refineNode.getPath(), refineNode);
+        }
+        instance.refines = ImmutableMap.copyOf(refineNodes);
 
-            isBuilt = true;
+        // UNKNOWN NODES
+        for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+            unknownNodes.add(b.build());
         }
+        instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
 
         return instance;
     }
 
     @Override
     public DataNodeContainerBuilder getParent() {
-        return parent;
+        return parentBuilder;
     }
 
     @Override
-    public void setParent(Builder parent) {
+    public void setParent(final Builder parent) {
         if (!(parent instanceof DataNodeContainerBuilder)) {
-            throw new YangParseException(moduleName, line,
+            throw new YangParseException(getModuleName(), getLine(),
                     "Parent of 'uses' has to be instance of DataNodeContainerBuilder, but was: '" + parent + "'.");
         }
-        this.parent = (DataNodeContainerBuilder) parent;
+        this.parentBuilder = (DataNodeContainerBuilder) parent;
     }
 
     @Override
@@ -140,7 +117,7 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
     }
 
     @Override
-    public void setGroupingDefinition(GroupingDefinition groupingDefinition) {
+    public void setGroupingDefinition(final GroupingDefinition groupingDefinition) {
         this.groupingDefinition = groupingDefinition;
         if (groupingDefinition != null) {
             this.groupingPath = groupingDefinition.getPath();
@@ -153,7 +130,7 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
     }
 
     @Override
-    public void setGrouping(GroupingBuilder grouping) {
+    public void setGrouping(final GroupingBuilder grouping) {
         this.groupingBuilder = grouping;
         if (groupingBuilder != null) {
             this.groupingPath = groupingBuilder.getPath();
@@ -161,18 +138,18 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
     }
 
     @Override
-    public String getGroupingPathAsString() {
-        return groupingName;
+    public SchemaPath getTargetGroupingPath() {
+        return targetGroupingPath;
     }
 
     @Override
     public Set<AugmentationSchemaBuilder> getAugmentations() {
-        return addedAugments;
+        return augmentationBuilders;
     }
 
     @Override
     public void addAugment(final AugmentationSchemaBuilder augmentBuilder) {
-        addedAugments.add(augmentBuilder);
+        augmentationBuilders.add(augmentBuilder);
     }
 
     @Override
@@ -186,77 +163,56 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
     }
 
     @Override
-    public List<SchemaNodeBuilder> getRefineNodes() {
-        return refineBuilders;
-    }
-
-    @Override
-    public void addRefineNode(DataSchemaNodeBuilder refineNode) {
-        refineBuilders.add(refineNode);
-    }
-
-    @Override
-    public List<RefineHolder> getRefines() {
-        return refines;
-    }
-
-    @Override
-    public void addRefine(RefineHolder refine) {
-        refines.add(refine);
-    }
-
-    @Override
-    public Set<DataSchemaNodeBuilder> getTargetChildren() {
-        return targetChildren;
+    public boolean isAugmenting() {
+        return augmenting;
     }
 
     @Override
-    public void setTargetChildren(Set<DataSchemaNodeBuilder> targetChildren) {
-        this.targetChildren = targetChildren;
+    public void setAugmenting(final boolean augmenting) {
+        this.augmenting = augmenting;
     }
 
     @Override
-    public Set<GroupingBuilder> getTargetGroupings() {
-        return targetGroupings;
+    public boolean isResolved() {
+        return resolved;
     }
 
     @Override
-    public void setTargetGroupings(Set<GroupingBuilder> targetGroupings) {
-        this.targetGroupings = targetGroupings;
+    public void setResolved(final boolean resolved) {
+        this.resolved = resolved;
     }
 
     @Override
-    public Set<TypeDefinitionBuilder> getTargetTypedefs() {
-        return targetTypedefs;
+    public List<SchemaNodeBuilder> getRefineNodes() {
+        return refineBuilders;
     }
 
     @Override
-    public void setTargetTypedefs(Set<TypeDefinitionBuilder> targetTypedefs) {
-        this.targetTypedefs = targetTypedefs;
+    public void addRefineNode(final DataSchemaNodeBuilder refineNode) {
+        refineBuilders.add(refineNode);
     }
 
     @Override
-    public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {
-        return targetUnknownNodes;
+    public List<RefineBuilder> getRefines() {
+        return refines;
     }
 
     @Override
-    public void setTargetUnknownNodes(List<UnknownSchemaNodeBuilder> targetUnknownNodes) {
-        this.targetUnknownNodes = targetUnknownNodes;
+    public void addRefine(final RefineBuilder refine) {
+        refines.add(refine);
     }
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((groupingName == null) ? 0 : groupingName.hashCode());
-        result = prime * result + ((parent == null) ? 0 : parent.hashCode());
-        result = prime * result + ((refines == null) ? 0 : refines.hashCode());
+        result = prime * result + Objects.hashCode(groupingPath);
+        result = prime * result + Objects.hashCode(parentBuilder);
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -267,25 +223,18 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
             return false;
         }
         UsesNodeBuilderImpl other = (UsesNodeBuilderImpl) obj;
-        if (groupingName == null) {
-            if (other.groupingName != null) {
+        if (groupingPath == null) {
+            if (other.groupingPath != null) {
                 return false;
             }
-        } else if (!groupingName.equals(other.groupingName)) {
+        } else if (!groupingPath.equals(other.groupingPath)) {
             return false;
         }
-        if (parent == null) {
-            if (other.parent != null) {
+        if (parentBuilder == null) {
+            if (other.parentBuilder != null) {
                 return false;
             }
-        } else if (!parent.equals(other.parent)) {
-            return false;
-        }
-        if (refines == null) {
-            if (other.refines != null) {
-                return false;
-            }
-        } else if (!refines.equals(other.refines)) {
+        } else if (!parentBuilder.equals(other.parentBuilder)) {
             return false;
         }
         return true;
@@ -293,121 +242,7 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
 
     @Override
     public String toString() {
-        return "uses '" + groupingName + "'";
-    }
-
-    public final class UsesNodeImpl implements UsesNode {
-        private final SchemaPath groupingPath;
-        private Set<AugmentationSchema> augmentations = Collections.emptySet();
-        private boolean addedByUses;
-        private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
-        private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
-
-        private UsesNodeImpl(final SchemaPath groupingPath) {
-            this.groupingPath = groupingPath;
-        }
-
-        @Override
-        public SchemaPath getGroupingPath() {
-            return groupingPath;
-        }
-
-        @Override
-        public Set<AugmentationSchema> getAugmentations() {
-            return augmentations;
-        }
-
-        private void setAugmentations(final Set<AugmentationSchema> augmentations) {
-            if (augmentations != null) {
-                this.augmentations = augmentations;
-            }
-        }
-
-        @Override
-        public boolean isAugmenting() {
-            return false;
-        }
-
-        @Override
-        public boolean isAddedByUses() {
-            return addedByUses;
-        }
-
-        private void setAddedByUses(final boolean addedByUses) {
-            this.addedByUses = addedByUses;
-        }
-
-        @Override
-        public Map<SchemaPath, SchemaNode> getRefines() {
-            return refines;
-        }
-
-        private void setRefines(Map<SchemaPath, SchemaNode> refines) {
-            if (refines != null) {
-                this.refines = refines;
-            }
-        }
-
-        public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-            return unknownNodes;
-        }
-
-        private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
-            if (unknownSchemaNodes != null) {
-                this.unknownNodes = unknownSchemaNodes;
-            }
-        }
-
-        public UsesNodeBuilder toBuilder() {
-            return UsesNodeBuilderImpl.this;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
-            result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            final UsesNodeImpl other = (UsesNodeImpl) obj;
-            if (groupingPath == null) {
-                if (other.groupingPath != null) {
-                    return false;
-                }
-            } else if (!groupingPath.equals(other.groupingPath)) {
-                return false;
-            }
-            if (augmentations == null) {
-                if (other.augmentations != null) {
-                    return false;
-                }
-            } else if (!augmentations.equals(other.augmentations)) {
-                return false;
-            }
-            return true;
-        }
-
-        @Override
-        public String toString() {
-            StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
-            sb.append("[groupingPath=");
-            sb.append(groupingPath);
-            sb.append("]");
-            return sb.toString();
-        }
+        return "uses '" + groupingPath + "'";
     }
 
 }