ConcurrentMapBasedDoubleBinaryOperatorMemoizer extends ConcurrentMapBasedMemoizer<KEY, Double> implements DoubleBinaryOperator { @Override public double applyAsDouble(final double left, final double right) { final KEY key = keyFunction.apply(left, right); return computeIfAbsent(key, givenKey -> Double.valueOf(operator.applyAsDouble(left, right))) .doubleValue(); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedDoubleBinaryOperatorMemoizer(
            final ConcurrentMap<KEY, Double> cache,
            final DoubleBinaryFunction<KEY> keyFunction,
            final DoubleBinaryOperator operator); @Override double applyAsDouble(final double left, final double right);  }
@Test public void shouldMemoizeOperator() { final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final DoubleBinaryFunction<String> keyFunction = (first, second) -> first + " " + second; final DoubleBinaryOperator operator = (first, second) -> first + second; final ConcurrentMapBasedDoubleBinaryOperatorMemoizer<String> memoizer = new ConcurrentMapBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, operator); memoizer.applyAsDouble(123.456D, 789.123D); }
@Test public void shouldUseSetCacheKeyAndValue() { final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final DoubleBinaryFunction<String> keyFunction = (first, second) -> first + " " + second; final DoubleBinaryOperator operator = (first, second) -> first + second; final ConcurrentMapBasedDoubleBinaryOperatorMemoizer<String> memoizer = new ConcurrentMapBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, operator); memoizer.applyAsDouble(123D, 789D); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", "123.0 789.0", memoizer.viewCacheForTest().keySet().iterator().next()); Assert.assertEquals("Memoization value does not match expectations", 912D, memoizer.viewCacheForTest().values().iterator().next().doubleValue(), 0.0D); }
@Test public void shouldUseCallWrappedOperator() { final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final DoubleBinaryFunction<String> keyFunction = (first, second) -> first + " " + second; final DoubleBinaryOperator operator = Mockito.mock(DoubleBinaryOperator.class); final ConcurrentMapBasedDoubleBinaryOperatorMemoizer<String> memoizer = new ConcurrentMapBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, operator); memoizer.applyAsDouble(123D, 789D); Mockito.verify(operator).applyAsDouble(123D, 789D); }
@Test public void shouldUseReturnOperatorResult() { final ConcurrentMap<String, Double> cache = new ConcurrentHashMap<>(); final DoubleBinaryFunction<String> keyFunction = (first, second) -> first + " " + second; final DoubleBinaryOperator operator = (first, second) -> first + second; final ConcurrentMapBasedDoubleBinaryOperatorMemoizer<String> memoizer = new ConcurrentMapBasedDoubleBinaryOperatorMemoizer<>( cache, keyFunction, operator); Assert.assertEquals(912D, memoizer.applyAsDouble(123D, 789D), 0.0D); }