BUG-994: Create QNameModule for resource sharing 25/6925/12
authorRobert Varga <rovarga@cisco.com>
Tue, 13 May 2014 22:26:20 +0000 (00:26 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 19 May 2014 05:39:55 +0000 (07:39 +0200)
This splits out the per-module invariants out of QName into a separate
class, QNameModule. This simplifies the QName logic quite a bit and
allows sharing of QNameModule in the future.

As a cleanup we get rid of static imports and improve exception texts.

Change-Id: Ic2651fe12f1982360afcfae0549cdee426de1556
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java [new file with mode: 0644]

index a3bcbfd453e37be86ae800f1bccecd0c96068b7f..09a2625697526410a40daac992aeca7f9f06768c 100644 (file)
@@ -19,8 +19,6 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.opendaylight.yangtools.concepts.Immutable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * The QName from XML consists of local name of element and XML namespace, but
@@ -44,7 +42,6 @@ import org.slf4j.LoggerFactory;
  */
 public final class QName implements Immutable, Serializable, Comparable<QName> {
     private static final long serialVersionUID = 5398411242927766414L;
-    private static final Logger LOGGER = LoggerFactory.getLogger(QName.class);
 
     static final String QNAME_REVISION_DELIMITER = "?revision=";
     static final String QNAME_LEFT_PARENTHESIS = "(";
@@ -59,16 +56,12 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
 
     private static final char[] ILLEGAL_CHARACTERS = new char[] {'?', '(', ')', '&'};
 
-    //Nullable
-    private final URI namespace;
+    //Mandatory
+    private final QNameModule module;
     //Mandatory
     private final String localName;
     //Nullable
     private final String prefix;
-    //Nullable
-    private final String formattedRevision;
-    //Nullable
-    private final Date revision;
 
     /**
      * QName Constructor.
@@ -84,14 +77,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      */
     public QName(final URI namespace, final Date revision, final String prefix, final String localName) {
         this.localName = checkLocalName(localName);
-        this.namespace = namespace;
-        this.revision = revision;
         this.prefix = prefix;
-        if(revision != null) {
-            this.formattedRevision = getRevisionFormat().format(revision);
-        } else {
-            this.formattedRevision = null;
-        }
+        this.module = QNameModule.create(namespace, revision);
     }
 
     /**
@@ -148,24 +135,21 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      */
     @Deprecated
     public QName(final String input) throws ParseException {
-        Date revision = null;
-        String nsAndRev = input.substring(input.indexOf("(") + 1, input.indexOf(")"));
+        final String nsAndRev = input.substring(input.indexOf("(") + 1, input.indexOf(")"));
+        final Date revision;
+        final URI namespace;
         if (nsAndRev.contains("?")) {
             String[] splitted = nsAndRev.split("\\?");
-            this.namespace = URI.create(splitted[0]);
+            namespace = URI.create(splitted[0]);
             revision = getRevisionFormat().parse(splitted[1]);
         } else {
-            this.namespace = URI.create(nsAndRev);
+            namespace = URI.create(nsAndRev);
+            revision = null;
         }
 
         this.localName = checkLocalName(input.substring(input.indexOf(")") + 1));
-        this.revision = revision;
         this.prefix = null;
-        if (revision != null) {
-            this.formattedRevision = getRevisionFormat().format(revision);
-        } else {
-            this.formattedRevision = null;
-        }
+        this.module = QNameModule.create(namespace, revision);
     }
 
     public static QName create(final String input) {
@@ -196,7 +180,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      * @return XMLNamespace assigned to the YANG module.
      */
     public URI getNamespace() {
-        return namespace;
+        return module.getNamespace();
     }
 
     /**
@@ -218,7 +202,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      *         otherwise returns <code>null</code>
      */
     public Date getRevision() {
-        return revision;
+        return module.getRevision();
     }
 
     /**
@@ -235,8 +219,8 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         final int prime = 31;
         int result = 1;
         result = prime * result + ((localName == null) ? 0 : localName.hashCode());
-        result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
-        result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
+        result = prime * result + ((getNamespace() == null) ? 0 : getNamespace().hashCode());
+        result = prime * result + ((getFormattedRevision() == null) ? 0 : getFormattedRevision().hashCode());
         return result;
     }
 
@@ -245,13 +229,10 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         if (this == obj) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
+        if (!(obj instanceof QName)) {
             return false;
         }
-        QName other = (QName) obj;
+        final QName other = (QName) obj;
         if (localName == null) {
             if (other.localName != null) {
                 return false;
@@ -259,24 +240,23 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         } else if (!localName.equals(other.localName)) {
             return false;
         }
-        if (namespace == null) {
-            if (other.namespace != null) {
+        if (getNamespace() == null) {
+            if (other.getNamespace() != null) {
                 return false;
             }
-        } else if (!namespace.equals(other.namespace)) {
+        } else if (!getNamespace().equals(other.getNamespace())) {
             return false;
         }
-        if (formattedRevision == null) {
-            if (other.formattedRevision != null) {
+        if (getFormattedRevision() == null) {
+            if (other.getFormattedRevision() != null) {
                 return false;
             }
-        } else if (!revision.equals(other.revision)) {
+        } else if (!getRevision().equals(other.getRevision())) {
             return false;
         }
         return true;
     }
 
-
     public static QName create(final QName base, final String localName){
         return new QName(base, localName);
     }
@@ -287,23 +267,25 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
 
 
     public static QName create(final String namespace, final String revision, final String localName) throws IllegalArgumentException{
+        final URI namespaceUri;
         try {
-            URI namespaceUri = new URI(namespace);
-            Date revisionDate = parseRevision(revision);
-            return create(namespaceUri, revisionDate, localName);
+            namespaceUri = new URI(namespace);
         }  catch (URISyntaxException ue) {
-            throw new IllegalArgumentException("Namespace is is not valid URI", ue);
+            throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
         }
+
+        Date revisionDate = parseRevision(revision);
+        return create(namespaceUri, revisionDate, localName);
     }
 
     @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
-        if (namespace != null) {
-            sb.append(QNAME_LEFT_PARENTHESIS + namespace);
+        if (getNamespace() != null) {
+            sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
 
-            if (formattedRevision != null) {
-                sb.append(QNAME_REVISION_DELIMITER + formattedRevision);
+            if (getFormattedRevision() != null) {
+                sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
             }
             sb.append(QNAME_RIGHT_PARENTHESIS);
         }
@@ -311,51 +293,19 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         return sb.toString();
     }
 
-    /**
-     * Returns a namespace in form defined by section 5.6.4. of {@link https
-     * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
-     * the method will return <code>null</code> <br>
-     * example "http://example.acme.com/system?revision=2008-04-01"
-     *
-     * @return namespace in form defined by section 5.6.4. of {@link https
-     *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
-     *         defined, the method will return <code>null</code>
-     *
-     */
-    URI getRevisionNamespace() {
-
-        if (namespace == null) {
-            return null;
-        }
-
-        String query = "";
-        if (revision != null) {
-            query = "revision=" + formattedRevision;
-        }
-
-        URI compositeURI = null;
-        try {
-            compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
-                    namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
-        } catch (URISyntaxException e) {
-            LOGGER.error("", e);
-        }
-        return compositeURI;
-    }
-
     public String getFormattedRevision() {
-        return formattedRevision;
+        return module.getFormattedRevision();
     }
 
     public QName withoutRevision() {
-        return QName.create(namespace, null, localName);
+        return QName.create(getNamespace(), null, localName);
     }
 
     public static Date parseRevision(final String formatedDate) {
         try {
             return getRevisionFormat().parse(formatedDate);
         } catch (ParseException| RuntimeException e) {
-            throw new IllegalArgumentException("Revision is not in supported format:" + formatedDate,e);
+            throw new IllegalArgumentException(String.format("Revision '%s'is not in a supported format", formatedDate), e);
         }
     }
 
@@ -367,7 +317,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     }
 
     public boolean isEqualWithoutRevision(final QName other) {
-        return localName.equals(other.getLocalName()) && Objects.equals(namespace, other.getNamespace());
+        return localName.equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
     }
 
     @Override
@@ -379,30 +329,30 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         }
 
         // compare nullable namespace parameter
-        if (namespace == null) {
-            if (other.namespace != null) {
+        if (getNamespace() == null) {
+            if (other.getNamespace() != null) {
                 return -1;
             }
         } else {
-            if (other.namespace == null) {
+            if (other.getNamespace() == null) {
                 return 1;
             }
-            result = namespace.compareTo(other.namespace);
+            result = getNamespace().compareTo(other.getNamespace());
             if (result != 0) {
                 return result;
             }
         }
 
         // compare nullable revision parameter
-        if (revision == null) {
-            if (other.revision != null) {
+        if (getRevision() == null) {
+            if (other.getRevision() != null) {
                 return -1;
             }
         } else {
-            if (other.revision == null) {
+            if (other.getRevision() == null) {
                 return 1;
             }
-            result = revision.compareTo(other.revision);
+            result = getRevision().compareTo(other.getRevision());
             if (result != 0) {
                 return result;
             }
diff --git a/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java b/yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QNameModule.java
new file mode 100644 (file)
index 0000000..22428e3
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2013 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.common;
+
+import java.io.Serializable;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+
+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;
+
+    //Nullable
+    private final URI namespace;
+
+    //Nullable
+    private final Date revision;
+
+    //Nullable
+    private final 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) {
+               return new QNameModule(namespace, revision);
+       }
+
+       public String getFormattedRevision() {
+               return formattedRevision;
+       }
+
+       public URI getNamespace() {
+               return namespace;
+       }
+
+       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());
+        return result;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof QNameModule)) {
+            return false;
+        }
+        final QNameModule other = (QNameModule) obj;
+        if (namespace == null) {
+            if (other.namespace != null) {
+                return false;
+            }
+        } else if (!namespace.equals(other.namespace)) {
+            return false;
+        }
+        if (formattedRevision == null) {
+            if (other.formattedRevision != null) {
+                return false;
+            }
+        } else if (!revision.equals(other.revision)) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Returns a namespace in form defined by section 5.6.4. of {@link https
+     * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,
+     * the method will return <code>null</code> <br>
+     * example "http://example.acme.com/system?revision=2008-04-01"
+     *
+     * @return namespace in form defined by section 5.6.4. of {@link https
+     *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly
+     *         defined, the method will return <code>null</code>
+     *
+     */
+    URI getRevisionNamespace() {
+
+        if (namespace == null) {
+            return null;
+        }
+
+        String query = "";
+        if (revision != null) {
+            query = "revision=" + formattedRevision;
+        }
+
+        URI compositeURI = null;
+        try {
+            compositeURI = new URI(namespace.getScheme(), namespace.getUserInfo(), namespace.getHost(),
+                    namespace.getPort(), namespace.getPath(), query, namespace.getFragment());
+        } catch (URISyntaxException e) {
+            LOG.error("", e);
+        }
+        return compositeURI;
+    }
+}