ConcurrentMapBasedDoubleUnaryOperatorMemoizer extends ConcurrentMapBasedMemoizer<KEY, Double> implements DoubleUnaryOperator { @Override public double applyAsDouble(final double operand) { final KEY key = keyFunction.apply(operand); return computeIfAbsent(key, givenKey -> Double.valueOf(operator.applyAsDouble(operand))) .doubleValue(); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedDoubleUnaryOperatorMemoizer(
            final ConcurrentMap<KEY, Double> cache,
            final DoubleFunction<KEY> keyFunction,
            final DoubleUnaryOperator operator); @Override double applyAsDouble(final double operand);  }
@Test public void shouldMemoizeOperator() { final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); memoizer.applyAsDouble(123.456D); }
@Test public void shouldUseSetCacheKeyAndValue() { final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); memoizer.applyAsDouble(123D); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123D, memoizer.viewCacheForTest().keySet().iterator().next().doubleValue(), 0.0D); Assert.assertEquals("Memoization value does not match expectations", 123D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
@Test public void shouldUseCallWrappedOperator() { final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = Mockito.mock(DoubleUnaryOperator.class); final DoubleFunction<Double> keyFunction = Double::valueOf; final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); memoizer.applyAsDouble(123D); Mockito.verify(operator).applyAsDouble(123D); }
@Test public void shouldUseReturnOperatorResult() { final ConcurrentMap<Double, Double> cache = new ConcurrentHashMap<>(); final DoubleUnaryOperator operator = input -> input; final DoubleFunction<Double> keyFunction = Double::valueOf; final ConcurrentMapBasedDoubleUnaryOperatorMemoizer<Double> memoizer = new ConcurrentMapBasedDoubleUnaryOperatorMemoizer<>( cache, keyFunction, operator); Assert.assertEquals(123D, memoizer.applyAsDouble(123D), 0.0D); }