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