Merge "Fix a warning generic warning"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / NotificationBuilder.java
index 16eeacab9be53cca11708dbbbf4893ddd2ba8bc5..824bd1a262cc2cf8a1cef670ab5435a288de7252 100644 (file)
@@ -7,14 +7,11 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
+import java.net.URI;
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
+import java.util.Date;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
-import java.util.TreeMap;
-import java.util.TreeSet;
 
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
@@ -34,98 +31,105 @@ import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
 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.UsesNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.Comparators;
+import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
+import org.opendaylight.yangtools.yang.parser.util.YangParseException;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 
 public final class NotificationBuilder extends AbstractDataNodeContainerBuilder implements SchemaNodeBuilder,
         AugmentationTargetBuilder {
-    private boolean isBuilt;
-    private final NotificationDefinitionImpl instance;
+    private NotificationDefinitionImpl instance;
+    // SchemaNode args
     private SchemaPath schemaPath;
     private String description;
     private String reference;
     private Status status = Status.CURRENT;
-    private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
-    private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
-    private Set<AugmentationSchema> augmentations;
-    private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
+    // AugmentationTarget args
+    private final List<AugmentationSchema> augmentations = new ArrayList<>();
+    private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
+
+    NotificationBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
+        super(moduleName, line, qname);
+        this.schemaPath = path;
+    }
 
-    NotificationBuilder(final String moduleName, final int line, final QName qname) {
+    NotificationBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final NotificationDefinition base) {
         super(moduleName, line, qname);
-        instance = new NotificationDefinitionImpl(qname);
+        this.schemaPath = path;
+
+        description = base.getDescription();
+        reference = base.getReference();
+        status = base.getStatus();
+
+        URI ns = qname.getNamespace();
+        Date rev = qname.getRevision();
+        String pref = qname.getPrefix();
+        addedChildNodes.addAll(ParserUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
+        addedGroupings.addAll(ParserUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
+        addedTypedefs.addAll(ParserUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
+        addedUnknownNodes.addAll(ParserUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
+                rev, pref));
+
+        augmentations.addAll(base.getAvailableAugmentations());
+        usesNodes.addAll(base.getUses());
     }
 
     @Override
     public NotificationDefinition build() {
-        if (!isBuilt) {
-            // process uses
-            for(UsesNodeBuilder use : addedUsesNodes) {
-                addedChildNodes.addAll(use.getTargetChildren());
-                addedGroupings.addAll(use.getTargetGroupings());
-                addedTypedefs.addAll(use.getTargetTypedefs());
-                addedUnknownNodes.addAll(use.getTargetUnknownNodes());
-            }
+        if (!(parentBuilder instanceof ModuleBuilder)) {
+            throw new YangParseException(moduleName, line, "Notification can be defined only under module (was " + parentBuilder + ")");
+        }
+        if (instance != null) {
+            return instance;
+        }
 
-            instance.setPath(schemaPath);
-            instance.setDescription(description);
-            instance.setReference(reference);
-            instance.setStatus(status);
+        instance = new NotificationDefinitionImpl(qname, schemaPath);
 
-            // CHILD NODES
-            final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
-            for (DataSchemaNodeBuilder node : addedChildNodes) {
-                childs.put(node.getQName(), node.build());
-            }
-            instance.setChildNodes(childs);
+        instance.description = description;
+        instance.reference = reference;
+        instance.status = status;
 
-            // GROUPINGS
-            final Set<GroupingDefinition> groupingDefs = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
-            for (GroupingBuilder builder : addedGroupings) {
-                groupingDefs.add(builder.build());
-            }
-            instance.setGroupings(groupingDefs);
+        // CHILD NODES
+        for (DataSchemaNodeBuilder node : addedChildNodes) {
+            childNodes.add(node.build());
+        }
+        instance.childNodes = ImmutableSet.copyOf(childNodes);
 
-            // TYPEDEFS
-            final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
-            for (TypeDefinitionBuilder entry : addedTypedefs) {
-                typedefs.add(entry.build());
-            }
-            instance.setTypeDefinitions(typedefs);
+        // GROUPINGS
+        for (GroupingBuilder builder : addedGroupings) {
+            groupings.add(builder.build());
+        }
+        instance.groupings = ImmutableSet.copyOf(groupings);
 
-            // USES
-            final Set<UsesNode> uses = new HashSet<UsesNode>();
-            for (UsesNodeBuilder builder : addedUsesNodes) {
-                uses.add(builder.build());
-            }
-            instance.setUses(uses);
+        // TYPEDEFS
+        for (TypeDefinitionBuilder entry : addedTypedefs) {
+            typedefs.add(entry.build());
+        }
+        instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
 
-            // AUGMENTATIONS
-            if (augmentations == null) {
-                augmentations = new HashSet<AugmentationSchema>();
-                for (AugmentationSchemaBuilder builder : addedAugmentations) {
-                    augmentations.add(builder.build());
-                }
-            }
-            instance.setAvailableAugmentations(augmentations);
+        // USES
+        for (UsesNodeBuilder builder : addedUsesNodes) {
+            usesNodes.add(builder.build());
+        }
+        instance.uses = ImmutableSet.copyOf(usesNodes);
 
-            // UNKNOWN NODES
-            final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
-            for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
-                unknownNodes.add(b.build());
-            }
-            Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
-            instance.setUnknownSchemaNodes(unknownNodes);
+        // AUGMENTATIONS
+        for (AugmentationSchemaBuilder builder : augmentationBuilders) {
+            augmentations.add(builder.build());
+        }
+        instance.augmentations = ImmutableSet.copyOf(augmentations);
 
-            isBuilt = true;
+        // UNKNOWN NODES
+        for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+            unknownNodes.add(b.build());
         }
+        instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
 
         return instance;
     }
 
-    @Override
-    public void rebuild() {
-        isBuilt = false;
-        build();
-    }
 
     @Override
     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
@@ -137,19 +141,14 @@ public final class NotificationBuilder extends AbstractDataNodeContainerBuilder
         addedTypedefs.add(type);
     }
 
