From 39894a777bd166f9fbe5d8c329bac42e8c73b1d4 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 29 Dec 2010 01:57:23 +0000 Subject: Use new Cojen utility classes. --- .../carbonado/util/QuickConstructorGenerator.java | 8 +++ .../com/amazon/carbonado/util/SoftValuedCache.java | 75 ++++++++++++++++++++-- 2 files changed, 79 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/amazon/carbonado') diff --git a/src/main/java/com/amazon/carbonado/util/QuickConstructorGenerator.java b/src/main/java/com/amazon/carbonado/util/QuickConstructorGenerator.java index a746c1f..e8c914e 100644 --- a/src/main/java/com/amazon/carbonado/util/QuickConstructorGenerator.java +++ b/src/main/java/com/amazon/carbonado/util/QuickConstructorGenerator.java @@ -43,7 +43,9 @@ import org.cojen.util.WeakIdentityMap; * but the actual object type is not. * * @author Brian S O'Neill + * @deprecated use {@link org.cojen.util.QuickConstructorGenerator} */ +@Deprecated public class QuickConstructorGenerator { // Map> @SuppressWarnings("unchecked") @@ -86,6 +88,12 @@ public class QuickConstructorGenerator { */ @SuppressWarnings("unchecked") public static synchronized F getInstance(Class objectType, Class factory) { + try { + return org.cojen.util.QuickConstructorGenerator.getInstance(objectType, factory); + } catch (NoClassDefFoundError e) { + // Use older code instead. + } + SoftValuedCache, Object> innerCache = cCache.get(factory); if (innerCache == null) { innerCache = SoftValuedCache.newCache(7); diff --git a/src/main/java/com/amazon/carbonado/util/SoftValuedCache.java b/src/main/java/com/amazon/carbonado/util/SoftValuedCache.java index 3e9e767..c1562a6 100644 --- a/src/main/java/com/amazon/carbonado/util/SoftValuedCache.java +++ b/src/main/java/com/amazon/carbonado/util/SoftValuedCache.java @@ -27,13 +27,17 @@ import java.lang.ref.SoftReference; * thread. Cache permits null keys, but not null values. * * @author Brian S O'Neill + * @deprecated use Cojen {@link org.cojen.util.Cache} interface */ +@Deprecated public abstract class SoftValuedCache { - // Design note: Public class is abstract to make it easy to swap out this - // implementation with something available in a shared collection library. - public static SoftValuedCache newCache(int capacity) { - return new Impl(capacity); + try { + return new Wrapper(capacity); + } catch (NoClassDefFoundError e) { + // Use older implementation for compatibility. + return new Impl(capacity); + } } public abstract int size(); @@ -58,6 +62,69 @@ public abstract class SoftValuedCache { public abstract String toString(); + private static class Wrapper extends SoftValuedCache { + private final org.cojen.util.Cache mCache; + + Wrapper(int capacity) { + mCache = new org.cojen.util.SoftValueCache(capacity); + } + + @Override + public int size() { + return mCache.size(); + } + + @Override + public boolean isEmpty() { + return mCache.isEmpty(); + } + + @Override + public V get(K key) { + return mCache.get(key); + } + + @Override + public V put(K key, V value) { + return mCache.put(key, value); + } + + @Override + public V putIfAbsent(K key, V value) { + return mCache.putIfAbsent(key, value); + } + + @Override + public V remove(K key) { + return mCache.remove(key); + } + + @Override + public boolean remove(K key, V value) { + return mCache.remove(key, value); + } + + @Override + public boolean replace(K key, V oldValue, V newValue) { + return mCache.replace(key, oldValue, newValue); + } + + @Override + public V replace(K key, V value) { + return mCache.replace(key, value); + } + + @Override + public void clear() { + mCache.clear(); + } + + @Override + public String toString() { + return mCache.toString(); + } + } + private static class Impl extends SoftValuedCache { private static final float LOAD_FACTOR = 0.75f; -- cgit v1.2.3