View Javadoc

1   package com.atlassian.asap.core.server.jersey;
2   
3   import java.util.Map;
4   import java.util.concurrent.ConcurrentHashMap;
5   import java.util.function.Function;
6   
7   /**
8    * Memoizes the result of a function.
9    */
10  final class Memoizer<T, U> {
11  
12      private final Map<T, U> cache = new ConcurrentHashMap<>();
13  
14      private Memoizer() {
15      }
16  
17      private Function<T, U> doMemoize(final Function<T, U> function) {
18          return input -> cache.computeIfAbsent(input, function);
19      }
20  
21      static <T, U> Function<T, U> memoize(final Function<T, U> function) {
22          return new Memoizer<T, U>().doMemoize(function);
23      }
24  }