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