JCacheBasedIntUnaryOperatorMemoizer extends AbstractJCacheBasedMemoizer<KEY, Integer> implements IntUnaryOperator { @Override public int applyAsInt(final int value) { final KEY key = keyFunction.apply(value); return invoke(key, givenKey -> Integer.valueOf(biFunction.applyAsInt(value))).intValue(); } JCacheBasedIntUnaryOperatorMemoizer(
            final Cache<KEY, Integer> cache,
            final IntFunction<KEY> keyFunction,
            final IntUnaryOperator biFunction); @Override int applyAsInt(final int value);  }
@Test public void shouldMemoizeBiFunction() { final IntUnaryOperator function = first -> 123; final IntFunction<String> keyfunction = a -> "key"; try (final Cache<String, Integer> cache = JCacheMemoize.createCache(ToDoubleBiFunction.class)) { final JCacheBasedIntUnaryOperatorMemoizer<String> loader = new JCacheBasedIntUnaryOperatorMemoizer<>( cache, keyfunction, function); Assert.assertEquals("Memoized value does not match expectation", 123, loader.applyAsInt(123)); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final IntFunction<String> keyfunction = a -> "key"; try (final Cache<String, Integer> cache = Mockito.mock(Cache.class)) { final JCacheBasedIntUnaryOperatorMemoizer<String> loader = new JCacheBasedIntUnaryOperatorMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.applyAsInt(123); } }