Add QName.withModule(QNameModule) method 17/70817/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 11 Mar 2018 21:06:02 +0000 (22:06 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Apr 2018 09:23:20 +0000 (11:23 +0200)
In situations when a companion QName with specified QNameModule needs
to be created, we can side-step localName checking and just directly
reuse it. QName.withModule() does precisely that.

Change-Id: Ib1ca72210c0114610dafba0dfbbce15a2b8ddc03
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 010c1f2204d4f0408a10b81a51c14933522825ad)

yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java

index 29abd7880c4c85977e43e18e4f083d711bf7174a..08f5cdeeea855a298b82ad0c5c479660e6e1aaec 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.common;
 import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat;
 
 import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
 import com.google.common.collect.Interner;
 import com.google.common.collect.Interners;
 import java.io.Serializable;
@@ -70,7 +69,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     private static final Pattern QNAME_PATTERN_NO_NAMESPACE_NO_REVISION =
         Pattern.compile(QNAME_STRING_NO_NAMESPACE_NO_REVISION);
 
-    private static final char[] ILLEGAL_CHARACTERS = new char[] { '?', '(', ')', '&', ':' };
+    private static final char[] ILLEGAL_CHARACTERS = { '?', '(', ')', '&', ':' };
 
     // Non-null
     private final QNameModule module;
@@ -79,7 +78,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     private transient int hash;
 
     private QName(final QNameModule module, final String localName) {
-        this.localName = checkLocalName(localName);
+        this.localName = localName;
         this.module = module;
     }
 
@@ -92,18 +91,17 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      *            YANG schema identifier
      */
     public QName(final URI namespace, final String localName) {
-        this(QNameModule.create(namespace, null), localName);
+        this(QNameModule.create(namespace, null), checkLocalName(localName));
     }
 
     private static String checkLocalName(final String localName) {
         Preconditions.checkArgument(localName != null, "Parameter 'localName' may not be null.");
-        Preconditions.checkArgument(!Strings.isNullOrEmpty(localName),
-                "Parameter 'localName' must be a non-empty string.");
+        Preconditions.checkArgument(!localName.isEmpty(), "Parameter 'localName' must be a non-empty string.");
 
         for (final char c : ILLEGAL_CHARACTERS) {
             if (localName.indexOf(c) != -1) {
-                throw new IllegalArgumentException(String.format(
-                        "Parameter 'localName':'%s' contains illegal character '%s'", localName, c));
+                throw new IllegalArgumentException("Parameter 'localName':'" + localName
+                    + "' contains illegal character '" + c + "'");
             }
         }
         return localName;
@@ -145,7 +143,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      * @return Instance of QName
      */
     public static QName create(final QNameModule qnameModule, final String localName) {
-        return new QName(Preconditions.checkNotNull(qnameModule,"module may not be null"), localName);
+        return new QName(Preconditions.checkNotNull(qnameModule, "module may not be null"), checkLocalName(localName));
     }
 
     /**
@@ -311,7 +309,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         try {
             return new URI(namespace);
         } catch (final URISyntaxException ue) {
-            throw new IllegalArgumentException(String.format("Namespace '%s' is not a valid URI", namespace), ue);
+            throw new IllegalArgumentException("Namespace '" + namespace + "' is not a valid URI", ue);
         }
     }
 
@@ -347,11 +345,23 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
 
     /**
      * Creates copy of this with revision and prefix unset.
+     * Returns a QName with the specified QNameModule and the same localname as this one.
+     *
+     * @param newModule New QNameModule to use
+     * @return a QName with specified QNameModule and same local name as this one
+     */
+    public QName withModule(@Nonnull final QNameModule newModule) {
+        return new QName(Preconditions.checkNotNull(newModule), localName);
+    }
+
+    /**
+     * Returns a QName with the same namespace and local name, but with no revision. If this QName does not have
+     * a Revision, this object is retured.
      *
      * @return copy of this QName with revision and prefix unset.
      */
     public QName withoutRevision() {
-        return create(getNamespace(), null, localName);
+        return new QName(QNameModule.create(getNamespace(), null), localName);
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")