All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.galan.commons.collection.SoftReferenceCache Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
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