Use String concatenation instead of StringBuffer/Builder
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / QName.java
index 25f6d30b99fb28e96018230ef00915cba7bba21a..c1bd85d23591f43f4a5c91491e0e566cac731fff 100644 (file)
@@ -8,8 +8,9 @@
 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.collect.Interner;
+import com.google.common.collect.Interners;
 import java.io.Serializable;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -19,8 +20,6 @@ import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.objcache.ObjectCache;
-import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
 
 /**
  * The QName from XML consists of local name of element and XML namespace, but
@@ -47,7 +46,7 @@ import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
  *
  */
 public final class QName implements Immutable, Serializable, Comparable<QName> {
-    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QName.class);
+    private static final Interner<QName> INTERNER = Interners.newWeakInterner();
     private static final long serialVersionUID = 5398411242927766414L;
 
     static final String QNAME_REVISION_DELIMITER = "?revision=";
@@ -74,21 +73,13 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      * Look up specified QName in the global cache and return a shared reference.
      *
      * @param qname QName instance
-     * @return Cached instance, according to {@link ObjectCache} policy.
+     * @return Cached instance, according to {@link org.opendaylight.yangtools.objcache.ObjectCache} policy.
+     *
+     * @deprecated Use {@link #intern()} instead.
      */
+    @Deprecated
     public static QName cachedReference(final QName qname) {
-        // We also want to make sure we keep the QNameModule cached
-        final QNameModule myMod = qname.getModule();
-        final QNameModule cacheMod = QNameModule.cachedReference(myMod);
-
-        final QName what;
-        if (cacheMod.equals(myMod)) {
-            what = qname;
-        } else {
-            what = QName.create(cacheMod, qname.localName);
-        }
-
-        return CACHE.getReference(what);
+        return qname.intern();
     }
 
     /**
@@ -182,11 +173,28 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
         return module.getRevision();
     }
 
+    /**
+     * Return an interned reference to a equivalent QName.
+     *
+     * @return Interned reference, or this object if it was interned.
+     */
+    public QName intern() {
+        // We also want to make sure we keep the QNameModule cached
+        final QNameModule cacheMod = module.intern();
+
+        // Identity comparison is here on purpose, as we are deciding whether to potentially store 'qname' into the
+        // interner. It is important that it does not hold user-supplied reference (such a String instance from
+        // parsing of an XML document).
+        final QName template = cacheMod == module ? this : QName.create(cacheMod, localName);
+
+        return INTERNER.intern(template);
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((localName == null) ? 0 : localName.hashCode());
+        result = prime * result + Objects.hashCode(localName);
         result = prime * result + module.hashCode();
         return result;
     }
@@ -211,14 +219,7 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
             return false;
         }
         final QName other = (QName) obj;
-        if (localName == null) {
-            if (other.localName != null) {
-                return false;
-            }
-        } else if (!localName.equals(other.localName)) {
-            return false;
-        }
-        return module.equals(other.module);
+        return Objects.equals(localName, other.localName) && module.equals(other.module);
     }
 
     public static QName create(final QName base, final String localName) {
@@ -307,10 +308,10 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
     public String toString() {
         final StringBuilder sb = new StringBuilder();
         if (getNamespace() != null) {
-            sb.append(QNAME_LEFT_PARENTHESIS + getNamespace());
+            sb.append(QNAME_LEFT_PARENTHESIS).append(getNamespace());
 
             if (getFormattedRevision() != null) {
-                sb.append(QNAME_REVISION_DELIMITER + getFormattedRevision());
+                sb.append(QNAME_REVISION_DELIMITER).append(getFormattedRevision());
             }
             sb.append(QNAME_RIGHT_PARENTHESIS);
         }