JCacheBasedBiPredicateMemoizer extends AbstractJCacheBasedMemoizer<KEY, Boolean> implements BiPredicate<FIRST, SECOND> { @Override public boolean test(final FIRST first, final SECOND second) { final KEY key = keyFunction.apply(first, second); return invoke(key, givenKey -> Boolean.valueOf(biPredicate.test(first, second))).booleanValue(); } JCacheBasedBiPredicateMemoizer(
            final Cache<KEY, Boolean> cache,
            final BiFunction<FIRST, SECOND, KEY> keyFunction,
            final BiPredicate<FIRST, SECOND> biPredicate); @Override boolean test(final FIRST first, final SECOND second);  }
@Test public void shouldMemoizeBiPredicate() { final BiPredicate<String, String> biPredicate = (first, second) -> true; final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, Boolean> cache = JCacheMemoize.createCache(BiPredicate.class)) { final JCacheBasedBiPredicateMemoizer<String, String, String> loader = new JCacheBasedBiPredicateMemoizer<>( cache, keyfunction, biPredicate); Assert.assertTrue("Memoized value does not match expectation", loader.test("first", "second")); } }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapRuntimeExceptionInMemoizationException() { final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction(); try (final Cache<String, Boolean> cache = Mockito.mock(Cache.class)) { final JCacheBasedBiPredicateMemoizer<String, String, String> loader = new JCacheBasedBiPredicateMemoizer<>( cache, keyfunction, null); given(cache.invoke(any(), any())).willThrow(RuntimeException.class); thrown.expect(MemoizationException.class); loader.test("first", "second"); } }