JCacheBasedIntBinaryOperatorMemoizer extends AbstractJCacheBasedMemoizer<KEY, Integer> implements IntBinaryOperator { @Override public int applyAsInt(final int left, final int right) { final KEY key = keyFunction.apply(left, right); return invoke(key, givenKey -> Integer.valueOf(function.applyAsInt(left, right))).intValue(); } JCacheBasedIntBinaryOperatorMemoizer(
            final Cache<KEY, Integer> cache,
            final IntBinaryFunction<KEY> keyFunction,
            final IntBinaryOperator function); @Override int applyAsInt(final int left, final int right);  }
@Test public void shouldMemoizeFunction() { final IntBinaryOperator function = (a, b) -> 123; final IntBinaryFunction<String> keyFunction = (a, b) -> "key"; try (final Cache<String, Integer> cache = JCacheMemoize.createCache(DoubleFunction.class)) { final JCacheBasedIntBinaryOperatorMemoizer<String> loader = new JCacheBasedIntBinaryOperatorMemoizer<>( cache, keyFunction, function); Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsInt(123, 789)); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final IntBinaryFunction<String> keyFunction = (a, b) -> "key"; try (final Cache<String, Integer> cache = Mockito.mock(Cache.class)) { final JCacheBasedIntBinaryOperatorMemoizer<String> loader = new JCacheBasedIntBinaryOperatorMemoizer<>( cache, keyFunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.applyAsInt(123, 789); } }