-    @Override
-    public void addUsesNode(final UsesNodeBuilder usesNodeBuilder) {
-        addedUsesNodes.add(usesNodeBuilder);
-    }
-
     @Override
     public SchemaPath getPath() {
         return schemaPath;
     }
 
     @Override
-    public void setPath(SchemaPath schemaPath) {
-        this.schemaPath = schemaPath;
+    public void setPath(SchemaPath path) {
+        this.schemaPath = path;
     }
 
     @Override
@@ -178,23 +177,13 @@ public final class NotificationBuilder extends AbstractDataNodeContainerBuilder
     }
 
     @Override
-    public void setStatus(final Status status) {
-        if (status != null) {
-            this.status = status;
-        }
-    }
-
-    public Set<AugmentationSchemaBuilder> getAugmentations() {
-        return addedAugmentations;
+    public void setStatus(Status status) {
+        this.status = Preconditions.checkNotNull(status, "status cannot be null");
     }
 
     @Override
-    public void addAugmentation(AugmentationSchemaBuilder augment) {
-        addedAugmentations.add(augment);
-    }
-
-    public void setAugmentations(final Set<AugmentationSchema> augmentations) {
-        this.augmentations = augmentations;
+    public void addAugmentation(final AugmentationSchemaBuilder augment) {
+        augmentationBuilders.add(augment);
     }
 
     @Override
@@ -202,21 +191,22 @@ public final class NotificationBuilder extends AbstractDataNodeContainerBuilder
         return "notification " + getQName().getLocalName();
     }
 
-    public final class NotificationDefinitionImpl implements NotificationDefinition {
+    private static final class NotificationDefinitionImpl implements NotificationDefinition {
         private final QName qname;
-        private SchemaPath path;
+        private final SchemaPath path;
         private String description;
         private String reference;
-        private Status status = Status.CURRENT;
-        private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
-        private Set<GroupingDefinition> groupings = Collections.emptySet();
-        private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
-        private Set<UsesNode> uses = Collections.emptySet();
-        private Set<AugmentationSchema> augmentations = Collections.emptySet();
-        private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
-
-        private NotificationDefinitionImpl(final QName qname) {
+        private Status status;
+        private ImmutableSet<AugmentationSchema> augmentations;
+        private ImmutableSet<DataSchemaNode> childNodes;
+        private ImmutableSet<GroupingDefinition> groupings;
+        private ImmutableSet<TypeDefinition<?>> typeDefinitions;
+        private ImmutableSet<UsesNode> uses;
+        private ImmutableList<UnknownSchemaNode> unknownNodes;
+
+        private NotificationDefinitionImpl(final QName qname, final SchemaPath path) {
             this.qname = qname;
+            this.path = path;
         }
 
         @Override
@@ -229,48 +219,24 @@ public final class NotificationBuilder extends AbstractDataNodeContainerBuilder
             return path;
         }
 
-        private void setPath(final SchemaPath path) {
-            this.path = path;
-        }
-
         @Override
         public String getDescription() {
             return description;
         }
 
-        private void setDescription(final String description) {
-            this.description = description;
-        }
-
         @Override
         public String getReference() {
             return reference;
         }
 
-        private void setReference(String reference) {
-            this.reference = reference;
-        }
-
         @Override
         public Status getStatus() {
             return status;
         }
 
-        private void setStatus(Status status) {
-            if (status != null) {
-                this.status = status;
-            }
-        }
-
         @Override
         public Set<DataSchemaNode> getChildNodes() {
-            return new HashSet<DataSchemaNode>(childNodes.values());
-        }
-
-        private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
-            if (childNodes != null) {
-                this.childNodes = childNodes;
-            }
+            return childNodes;
         }
 
         @Override
@@ -278,75 +244,34 @@ public final class NotificationBuilder extends AbstractDataNodeContainerBuilder
             return groupings;
         }
 
-        private void setGroupings(Set<GroupingDefinition> groupings) {
-            if (groupings != null) {
-                this.groupings = groupings;
-            }
-        }
-
         @Override
         public Set<UsesNode> getUses() {
             return uses;
         }
 
-        private void setUses(Set<UsesNode> uses) {
-            if (uses != null) {
-                this.uses = uses;
-            }
-        }
-
         @Override
         public Set<TypeDefinition<?>> getTypeDefinitions() {
             return typeDefinitions;
         }
 
-        private void setTypeDefinitions(final Set<TypeDefinition<?>> typeDefinitions) {
-            if (typeDefinitions != null) {
-                this.typeDefinitions = typeDefinitions;
-            }
-        }
-
         @Override
         public Set<AugmentationSchema> getAvailableAugmentations() {
             return augmentations;
         }
 
-        private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
-            if (augmentations != null) {
-                this.augmentations = augmentations;
-            }
-        }
-
         @Override
         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
             return unknownNodes;
         }
 
-        private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
-            if (unknownNodes != null) {
-                this.unknownNodes = unknownNodes;
-            }
-        }
-
         @Override
-        public DataSchemaNode getDataChildByName(QName name) {
-            return childNodes.get(name);
+        public DataSchemaNode getDataChildByName(final QName name) {
+            return getChildNode(childNodes, name);
         }
 
         @Override
-        public DataSchemaNode getDataChildByName(String name) {
-            DataSchemaNode result = null;
-            for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
-                if (entry.getKey().getLocalName().equals(name)) {
-                    result = entry.getValue();
-                    break;
-                }
-            }
-            return result;
-        }
-
-        public NotificationBuilder toBuilder() {
-            return NotificationBuilder.this;
+        public DataSchemaNode getDataChildByName(final String name) {
+            return getChildNode(childNodes, name);
         }
 
         @Override
@@ -359,7 +284,7 @@ public final class NotificationBuilder extends AbstractDataNodeContainerBuilder
         }
 
         @Override
-        public boolean equals(Object obj) {
+        public boolean equals(final Object obj) {
             if (this == obj) {
                 return true;
             }