GenuineSounds

Untitled

Feb 21st, 2015
637
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. package com.genuinevm.tests;
  2.  
  3. import java.util.Map;
  4. import java.util.concurrent.ConcurrentHashMap;
  5. import java.util.function.Function;
  6.  
  7. public class MemoizedFunction<T, U> implements Function<T, U> {
  8.  
  9.     public final Map<T, U> cache = new ConcurrentHashMap<>();
  10.     private final Function<T, U> function;
  11.  
  12.     public MemoizedFunction(Function<T, U> function) {
  13.         this.function = function;
  14.     }
  15.  
  16.     public Function<T, U> normal() {
  17.         return function::apply;
  18.     }
  19.  
  20.     @Override
  21.     public U apply(T t) {
  22.         return cache.computeIfAbsent(t, function);
  23.     }
  24. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment