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