Bug 5335: augmenting a mandatory node on a presence container
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ModuleImpl.java
index 7adca41d193a100d56e38a05d10e57b2faf2a310..fa4a8ae25aef164cf528413466411059d6852981 100644 (file)
@@ -1,15 +1,26 @@
+/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
-import com.google.common.base.Optional;
+import static com.google.common.base.Preconditions.checkNotNull;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.net.URI;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.NavigableSet;
+import java.util.Objects;
 import java.util.Set;
 import java.util.TreeSet;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
@@ -24,17 +35,21 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
 
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
 public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implements Module, Immutable {
 
-    private final URI namespace;
+    private final QNameModule qnameModule;
     private final String name;
     private final String sourcePath;
-    private final Optional<Date> revision;
     private final String prefix;
     private final String yangVersion;
     private final String organization;
     private final String contact;
     private final Set<ModuleImport> imports;
+    private final Set<Module> submodules;
     private final Set<FeatureDefinition> features;
     private final Set<NotificationDefinition> notifications;
     private final Set<AugmentationSchema> augmentations;
@@ -49,7 +64,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
      *
      *
      * <b>Note</b>Constructor has intentionality limited visibility to package, since
-     * this class should be only instantied via builders.
+     * this class should be only instantiated via builders.
      *
      * @param name
      * @param sourcePath
@@ -57,13 +72,14 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
      */
     ModuleImpl(final String name, final String sourcePath, final ModuleBuilder builder) {
         super(builder);
-        this.name = name;
-        this.sourcePath = sourcePath;
-        this.imports = ImmutableSet.<ModuleImport> copyOf(builder.imports);
-        this.namespace = builder.getNamespace();
+        this.name = checkNotNull(name, "Missing name");
+        this.sourcePath = sourcePath; //TODO: can this be nullable?
+        this.imports = ImmutableSet.<ModuleImport> copyOf(builder.imports.values());
+        this.submodules = ImmutableSet.<Module> copyOf(builder.submodules);
         this.prefix = builder.getPrefix();
-        this.revision = builder.getRevision() == null ? Optional.<Date>absent():
-                Optional.of(new Date(builder.getRevision().getTime()));
+
+        this.qnameModule = QNameModule.create(builder.getNamespace(),
+                builder.getRevision() == null ? null : new Date(builder.getRevision().getTime())).intern();
         this.yangVersion = builder.getYangVersion();
         this.organization = builder.getOrganization();
         this.contact = builder.getContact();
@@ -75,7 +91,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
         this.extensionNodes = ImmutableList.copyOf(builder.getExtensions());
         this.identities = ImmutableSet.copyOf(builder.getIdentities());
         this.unknownNodes = ImmutableList.copyOf(builder.getExtensionInstances());
-        this.source = builder.source;
+        this.source = checkNotNull(builder.getSource(), "Missing source");
 
     }
 
@@ -86,7 +102,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
 
     @Override
     public URI getNamespace() {
-        return namespace;
+        return qnameModule.getNamespace();
     }
 
     @Override
@@ -96,11 +112,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
 
     @Override
     public Date getRevision() {
-        if (revision.isPresent()) {
-            return new Date(revision.get().getTime());
-        } else {
-            return null;
-        }
+        return qnameModule.getRevision();
     }
 
     @Override
@@ -128,6 +140,11 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
         return imports;
     }
 
+    @Override
+    public Set<Module> getSubmodules() {
+        return submodules;
+    }
+
     @Override
     public Set<FeatureDefinition> getFeatures() {
         return features;
@@ -168,6 +185,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
         return unknownNodes;
     }
 
+    @Override
     public String getSource() {
         return source;
     }
@@ -176,10 +194,9 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
-        result = prime * result + ((name == null) ? 0 : name.hashCode());
-        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
-        result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
+        result = prime * result + Objects.hashCode(name);
+        result = prime * result + Objects.hashCode(yangVersion);
+        result = prime * result + qnameModule.hashCode();
         return result;
     }
 
@@ -195,13 +212,6 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
             return false;
         }
         ModuleImpl other = (ModuleImpl) obj;
-        if (namespace == null) {
-            if (other.namespace != null) {
-                return false;
-            }
-        } else if (!namespace.equals(other.namespace)) {
-            return false;
-        }
         if (name == null) {
             if (other.name != null) {
                 return false;
@@ -209,11 +219,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
         } else if (!name.equals(other.name)) {
             return false;
         }
-        if (revision == null) {
-            if (other.revision != null) {
-                return false;
-            }
-        } else if (!revision.equals(other.revision)) {
+        if (!qnameModule.equals(other.qnameModule)) {
             return false;
         }
         if (yangVersion == null) {
@@ -227,7 +233,7 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
     }
 
     private static <T extends SchemaNode> Set<T> toImmutableSortedSet(final Set<T> original) {
-        TreeSet<T> sorted = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+        NavigableSet<T> sorted = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
         sorted.addAll(original);
         return Collections.unmodifiableSet(sorted);
     }
@@ -236,12 +242,17 @@ public final class ModuleImpl extends AbstractDocumentedDataNodeContainer implem
     public String toString() {
         StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
         sb.append("[");
-        sb.append("name=" + name);
-        sb.append(", namespace=" + namespace);
-        sb.append(", revision=" + revision);
-        sb.append(", prefix=" + prefix);
-        sb.append(", yangVersion=" + yangVersion);
+        sb.append("name=").append(name);
+        sb.append(", namespace=").append(getNamespace());
+        sb.append(", revision=").append(getRevision());
+        sb.append(", prefix=").append(prefix);
+        sb.append(", yangVersion=").append(yangVersion);
         sb.append("]");
         return sb.toString();
     }
-}
\ No newline at end of file
+
+    @Override
+    public QNameModule getQNameModule() {
+        return qnameModule;
+    }
+}