Use a Guava interner instead of ObjectCache 21/30321/2
authorRobert Varga <robert.varga@pantheon.sk>
Sun, 29 Nov 2015 00:04:44 +0000 (01:04 +0100)
committerRobert Varga <robert.varga@pantheon.sk>
Sun, 29 Nov 2015 00:21:30 +0000 (01:21 +0100)
The use of ObjectCache is equivalent to the use of an Interner. The only
difference are soft references -- and we actually want to use weak
references instead, anyway.

Change-Id: Iee6efb2fdef0b22ec5bdc8c633b9535bf7c16a85
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
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

index d330d6105ea1516c6020dd19bdfa9d4b3b91f486..ed05140174f03cbf3932a1f6bd0c0ed4dfb19bdc 100644 (file)
@@ -9,6 +9,8 @@ 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;
@@ -46,6 +48,7 @@ import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
  *
  */
 public final class QName implements Immutable, Serializable, Comparable<QName> {
+    private static final Interner<QName> INTERNER = Interners.newWeakInterner();
     private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QName.class);
     private static final long serialVersionUID = 5398411242927766414L;
 
@@ -74,7 +77,10 @@ public final class QName implements Immutable, Serializable, Comparable<QName> {
      *
      * @param qname QName instance
      * @return Cached instance, according to {@link 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();
@@ -184,6 +190,23 @@ 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;
index 1ddd092db52b2e6d99221de60f6b6c4f3502c5f8..32cf9c0cd557b5168cb1da7dd38a25ea04554e01 100644 (file)
@@ -8,6 +8,8 @@
 package org.opendaylight.yangtools.yang.common;
 
 import com.google.common.base.MoreObjects;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
 import java.io.Serializable;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -20,6 +22,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class QNameModule implements Immutable, Serializable {
+    private static final Interner<QNameModule> INTERNER = Interners.newWeakInterner();
     private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(QNameModule.class);
     private static final Logger LOG = LoggerFactory.getLogger(QNameModule.class);
     private static final QNameModule NULL_INSTANCE = new QNameModule(null, null);
@@ -44,11 +47,23 @@ public final class QNameModule implements Immutable, Serializable {
      *
      * @param module Module instance
      * @return Cached instance, according to {@link ObjectCache} policy.
+     *
+     * @deprecated Use {@link #intern()} instead.
      */
+    @Deprecated
     public static QNameModule cachedReference(final QNameModule module) {
         return CACHE.getReference(module);
     }
 
+    /**
+     * Return an interned reference to a equivalent QNameModule.
+     *
+     * @return Interned reference, or this object if it was interned.
+     */
+    public QNameModule intern() {
+        return INTERNER.intern(this);
+    }
+
     /**
      * Create a new QName module instance with specified namespace/revision.
      *