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