GuavaCacheBasedLongPredicateMemoizer extends AbstractGuavaCacheBasedMemoizer<KEY, Boolean> implements LongPredicate { @Override public boolean test(final long input) { final KEY key = keyFunction.apply(input); return get(key, givenKey -> Boolean.valueOf(predicate.test(input))).booleanValue(); } GuavaCacheBasedLongPredicateMemoizer(
            final Cache<KEY, Boolean> cache,
            final LongFunction<KEY> keyFunction,
            final LongPredicate predicate); @Override boolean test(final long input);  }
@Test public void shouldTestGivenValue() { final LongPredicate predicate = Mockito.mock(LongPredicate.class); final LongFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedLongPredicateMemoizer<String> memoizer = new GuavaCacheBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); memoizer.test(123); Mockito.verify(predicate).test(123); }
@Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { final LongPredicate predicate = a -> true; final LongFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedLongPredicateMemoizer<String> memoizer = new GuavaCacheBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); thrown.expect(MemoizationException.class); memoizer.test(123); }
@Test public void shouldMemoizeResult() { final LongPredicate predicate = a -> true; final LongFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build(); final GuavaCacheBasedLongPredicateMemoizer<String> memoizer = new GuavaCacheBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); Assert.assertTrue(memoizer.test(123)); }