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