Advertisement
GenuineSounds

Untitled

Feb 21st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.42 KB | None | 0 0
  1. public class MemoFunction<T, U> implements Function<T, U> {
  2.  
  3.     public final Map<T, U> cache = new ConcurrentHashMap<>();
  4.     private final Function<T, U> function;
  5.  
  6.     public MemoFunction(Function<T, U> function) {
  7.         this.function = function;
  8.     }
  9.  
  10.     public Function<T, U> cache() {
  11.         return input -> cache.computeIfAbsent(input, function::apply);
  12.     }
  13.  
  14.     @Override
  15.     public U apply(T t) {
  16.         return function.apply(t);
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement