@Test public void DoublePredicate() { // TODO - Convert the anonymous inner class to a lambda DoublePredicate predicate = new DoublePredicate() { @Override public boolean test(double value) { return value > 3.0; } }; List<Double> greaterThan = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0).filter(predicate).boxed().collect(Collectors.toList()); Assert.assertEquals(Arrays.asList(4.0d, 5.0d), greaterThan); List<Double> lessThanEqualTo = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0).filter(predicate.negate()).boxed().collect(Collectors.toList()); Assert.assertEquals(Arrays.asList(1.0d, 2.0d, 3.0d), lessThanEqualTo); Assert.assertTrue(DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0).anyMatch(predicate)); Assert.assertFalse(DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0).allMatch(predicate)); Assert.assertFalse(DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0).noneMatch(predicate)); }
@Override public final DoubleStream filter(DoublePredicate predicate) { Objects.requireNonNull(predicate); return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SIZED) { @Override Sink<Double> opWrapSink(int flags, Sink<Double> sink) { return new Sink.ChainedDouble<Double>(sink) { @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(double t) { if (predicate.test(t)) downstream.accept(t); } }; } }; }
/** * Constructs a quantified predicate matcher for a {@code DoubleStream}. * * @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<Double, Boolean> makeDouble(DoublePredicate predicate, MatchKind matchKind) { Objects.requireNonNull(predicate); Objects.requireNonNull(matchKind); class MatchSink extends BooleanTerminalSink<Double> implements Sink.OfDouble { MatchSink() { super(matchKind); } @Override public void accept(double t) { if (!stop && predicate.test(t) == matchKind.stopOnPredicateMatches) { stop = true; value = matchKind.shortCircuitResult; } } } return new MatchOp<>(StreamShape.DOUBLE_VALUE, matchKind, MatchSink::new); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Double, Boolean> cache = null; final DoublePredicate predicate = input -> true; final DoubleFunction<Double> keyFunction = Double::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedDoublePredicateMemoizer<>(cache, keyFunction, predicate); }
/** * @throws ExecutionException * Added for the call to 'cache.get(..)'. */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException { // given final DoublePredicate predicate = a -> true; final DoubleFunction<String> keyFunction = a -> "key"; final Cache<String, Boolean> cache = Mockito.mock(Cache.class); given(cache.get(any(), any())).willThrow(ExecutionException.class); final GuavaCacheBasedDoublePredicateMemoizer<String> memoizer = new GuavaCacheBasedDoublePredicateMemoizer<>( cache, keyFunction, predicate); // when thrown.expect(MemoizationException.class); // then memoizer.test(123.456D); }
public OnGlobalAttributeCondition (IGame game, AttributeType attributeType, DoublePredicate valueCheck, IEventPackage otherPackage, IEventPackage globalPackage) { myGame = game; myAttributeType = attributeType; myValueCheck = valueCheck; myOtherPackage = otherPackage; myGlobalPackage = globalPackage; }
public OnSpriteAttributeCondition (IGame game, AttributeType attributeType, DoublePredicate valueCheck, IEventPackage spritePackage, IEventPackage otherPackage, IEventPackage globalPackage) { myGame = game; myAttributeType = attributeType; myValueCheck = valueCheck; mySpritePackage = spritePackage; myOtherPackage = otherPackage; myGlobalPackage = globalPackage; }
public DoublePredicate generateDouble (String value, String type) { double valToCompare = Double.parseDouble(value); if (type.equals("==")) { return new EqualsDoublePredicate(valToCompare); } else if (type.equals(">")) { return new GreaterThanDoublePredicate(valToCompare); } else { return new LessThanDoublePredicate(valToCompare); } }
@Test public void testDoublePredicate() { DoublePredicate isCorrect = (d) -> { return d > 2894d; }; assertTrue(isCorrect.test(2898d)); assertFalse(isCorrect.test(2751d)); assertFalse(isCorrect.test(38L)); }
private void assertDoublePredicates(Supplier<DoubleStream> source, Kind kind, DoublePredicate[] predicates, boolean... answers) { for (int i = 0; i < predicates.length; i++) { setContext("i", i); boolean match = doubleKinds.get(kind).apply(predicates[i]).apply(source.get()); assertEquals(answers[i], match, kind.toString() + predicates[i].toString()); } }
@Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) public void testDoubleStream(String name, TestData.OfDouble data) { for (DoublePredicate p : DOUBLE_PREDICATES) { setContext("p", p); for (Kind kind : Kind.values()) { setContext("kind", kind); exerciseTerminalOps(data, doubleKinds.get(kind).apply(p)); exerciseTerminalOps(data, s -> s.filter(dpFalse), doubleKinds.get(kind).apply(p)); exerciseTerminalOps(data, s -> s.filter(dpEven), doubleKinds.get(kind).apply(p)); } } }
@Test public void should_verify_an_actual_double_predicate_is_conform_to_an_expected_result() { assertThat(resultOf(() -> { gwtMock.whenAnEventHappensInRelationToAnActionOfTheConsumer(); return (DoublePredicate) number -> number < 100; }).accepts(99)).hasSameClassAs(assertThat((DoublePredicate) 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 */ RangeOfDoubles(double start, double end, double step, DoublePredicate excludes) { super(start, end); this.start = start; this.end = end; this.step = step; this.ascend = start <= end; this.excludes = excludes; }
JCacheBasedDoublePredicateMemoizer( final Cache<KEY, Boolean> cache, final DoubleFunction<KEY> keyFunction, final DoublePredicate predicate) { super(cache); this.keyFunction = keyFunction; this.predicate = predicate; }
@SuppressWarnings(CompilerWarnings.NLS) ConcurrentMapBasedDoublePredicateMemoizer( final ConcurrentMap<KEY, Boolean> cache, final DoubleFunction<KEY> keyFunction, final DoublePredicate predicate) { super(cache); this.keyFunction = keyFunction; this.predicate = requireNonNull(predicate, "Cannot memoize a NULL Predicate - provide an actual Predicate to fix this."); }
/** * @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 DoublePredicate thatUnsafelyThrowsUnchecked() throws E { return (final double v1) -> { try { return testWithThrowable(v1); } catch(final Throwable throwable) { SuppressedException.throwUnsafelyAsUnchecked(throwable); return false; } }; }
/** * Test whether all elements of a double[] array satisfy a double -> boolean predicate */ public static boolean allMatch(final double[] array, final DoublePredicate pred) { JointCallingUtils.nonNull(array, "array may not be null"); JointCallingUtils.nonNull(pred, "predicate may not be null"); for (final double x : array) { if (!pred.test(x)) { return false; } } return true; }
@Test void testDescribeDoublePredicateReturnsExpectedStringForNonDescriptive() { DoublePredicate predicate = t -> t > 0; Assertions.assertEquals(ValidityUtils.unknownPredicatePrefix() + predicate.toString(), ValidityUtils.describe(predicate), "Non descriptive predicate should be consistently described."); }
@Test void testDescriptiveDoublePredicateHasExpectedPredicate() { DoublePredicate predicate = getNotValueTypeInstance(); Assertions.assertAll("DescriptiveDoublePredicate should wrap the expected predicate.", () -> Assertions.assertTrue(predicate.test(1.0)), () -> Assertions.assertFalse(predicate.test(0.0)), () -> Assertions.assertFalse(predicate.test(-1.0))); }
/** * */ @Test public void shouldMemoizeDoublePredicateWithKeyFunction() { // given final DoublePredicate predicate = a -> true; final DoubleFunction<String> keyFunction = a -> "key"; // when final DoublePredicate memoize = JCacheMemoize.doublePredicate(predicate, keyFunction); // then Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize); }
@Test void testDescriptiveDoublePredicateAndHasExpectedPredicate() { DoublePredicate other = new DescriptiveDoublePredicate("10 > " + AbstractDescriptivePredicate.TOKEN, t -> 10 > t); DoublePredicate predicate = getNotValueTypeInstance().and(other); Assertions.assertAll("DescriptiveDoublePredicate should wrap the expected predicate.", () -> Assertions.assertFalse(predicate.test(-1.0)), () -> Assertions.assertFalse(predicate.test(0.0)), () -> Assertions.assertTrue(predicate.test(1.0)), () -> Assertions.assertTrue(predicate.test(9.0)), () -> Assertions.assertFalse(predicate.test(10.0))); }
/** * */ @Test public void shouldMemoizeDoublePredicateWithLambda() { // given // when final DoublePredicate memoize = CaffeineMemoize.doublePredicate(a -> true); // then Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize); }
@Test public void filter() { DoublePredicate p = d -> true; DoubleStream stream = this.parallelStreamSupportMock.filter(p); verify(this.delegateMock).filter(p); assertSame(this.parallelStreamSupportMock, stream); }
/** * */ @Test public void shouldMemoizeDoublePredicateWithLambda() { // given // when final DoublePredicate memoize = MapMemoize.doublePredicate(input -> true); // then Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize); }
/** * */ @Test public void shouldMemoizeDoublePredicate() { // given final DoublePredicate predicate = a -> true; // when final DoublePredicate memoize = JCacheMemoize.doublePredicate(predicate); // then Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize); }
/** * */ @Test public void shouldMemoizePredicateCall() { // given final ConcurrentMap<Double, Boolean> cache = new ConcurrentHashMap<>(); final DoublePredicate predicate = input -> true; final DoubleFunction<Double> keyFunction = Double::valueOf; // when final ConcurrentMapBasedDoublePredicateMemoizer<Double> memoizer = new ConcurrentMapBasedDoublePredicateMemoizer<>( cache, keyFunction, predicate); // then Assert.assertTrue("Memoized value does not match expectations", memoizer.test(123.456D)); }