net.finmath.util.Cached Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of finmath-lib Show documentation
Show all versions of finmath-lib Show documentation
finmath lib is a Mathematical Finance Library in Java.
It provides algorithms and methodologies related to mathematical finance.
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);
}
}