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

net.finmath.util.Cached Maven / Gradle / Ivy

Go to download

finmath lib is a Mathematical Finance Library in Java. It provides algorithms and methodologies related to mathematical finance.

There is a newer version: 6.0.19
Show newest version
package net.finmath.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * A simple wrapper, wrapping a Function<K,V> such that all calculations are cached in a ConcurrentHashMap<K,V>
 * 
 * @author Christian Fries
 *
 * @param  The type of the cache key.
 * @param  The type of the value.
 */
public class Cached implements Function {

	private final Function mappingFunction;
	private final Map cache = new ConcurrentHashMap();

	Cached(Function mappingFunction) { this.mappingFunction = mappingFunction; }

	public static  Function of(Function mappingFunction) {
		return new Cached(mappingFunction);
	}

	@Override
	public V apply(K key) {
		return cache.computeIfAbsent(key, mappingFunction); 
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy