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