Use Guava's Interner instead of ObjectCache 69/62269/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 24 Aug 2017 12:04:04 +0000 (14:04 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 28 Aug 2017 09:55:46 +0000 (09:55 +0000)
Guava's Interner does pretty much the same thing, no need to use
ObjectCache.

Change-Id: I4ee79155aeac465bfd59392656f555c3509459fd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/pom.xml
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/PotentialSchemaSource.java

index 63abe1b60f5aeb08e9819cc44e6f8331ac5812ac..5a82dbce13433ef1e45300d5126f6df39c63f168 100644 (file)
     </dependencyManagement>
 
     <dependencies>
-        <dependency>
-            <groupId>${project.groupId}</groupId>
-            <artifactId>object-cache-api</artifactId>
-        </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>util</artifactId>
index 80d23a94ff9763ae5a43062a775c8c32e3fadd63..3652fd42792bb0ff716ab8087840466f309eab97 100644 (file)
@@ -9,9 +9,9 @@ package org.opendaylight.yangtools.yang.model.repo.spi;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
-
-import org.opendaylight.yangtools.objcache.ObjectCache;
-import org.opendaylight.yangtools.objcache.ObjectCacheFactory;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
+import java.util.Objects;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 
@@ -64,7 +64,7 @@ public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
         }
     }
 
-    private static final ObjectCache CACHE = ObjectCacheFactory.getObjectCache(PotentialSchemaSource.class);
+    private static final Interner<PotentialSchemaSource<?>> INTERNER = Interners.newWeakInterner();
     private final Class<? extends T> representation;
     private final SourceIdentifier sourceIdentifier;
     private final int cost;
@@ -87,8 +87,9 @@ public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
      *
      * @return A potentially shared reference, not guaranteed to be unique.
      */
+    @SuppressWarnings("unchecked")
     public PotentialSchemaSource<T> cachedReference() {
-        return CACHE.getReference(this);
+        return (PotentialSchemaSource<T>) INTERNER.intern(this);
     }
 
     public SourceIdentifier getSourceIdentifier() {
@@ -105,12 +106,7 @@ public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
 
     @Override
     public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + cost;
-        result = prime * result + representation.hashCode();
-        result = prime * result + sourceIdentifier.hashCode();
-        return result;
+        return Objects.hash(cost, representation, sourceIdentifier);
     }
 
     @Override
@@ -122,15 +118,7 @@ public final class PotentialSchemaSource<T extends SchemaSourceRepresentation> {
             return false;
         }
         final PotentialSchemaSource<?> other = (PotentialSchemaSource<?>) obj;
-        if (cost != other.cost) {
-            return false;
-        }
-        if (!representation.equals(other.representation)) {
-            return false;
-        }
-        if (!sourceIdentifier.equals(other.sourceIdentifier)) {
-            return false;
-        }
-        return true;
+        return cost == other.cost && representation.equals(other.representation)
+                && sourceIdentifier.equals(other.sourceIdentifier);
     }
 }