ConcurrentMapBasedObjIntConsumerMemoizer extends ConcurrentMapBasedMemoizer<KEY, INPUT> implements ObjIntConsumer<INPUT> { @Override public void accept(final INPUT input, final int value) { final KEY key = keyFunction.apply(input, value); computeIfAbsent(key, givenKey -> { consumer.accept(input, value); return input; }); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedObjIntConsumerMemoizer(
            final ConcurrentMap<KEY, INPUT> cache,
            final ObjIntFunction<INPUT, KEY> keyFunction,
            final ObjIntConsumer<INPUT> consumer); @Override void accept(final INPUT input, final int value);  }
@Test public void shouldMemoizeConsumer() { final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<String> consumer = (first, second) -> System.out.println(first + second); final ConcurrentMapBasedObjIntConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjIntConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept("test", 123); }
@Test public void shouldUseSetCacheKeyAndValue() { final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<String> consumer = (first, second) -> System.out.println(first + second); final ConcurrentMapBasedObjIntConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjIntConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept("test", 123); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "test123", memoizer.viewCacheForTest().keySet().iterator().next()); Assert.assertEquals("Memoization value does not match expectations", "test", memoizer.viewCacheForTest().values().iterator().next()); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldUseCallWrappedConsumer() { final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final ObjIntFunction<String, String> keyFunction = (first, second) -> first + second; final ObjIntConsumer<String> consumer = Mockito.mock(ObjIntConsumer.class); final ConcurrentMapBasedObjIntConsumerMemoizer<String, String> memoizer = new ConcurrentMapBasedObjIntConsumerMemoizer<>( cache, keyFunction, consumer); memoizer.accept("test", 123); Mockito.verify(consumer).accept("test", 123); }