GuavaCacheBasedIntToDoubleFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Double> implements IntToDoubleFunction { @Override public double applyAsDouble(final int value) { final KEY key = keyFunction.apply(value); return get(key, givenKey -> Double.valueOf(function.applyAsDouble(value))).doubleValue(); } GuavaCacheBasedIntToDoubleFunctionMemoizer(
            final Cache<KEY, Double> cache,
            final IntFunction<KEY> keyFunction,
            final IntToDoubleFunction function); @Override double applyAsDouble(final int value);  }
@Test public void shouldTransformInput() { final IntToDoubleFunction function = a -> 123.456D; final IntFunction<Integer> keyFunction = Integer::valueOf; final Cache<Integer, Double> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedIntToDoubleFunctionMemoizer<Integer> memoizer = new GuavaCacheBasedIntToDoubleFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectation", 123.456D, memoizer.applyAsDouble(789), 0.0D); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final IntFunction<Integer> keyFunction = Integer::valueOf; final Cache<Integer, Double> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedIntToDoubleFunctionMemoizer<Integer> memoizer = new GuavaCacheBasedIntToDoubleFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsDouble(789); }