JCacheBasedDoubleConsumerMemoizer extends AbstractJCacheBasedMemoizer<KEY, Double> implements DoubleConsumer { @Override public void accept(final double input) { final KEY key = keyFunction.apply(input); invoke(key, givenKey -> { consumer.accept(input); return Double.valueOf(input); }); } JCacheBasedDoubleConsumerMemoizer(
            final Cache<KEY, Double> cache,
            final DoubleFunction<KEY> keyFunction,
            final DoubleConsumer consumer); @Override void accept(final double input);  }
@Test public void shouldMemoizeConsumer() { final DoubleConsumer consumer = Mockito.mock(DoubleConsumer.class); final DoubleFunction<String> keyFunction = a -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(DoubleConsumer.class)) { final JCacheBasedDoubleConsumerMemoizer<String> loader = new JCacheBasedDoubleConsumerMemoizer<>(cache, keyFunction, consumer); loader.accept(123.456D); Mockito.verify(consumer).accept(123.456D); } }
@Test public void shouldMemoizeConsumerOnce() { final DoubleConsumer consumer = Mockito.mock(DoubleConsumer.class); final DoubleFunction<String> keyFunction = a -> "key"; try (final Cache<String, Double> cache = JCacheMemoize.createCache(DoubleConsumer.class)) { final JCacheBasedDoubleConsumerMemoizer<String> loader = new JCacheBasedDoubleConsumerMemoizer<>(cache, keyFunction, consumer); loader.accept(123.456D); loader.accept(123.456D); Mockito.verify(consumer).accept(123.456D); } }
@Test public void shouldWrapRuntimeExceptionInMemoizationException() { final DoubleFunction<String> keyFunction = a -> "key"; try (final Cache<String, Double> cache = Mockito.mock(Cache.class)) { final JCacheBasedDoubleConsumerMemoizer<String> loader = new JCacheBasedDoubleConsumerMemoizer<>(cache, keyFunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.accept(123.456D); } }