
de.galan.commons.collection.SoftReferenceCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
Additional common functionality for Java developers.
package de.galan.commons.collection;
import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Stores it's value-entries as SoftReferences
*
* @author galan
* @param Type of value objects
*/
public class SoftReferenceCache {
private Map> cache = null;
public SoftReferenceCache() {
cache = Collections.synchronizedMap(new HashMap>());
}
public void put(String key, T obj) {
cache.put(key, new SoftReference(obj));
}
public T get(String key) {
SoftReference ref = cache.get(key);
if (ref == null) {
return null;
}
T result = ref.get();
if (result == null) {
cache.remove(key);
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy