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