blob: d0f66717373023c2de8ee0b1ba33ef045b6c4579 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 The University of York.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* Dimitrios Kolovos - initial API and implementation
******************************************************************************/
package org.eclipse.epsilon.eol.util;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
public class Cache<K, V> {
protected ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
protected HashMap<IdentityBasedWeakReference, V> map = new HashMap<IdentityBasedWeakReference, V>();
protected Thread cleanUpThread = null;
public Cache() {}
public HashMap<IdentityBasedWeakReference, V> getMap() {
return map;
}
protected Thread createCleanUpThread() {
return new Thread(){
@Override
public void run() {
try {
while (map.size() > 0 && !Thread.currentThread().isInterrupted()) {
IdentityBasedWeakReference reference = (IdentityBasedWeakReference) referenceQueue.remove();
synchronized(map) {
if (reference != null && map.containsKey(reference)) {
map.remove(reference);
}
}
}
} catch (InterruptedException e) {}
}
};
}
public V get(Object key) {
synchronized (map) {
return map.get(new IdentityBasedWeakReference(key, referenceQueue));
}
}
public V put(K key, V value) {
synchronized (map) {
IdentityBasedWeakReference reference = new IdentityBasedWeakReference(key, referenceQueue);
if (!map.containsKey(reference)) {
map.put(reference, value);
if (map.size() == 1) {
try {
cleanUpThread = createCleanUpThread();
cleanUpThread.setDaemon(true);
cleanUpThread.start();
}
catch (Exception ex) {}
}
}
return value;
}
}
public int size() {
return map.size();
}
public void dispose() {
if (cleanUpThread != null) {
cleanUpThread.interrupt();
cleanUpThread = null;
}
map.clear();
}
}