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