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