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