@Test public void LongPredicate() { // TODO - Convert the anonymous inner class to a lambda LongPredicate predicate = new LongPredicate() { @Override public boolean test(long value) { return value % 2 == 0; } }; List<Long> evens = LongStream.rangeClosed(1, 5).filter(predicate).boxed().collect(Collectors.toList()); Assert.assertEquals(Arrays.asList(2L, 4L), evens); List<Long> odds = LongStream.rangeClosed(1, 5).filter(predicate.negate()).boxed().collect(Collectors.toList()); Assert.assertEquals(Arrays.asList(1L, 3L, 5L), odds); Assert.assertTrue(LongStream.rangeClosed(1, 5).anyMatch(predicate)); Assert.assertFalse(LongStream.rangeClosed(1, 5).allMatch(predicate)); Assert.assertFalse(LongStream.rangeClosed(1, 5).noneMatch(predicate)); }
@Override public final LongStream filter(LongPredicate predicate) { Objects.requireNonNull(predicate); return new StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SIZED) { @Override Sink<Long> opWrapSink(int flags, Sink<Long> sink) { return new Sink.ChainedLong<Long>(sink) { @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(long t) { if (predicate.test(t)) downstream.accept(t); } }; } }; }
/** * Constructs a quantified predicate matcher for a {@code LongStream}. * * @param predicate the {@code Predicate} to apply to stream elements * @param matchKind the kind of quantified match (all, any, none) * @return a {@code TerminalOp} implementing the desired quantified match * criteria */ public static TerminalOp<Long, Boolean> makeLong(LongPredicate predicate, MatchKind matchKind) { Objects.requireNonNull(predicate); Objects.requireNonNull(matchKind); class MatchSink extends BooleanTerminalSink<Long> implements Sink.OfLong { MatchSink() { super(matchKind); } @Override public void accept(long t) { if (!stop && predicate.test(t) == matchKind.stopOnPredicateMatches) { stop = true; value = matchKind.shortCircuitResult; } } } return new MatchOp<>(StreamShape.LONG_VALUE, matchKind, MatchSink::new); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Long, Boolean> cache = null; final LongPredicate predicate = input -> true; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongPredicateMemoizer<>(cache, keyFunction, predicate); }
/** * */ @Test public void shouldTestGivenValue() { // given final LongPredicate predicate = Mockito.mock(LongPredicate.class); final LongFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build(); // when final GuavaCacheBasedLongPredicateMemoizer<String> memoizer = new GuavaCacheBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); // then memoizer.test(123); Mockito.verify(predicate).test(123); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given 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); // when thrown.expect(MemoizationException.class); // then memoizer.test(123); }
private void assertLongPredicates(Supplier<LongStream> source, Kind kind, LongPredicate[] predicates, boolean... answers) { for (int i = 0; i < predicates.length; i++) { setContext("i", i); boolean match = longKinds.get(kind).apply(predicates[i]).apply(source.get()); assertEquals(answers[i], match, kind.toString() + predicates[i].toString()); } }
@Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) public void testLongStream(String name, TestData.OfLong data) { for (LongPredicate p : LONG_PREDICATES) { setContext("p", p); for (Kind kind : Kind.values()) { setContext("kind", kind); exerciseTerminalOps(data, longKinds.get(kind).apply(p)); exerciseTerminalOps(data, s -> s.filter(lpFalse), longKinds.get(kind).apply(p)); exerciseTerminalOps(data, s -> s.filter(lpEven), longKinds.get(kind).apply(p)); } } }
@Test public void should_verify_an_actual_long_predicate_is_conform_to_an_expected_result() { assertThat(resultOf(() -> { gwtMock.whenAnEventHappensInRelationToAnActionOfTheConsumer(); return (LongPredicate) number -> number < 100; }).accepts(99)).hasSameClassAs(assertThat((LongPredicate) t -> t == 0)); }
/** * Constructor * @param start the start for range, inclusive * @param end the end for range, exclusive * @param step the step increment * @param excludes optional predicate to exclude elements in this range */ RangeOfLongs(long start, long end, long step, LongPredicate excludes) { super(start, end); this.start = start; this.end = end; this.step = step; this.ascend = start <= end; this.excludes = excludes; }
/** * @param defaultReturnValue A value to return if any throwable is caught. * @return An interface that returns a default value if any exception occurs. */ default LongPredicate thatReturnsOnCatch(final boolean defaultReturnValue) { return (final long v1) -> { try { return testWithThrowable(v1); } catch(final Throwable throwable) { return defaultReturnValue; } }; }
/** * */ @Test public void shouldMemoizeLongPredicateWithKeyFunction() { // given final LongPredicate predicate = a -> true; final LongFunction<String> keyFunction = a -> "key"; // when final LongPredicate memoize = JCacheMemoize.longPredicate(predicate, keyFunction); // then Assert.assertNotNull("Memoized LongPredicate is NULL", memoize); }
/** * */ @Test public void shouldMemoizePredicateCall() { // given final ConcurrentMap<Long, Boolean> cache = new ConcurrentHashMap<>(); final LongPredicate predicate = input -> true; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongPredicateMemoizer<Long> memoizer = new ConcurrentMapBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); // then Assert.assertTrue("Memoized value does not match expectations", memoizer.test(123L)); }
/** * */ @Test public void shouldMemoizeLongPredicateWithLambda() { // given // when final LongPredicate memoize = JCacheMemoize.longPredicate(a -> true); // then Assert.assertNotNull("Memoized LongPredicate is NULL", memoize); }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedLongPredicateMemoizer( final ConcurrentMap<KEY, Boolean> cache, final LongFunction<KEY> keyFunction, final LongPredicate predicate) { super(cache); this.keyFunction = keyFunction; this.predicate = requireNonNull(predicate, "Cannot memoize a NULL Predicate - provide an actual Predicate to fix this."); }
@Test public void noneMatch() { LongPredicate p = i -> true; boolean result = this.parallelStreamSupportMock.noneMatch(p); verify(this.delegateMock).noneMatch(p); assertTrue(result); }
/** * */ @Test public void shouldMemoizeLongPredicate() { // given final LongPredicate predicate = a -> true; // when final LongPredicate memoize = JCacheMemoize.longPredicate(predicate); // then Assert.assertNotNull("Memoized LongPredicate is NULL", memoize); }
@Test void testDescribeLongPredicateReturnsExpectedStringForNonDescriptive() { LongPredicate predicate = t -> t > 0; Assertions.assertEquals(ValidityUtils.unknownPredicatePrefix() + predicate.toString(), ValidityUtils.describe(predicate), "Non descriptive predicate should be consistently described."); }
@Test void testDescriptiveLongPredicateHasExpectedPredicate() { LongPredicate predicate = getNotValueTypeInstance(); Assertions.assertAll("DescriptiveLongPredicate should wrap the expected predicate.", () -> Assertions.assertTrue(predicate.test(1)), () -> Assertions.assertFalse(predicate.test(0)), () -> Assertions.assertFalse(predicate.test(-1))); }
@Test void testDescriptiveLongPredicateAndHasExpectedPredicate() { LongPredicate other = new DescriptiveLongPredicate("10 > " + AbstractDescriptivePredicate.TOKEN, t -> 10 > t); LongPredicate predicate = getNotValueTypeInstance().and(other); Assertions.assertAll("DescriptiveLongPredicate should wrap the expected predicate.", () -> Assertions.assertFalse(predicate.test(-1)), () -> Assertions.assertFalse(predicate.test(0)), () -> Assertions.assertTrue(predicate.test(1)), () -> Assertions.assertTrue(predicate.test(9)), () -> Assertions.assertFalse(predicate.test(10))); }
@Test void testDescriptiveLongPredicateOrHasExpectedPredicate() { LongPredicate other = new DescriptiveLongPredicate("10 > " + AbstractDescriptivePredicate.TOKEN, t -> 10 > t); LongPredicate predicate = getNotValueTypeInstance().or(other); Assertions.assertAll("DescriptiveLongPredicate should wrap the expected predicate.", () -> Assertions.assertTrue(predicate.test(-1)), () -> Assertions.assertTrue(predicate.test(0)), () -> Assertions.assertTrue(predicate.test(1)), () -> Assertions.assertTrue(predicate.test(9)), () -> Assertions.assertTrue(predicate.test(10))); }
/** * */ @Test public void shouldAcceptCacheAndPredicate() { // given final ConcurrentMap<Long, Boolean> cache = new ConcurrentHashMap<>(); final LongPredicate predicate = input -> true; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongPredicateMemoizer<Long> memoizer = new ConcurrentMapBasedLongPredicateMemoizer<>( cache, keyFunction, predicate); // then Assert.assertNotNull("Memoizer is NULL", memoizer); }
/** * */ @Test public void shouldMemoizeLongPredicateWithLambda() { // given // when final LongPredicate memoize = MapMemoize.longPredicate(input -> true); // then Assert.assertNotNull("Memoized LongPredicate is NULL", memoize); }
/** * @throws E if an exception E has been thrown, it is rethrown by this method * @return An interface that is only returned if no exception has been thrown. */ default LongPredicate thatUnsafelyThrowsUnchecked() throws E { return (final long v1) -> { try { return testWithThrowable(v1); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); return false; } }; }
/** * */ @Test public void shouldMemoizeLongPredicate() { // given final LongPredicate predicate = input -> true; // when final LongPredicate memoize = MapMemoize.longPredicate(predicate); // then Assert.assertNotNull("Memoized LongPredicate is NULL", memoize); }