GuavaCacheBasedDoubleBinaryOperatorMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Double> implements DoubleBinaryOperator { @Override public double applyAsDouble(final double left, final double right) { final KEY key = keyFunction.apply(left, right); return get(key, givenKey -> Double.valueOf(function.applyAsDouble(left, right))).doubleValue(); } GuavaCacheBasedDoubleBinaryOperatorMemoizer(
            final Cache<KEY, Double> cache,
            final DoubleBinaryFunction<KEY> keyFunction,
            final DoubleBinaryOperator function); @Override double applyAsDouble(final double left, final double right);  }
@Test public void shouldTransformInput() { final DoubleBinaryOperator function = (a, b) -> 123.456D; final DoubleBinaryFunction<String> keyFunction = (a, b) -> "key"; final Cache<String, Double> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedDoubleBinaryOperatorMemoizer<String> memoizer = new GuavaCacheBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, function); Assert.assertEquals(123.456D, memoizer.applyAsDouble(123.456D, 789.123D), 0.0D); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final DoubleBinaryFunction<String> keyFunction = (a, b) -> "key"; final Cache<String, Double> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoubleBinaryOperatorMemoizer<String> memoizer = new GuavaCacheBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, null); thrown.expect(MemoizationException.class); memoizer.applyAsDouble(123.456D, 789.123D); }