GuavaCacheBasedFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, OUTPUT> implements Function<INPUT, OUTPUT> { @Override public OUTPUT apply(final INPUT input) { final KEY key = keyFunction.apply(input); return get(key, givenKey -> function.apply(input)); } GuavaCacheBasedFunctionMemoizer(
            final Cache<KEY, OUTPUT> cache,
            final Function<INPUT, KEY> keyFunction,
            final Function<INPUT, OUTPUT> function); @Override OUTPUT apply(final INPUT input);  }
@Test public void shouldTransformInput() { final Function<String, String> function = Function.identity(); final Function<String, String> keyFunction = Function.identity(); final Cache<String, String> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("value", memoizer.apply("value")); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final Function<String, String> keyFunction = Function.identity(); final Cache<String, String> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedFunctionMemoizer<String, String, String> memoizer = new GuavaCacheBasedFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.apply("test"); }