Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MemoFunction<T, U> implements Function<T, U> {
- public final Map<T, U> cache = new ConcurrentHashMap<>();
- private final Function<T, U> function;
- public MemoFunction(Function<T, U> function) {
- this.function = function;
- }
- public Function<T, U> cache() {
- return input -> cache.computeIfAbsent(input, function::apply);
- }
- @Override
- public U apply(T t) {
- return function.apply(t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement