com.unit16.z.function.HashCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unit16-z Show documentation
Show all versions of unit16-z Show documentation
A collection of utilities for representing and manipulating time series and such.
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);
}
}