@Test public void shouldMemoizeFunctionCall() { final ConcurrentMap<String, String> cache = new ConcurrentHashMap<>(); final IntFunction<String> function = input -> "output"; final IntFunction<String> keyFunction = input -> "key"; final ConcurrentMapBasedIntFunctionMemoizer<String, String> memoizer = new ConcurrentMapBasedIntFunctionMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectations", "output", memoizer.apply(123)); }
@Override public OUTPUT apply(final int value) { final KEY key = keyFunction.apply(value); return computeIfAbsent(key, givenKey -> function.apply(value)); }
ConcurrentMapBasedIntFunctionMemoizer extends ConcurrentMapBasedMemoizer<KEY, OUTPUT> implements IntFunction<OUTPUT> { @Override public OUTPUT apply(final int value) { final KEY key = keyFunction.apply(value); return computeIfAbsent(key, givenKey -> function.apply(value)); } }
ConcurrentMapBasedIntFunctionMemoizer extends ConcurrentMapBasedMemoizer<KEY, OUTPUT> implements IntFunction<OUTPUT> { @Override public OUTPUT apply(final int value) { final KEY key = keyFunction.apply(value); return computeIfAbsent(key, givenKey -> function.apply(value)); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedIntFunctionMemoizer(
            final ConcurrentMap<KEY, OUTPUT> cache,
            final IntFunction<KEY> keyFunction,
            final IntFunction<OUTPUT> function); }
ConcurrentMapBasedIntFunctionMemoizer extends ConcurrentMapBasedMemoizer<KEY, OUTPUT> implements IntFunction<OUTPUT> { @Override public OUTPUT apply(final int value) { final KEY key = keyFunction.apply(value); return computeIfAbsent(key, givenKey -> function.apply(value)); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedIntFunctionMemoizer(
            final ConcurrentMap<KEY, OUTPUT> cache,
            final IntFunction<KEY> keyFunction,
            final IntFunction<OUTPUT> function); @Override OUTPUT apply(final int value); }
ConcurrentMapBasedIntFunctionMemoizer extends ConcurrentMapBasedMemoizer<KEY, OUTPUT> implements IntFunction<OUTPUT> { @Override public OUTPUT apply(final int value) { final KEY key = keyFunction.apply(value); return computeIfAbsent(key, givenKey -> function.apply(value)); } @SuppressWarnings(CompilerWarnings.NLS)  ConcurrentMapBasedIntFunctionMemoizer(
            final ConcurrentMap<KEY, OUTPUT> cache,
            final IntFunction<KEY> keyFunction,
            final IntFunction<OUTPUT> function); @Override OUTPUT apply(final int value);  }