JCacheBasedIntPredicateMemoizer extends AbstractJCacheBasedMemoizer<KEY, Boolean> implements IntPredicate { @Override public boolean test(final int input) { final KEY key = keyFunction.apply(input); return invoke(key, givenKey -> Boolean.valueOf(predicate.test(input))).booleanValue(); } JCacheBasedIntPredicateMemoizer(
            final Cache<KEY, Boolean> cache,
            final IntFunction<KEY> keyFunction,
            final IntPredicate predicate); @Override boolean test(final int input);  }
@Test public void shouldMemoizePredicate() { final IntPredicate predicate = a -> true; final IntFunction<String> keyFunction = a -> "key"; try (final Cache<String, Boolean> cache = JCacheMemoize.createCache(IntPredicate.class)) { final JCacheBasedIntPredicateMemoizer<String> loader = new JCacheBasedIntPredicateMemoizer<>(cache, keyFunction, predicate); Assert.assertTrue("Memoized value does not match expectation", loader.test(123)); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final IntFunction<String> keyFunction = a -> "key"; try (final Cache<String, Boolean> cache = Mockito.mock(Cache.class)) { final JCacheBasedIntPredicateMemoizer<String> loader = new JCacheBasedIntPredicateMemoizer<>(cache, keyFunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.test(123); } }