Improve QNameModule.equals() performance
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QNameModule.java
index 22428e36d4b1afbef8e76ef22c090ca8a5d44f9b..a45c91b923b13c8ddb26fb5e81ad16d12d54f9cc 100644 (file)
@@ -16,9 +16,10 @@ import org.opendaylight.yangtools.concepts.Immutable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-final class QNameModule implements Immutable, Serializable {
-       private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
-       private static final long serialVersionUID = 1L;
+public final class QNameModule implements Immutable, Serializable {
+    private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
+    private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
+    private static final long serialVersionUID = 1L;
 
     //Nullable
     private final URI namespace;
@@ -27,39 +28,64 @@ final class QNameModule implements Immutable, Serializable {
     private final Date revision;
 
     //Nullable
-    private final String formattedRevision;
+    private String formattedRevision;
 
     private QNameModule(final URI namespace, final Date revision) {
         this.namespace = namespace;
         this.revision = revision;
-        if(revision != null) {
-            this.formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
-        } else {
-            this.formattedRevision = null;
+    }
+
+    public static QNameModule create(final URI namespace, final Date revision) {
+        if (namespace == null && revision == null) {
+            return NULL_INSTANCE;
         }
+
+        return new QNameModule(namespace, revision);
     }
 
-       public static QNameModule create(final URI namespace, final Date revision) {
-               return new QNameModule(namespace, revision);
-       }
+    public String getFormattedRevision() {
+        if (revision == null) {
+            return null;
+        }
 
-       public String getFormattedRevision() {
-               return formattedRevision;
-       }
+        if (formattedRevision == null) {
+            synchronized (this) {
+                if (formattedRevision == null) {
+                    formattedRevision = SimpleDateFormatUtil.getRevisionFormat().format(revision);
+                }
+            }
+        }
 
-       public URI getNamespace() {
-               return namespace;
-       }
+        return formattedRevision;
+    }
 
-       public Date getRevision() {
-               return revision;
-       }
+    /**
+     * Returns the namespace of the module which is specified as argument of
+     * YANG {@link Module <b><font color="#00FF00">namespace</font></b>}
+     * keyword.
+     *
+     * @return URI format of the namespace of the module
+     */
+    public URI getNamespace() {
+        return namespace;
+    }
+
+    /**
+     * Returns the revision date for the module.
+     *
+     * @return date of the module revision which is specified as argument of
+     *         YANG {@link Module <b><font color="#339900">revison</font></b>}
+     *         keyword
+     */
+    public Date getRevision() {
+        return revision;
+    }
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = (namespace == null) ? 0 : namespace.hashCode();
-        result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
+        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
         return result;
     }
 
@@ -72,18 +98,18 @@ final class QNameModule implements Immutable, Serializable {
             return false;
         }
         final QNameModule other = (QNameModule) obj;
-        if (namespace == null) {
-            if (other.namespace != null) {
+        if (revision == null) {
+            if (other.revision != null) {
                 return false;
             }
-        } else if (!namespace.equals(other.namespace)) {
+        } else if (!revision.equals(other.revision)) {
             return false;
         }
-        if (formattedRevision == null) {
-            if (other.formattedRevision != null) {
+        if (namespace == null) {
+            if (other.namespace != null) {
                 return false;
             }
-        } else if (!revision.equals(other.revision)) {
+        } else if (!namespace.equals(other.namespace)) {
             return false;
         }
         return true;
@@ -108,7 +134,7 @@ final class QNameModule implements Immutable, Serializable {
 
         String query = "";
         if (revision != null) {
-            query = "revision=" + formattedRevision;
+            query = "revision=" + getFormattedRevision();
         }
 
         URI compositeURI = null;