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