GenuineSounds

Memoizer

Feb 21st, 2015
622
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.50 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.concurrent.ConcurrentHashMap;
  3. import java.util.function.Function;
  4.  
  5. public class Memoizer<T, U> {
  6.  
  7.     private final Map<T, U> cache = new ConcurrentHashMap<>();
  8.  
  9.     private Memoizer() {}
  10.  
  11.     private Function<T, U> doMemoize(final Function<T, U> function) {
  12.         return input -> cache.computeIfAbsent(input, function::apply);
  13.     }
  14.  
  15.     public static <T, U> Function<T, U> memoize(final Function<T, U> function) {
  16.         return new Memoizer<T, U>().doMemoize(function);
  17.     }
  18. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment