GuavaCacheBasedLongToDoubleFunctionMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Double> implements LongToDoubleFunction { @Override public double applyAsDouble(final long value) { final KEY key = keyFunction.apply(value); return get(key, givenKey -> Double.valueOf(function.applyAsDouble(value))).doubleValue(); } GuavaCacheBasedLongToDoubleFunctionMemoizer(
            final Cache<KEY, Double> cache,
            final LongFunction<KEY> keyFunction,
            final LongToDoubleFunction function); @Override double applyAsDouble(final long value);  }
@Test public void shouldTransformInput() { final LongToDoubleFunction function = a -> 123.456D; final LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Double> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedLongToDoubleFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToDoubleFunctionMemoizer<>( 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 LongFunction<Long> keyFunction = Long::valueOf; final Cache<Long, Double> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedLongToDoubleFunctionMemoizer<Long> memoizer = new GuavaCacheBasedLongToDoubleFunctionMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsDouble(789); }