GuavaCacheBasedDoubleConsumerMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Double> implements DoubleConsumer { @Override public void accept(final double value) { final KEY key = keyFunction.apply(value); get(key, givenKey -> { consumer.accept(value); return Double.valueOf(value); }); } GuavaCacheBasedDoubleConsumerMemoizer(
            final Cache<KEY, Double> cache,
            final DoubleFunction<KEY> keyFunction,
            final DoubleConsumer consumer); @Override void accept(final double value);  }
@Test public void shouldConsumeGivenValue() { final DoubleFunction<String> keyFunction = a -> "key"; final DoubleConsumer consumer = Mockito.mock(DoubleConsumer.class); final Cache<String, Double> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedDoubleConsumerMemoizer<String> memoizer = new GuavaCacheBasedDoubleConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept(123.456D); Mockito.verify(consumer).accept(123.456D); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final DoubleFunction<String> keyFunction = a -> "key"; final DoubleConsumer consumer = System.out::println; final Cache<String, Double> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoubleConsumerMemoizer<String> memoizer = new GuavaCacheBasedDoubleConsumerMemoizer<>( cache, keyFunction, consumer); thrown.expect(MemoizationException.class); memoizer.accept(123.456D); }