Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.genuinevm.tests;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.function.Function;
- public class MemoizedFunction<T, U> implements Function<T, U> {
- public final Map<T, U> cache = new ConcurrentHashMap<>();
- private final Function<T, U> function;
- public MemoizedFunction(Function<T, U> function) {
- this.function = function;
- }
- public Function<T, U> normal() {
- return function::apply;
- }
- @Override
- public U apply(T t) {
- return cache.computeIfAbsent(t, function);
- }
- }
Advertisement