GenuineSounds icon

Memoizer

GenuineSounds | PRO | 02/21/15 11:53:48 AM UTC | 0 ⭐ | 527 👁️ | Never ⏰ | []
Java |

500 B

|

None

|

0 👍

/

0 👎

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
 
public class Memoizer<T, U> {
 
    private final Map<T, U> cache = new ConcurrentHashMap<>();
 
    private Memoizer() {}
 
    private Function<T, U> doMemoize(final Function<T, U> function) {
        return input -> cache.computeIfAbsent(input, function::apply);
    }
 
    public static <T, U> Function<T, U> memoize(final Function<T, U> function) {
        return new Memoizer<T, U>().doMemoize(function);
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