com.github.sviperll.collection.KeyedObjectPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chicory-core Show documentation
Show all versions of chicory-core Show documentation
Basic utilities to compensate shortcomings of Java SE.
Part of chicory.
/*
* Copyright (c) 2014, Victor Nazarov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Victor Nazarov nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.github.sviperll.collection;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
/**
* Pool of object instances.
* Instances are created on demand.
* Created instances are stored in the pool and reused.
*/
public class KeyedObjectPool {
/**
* Creates new pool of object instances.
* Instances are created on demand.
* Created instances are stored in the pool and reused.
* Hard retain policy guarantees that the same instance is always return for the same key.
* Soft and weak policies can return different instances for the same key.
* With soft and weak policies only one instance for given key can be referenced at any point of time.
*/
public static KeyedObjectFactory createInstance(KeyedObjectFactory factory, RetainPolicy policy) {
switch(policy) {
case HARD:
return new HardKeyedObjectPool(factory);
case SOFT:
return new SoftKeyedObjectPool(factory);
case WEAK:
return new WeakKeyedObjectPool(factory);
default:
throw new IllegalArgumentException("Unsupported retain policy: " + policy);
}
}
private KeyedObjectPool() {
}
/**
* HardKeyedObjectPool is a simple implementation.
* HardKeyedObjectPool provides mapping between key and value.
* HardKeyedObjectPool guarantees that the same instance is always return for the same key.
* Every keyed oject instance is cached and is never freed.
*/
private static class HardKeyedObjectPool implements KeyedObjectFactory {
private final Map map = new HashMap();
private final KeyedObjectFactory factory;
public HardKeyedObjectPool(KeyedObjectFactory factory) {
this.factory = factory;
}
@Override
public V createKeyedInstance(K key) {
V instance = map.get(key);
if (instance == null) {
instance = factory.createKeyedInstance(key);
map.put(key, instance);
}
return instance;
}
}
/**
* SoftKeyedObjectPool can return different instances for the same key.
* SoftKeyedObjectPool allowes instances to be garbage collected.
* Only one instance for given key can be referenced at any point of time.
* Instances are freed using the "soft" policy of JVM.
*/
private static class SoftKeyedObjectPool implements KeyedObjectFactory {
private final Object lock = new Object();
private final ReferenceQueue queue = new ReferenceQueue();
private final Map> map = new HashMap>();
private final KeyedObjectFactory factory;
private WeakReference garbageCollector = new WeakReference(new SoftKeyedObjectPoolGarbageCollector());
public SoftKeyedObjectPool(KeyedObjectFactory factory) {
this.factory = factory;
}
@Override
public V createKeyedInstance(K key) {
garbageCollect();
synchronized (lock) {
KeyedSoftReference ref = map.get(key);
V instance;
if (ref == null) {
instance = factory.createKeyedInstance(key);
ref = new KeyedSoftReference(key, instance, queue);
map.put(key, ref);
} else {
instance = ref.get();
if (instance == null) {
instance = factory.createKeyedInstance(key);
ref = new KeyedSoftReference(key, instance, queue);
map.put(key, ref);
}
}
return instance;
}
}
@SuppressWarnings("unchecked")
private void garbageCollect() {
synchronized (lock) {
KeyedSoftReference ref;
while ((ref = (KeyedSoftReference)queue.poll()) != null) {
map.remove(ref.key);
}
if (garbageCollector.get() == null)
garbageCollector = new WeakReference(new SoftKeyedObjectPoolGarbageCollector());
}
}
private class SoftKeyedObjectPoolGarbageCollector {
@Override
protected void finalize() throws Throwable {
super.finalize();
garbageCollect();
}
}
private static class KeyedSoftReference extends SoftReference {
private final K key;
public KeyedSoftReference(K key, V instance, ReferenceQueue queue) {
super(instance, queue);
this.key = key;
}
}
}
/**
* WeakKeyedObjectPool can return different instances for the same key.
* WeakKeyedObjectPool allowes instances to be garbage collected.
* Only one instance for given key can be referenced at any point of time.
* Instances are freed using the "weak" policy of JVM.
*/
private static class WeakKeyedObjectPool implements KeyedObjectFactory {
private final Object lock = new Object();
private final ReferenceQueue queue = new ReferenceQueue();
private final Map> map = new HashMap>();
private final KeyedObjectFactory factory;
private WeakReference garbageCollector = new WeakReference(new WeakKeyedObjectPoolGarbageCollector());
public WeakKeyedObjectPool(KeyedObjectFactory factory) {
this.factory = factory;
}
@Override
public V createKeyedInstance(K key) {
garbageCollect();
synchronized (lock) {
KeyedWeakReference ref = map.get(key);
V instance;
if (ref == null) {
instance = factory.createKeyedInstance(key);
ref = new KeyedWeakReference(key, instance, queue);
map.put(key, ref);
} else {
instance = ref.get();
if (instance == null) {
instance = factory.createKeyedInstance(key);
ref = new KeyedWeakReference(key, instance, queue);
map.put(key, ref);
}
}
return instance;
}
}
@SuppressWarnings("unchecked")
private void garbageCollect() {
synchronized (lock) {
KeyedWeakReference ref;
while ((ref = (KeyedWeakReference)queue.poll()) != null) {
map.remove(ref.key);
}
if (garbageCollector.get() == null)
garbageCollector = new WeakReference(new WeakKeyedObjectPoolGarbageCollector());
}
}
private class WeakKeyedObjectPoolGarbageCollector {
@Override
protected void finalize() throws Throwable {
super.finalize();
garbageCollect();
}
}
private static class KeyedWeakReference extends WeakReference {
private final K key;
public KeyedWeakReference(K key, V instance, ReferenceQueue queue) {
super(instance, queue);
this.key = key;
}
}
}
}