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