com.unit16.z.function.HashCache Maven / Gradle / Ivy
package com.unit16.z.function;
import java.util.HashMap;
import java.util.function.Function;
public final class HashCache
implements Function
{
private final HashMap cache_ = new HashMap<>();
/**
* Must be referentially transparent:
*/
private final Function f_;
public HashCache(Function f) { f_ = f; }
@Override
public Y apply(X from) {
if (!cache_.containsKey(from))
{
cache_.put(from, f_.apply(from));
}
return cache_.get(from);
}
}