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

consulting.omnia.util.core.Memoizer Maven / Gradle / Ivy

package consulting.omnia.util.core;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;

/**
 * @author Ronaldo Blanc ronaldoblanc at omnia.consulting
 */
public final class Memoizer {

	private static final int MAX_ELEMENTS = 100000;
	private static final Memoizer INSTANCE = new Memoizer();
	private static final ConcurrentMap results = new ConcurrentHashMap();
	private static final Logger LOGGER = Logger.getLogger(Memoizer.class
			.getCanonicalName());

	private Memoizer() {
	}

	public static Memoizer getInstance() {
		LOGGER.finest(Memoizer.class.getCanonicalName() + " instanced");
		return INSTANCE;
	}

	public void put(Object key, Object result) {
		LOGGER.finest("[Key: " + key + ", Value: " + result + "] added");
		if (results.size() >= MAX_ELEMENTS) {
			results.clear();
		}
		results.put(key, result);
	}

	public  T getResultAs(Object key, Class clazz) {
		return clazz.cast(results.get(key));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy