ConcurrentMapBasedLongConsumerMemoizer extends ConcurrentMapBasedMemoizer<KEY, Long> implements LongConsumer { @Override public void accept(final long value) { final KEY key = keyFunction.apply(value); computeIfAbsent(key, givenKey -> { consumer.accept(value); return Long.valueOf(value); }); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedLongConsumerMemoizer(
            final ConcurrentMap<KEY, Long> cache,
            final LongFunction<KEY> keyFunction,
            final LongConsumer consumer); @Override void accept(final long value);  }
@Test public void shouldMemoizeConsumer() { final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongConsumer consumer = System.out::println; final LongFunction<Long> keyFunction = Long::valueOf; final ConcurrentMapBasedLongConsumerMemoizer<Long> memoizer = new ConcurrentMapBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept(123L); }
@Test public void shouldUseSetCacheKeyAndValue() { final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongConsumer consumer = System.out::println; final LongFunction<Long> keyFunction = Long::valueOf; final ConcurrentMapBasedLongConsumerMemoizer<Long> memoizer = new ConcurrentMapBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept(123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123L, memoizer.viewCacheForTest().keySet().iterator().next().longValue()); Assert.assertEquals("Memoization value does not match expectations", 123L, memoizer.viewCacheForTest().values().iterator().next().longValue()); }
@Test public void shouldUseCallWrappedConsumer() { final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongConsumer consumer = Mockito.mock(LongConsumer.class); final LongFunction<Long> keyFunction = Long::valueOf; final ConcurrentMapBasedLongConsumerMemoizer<Long> memoizer = new ConcurrentMapBasedLongConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept(123L); Mockito.verify(consumer).accept(123L); }