Search in sources :

Example 1 with IntPredicate

use of com.annimon.stream.function.IntPredicate in project Lightweight-Stream-API by aNNiMON.

the class AnyMatchTest method testAnyMatch.

@Test
public void testAnyMatch() {
    IntStream.empty().anyMatch(new IntPredicate() {

        @Override
        public boolean test(int value) {
            throw new IllegalStateException();
        }
    });
    assertTrue(IntStream.of(42).anyMatch(new IntPredicate() {

        @Override
        public boolean test(int value) {
            return value == 42;
        }
    }));
    assertTrue(IntStream.of(5, 7, 9, 10, 7, 5).anyMatch(Functions.remainderInt(2)));
    assertFalse(IntStream.of(5, 7, 9, 11, 7, 5).anyMatch(Functions.remainderInt(2)));
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) Test(org.junit.Test)

Example 2 with IntPredicate

use of com.annimon.stream.function.IntPredicate in project Lightweight-Stream-API by aNNiMON.

the class FilterTest method testFilter.

@Test
public void testFilter() {
    IntStream.rangeClosed(1, 10).filter(Functions.remainderInt(2)).custom(assertElements(arrayContaining(2, 4, 6, 8, 10)));
    assertTrue(IntStream.iterate(0, new IntUnaryOperator() {

        @Override
        public int applyAsInt(int operand) {
            return operand + 1;
        }
    }).filter(new IntPredicate() {

        @Override
        public boolean test(int value) {
            return value == 0;
        }
    }).findFirst().getAsInt() == 0);
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) IntUnaryOperator(com.annimon.stream.function.IntUnaryOperator) Test(org.junit.Test)

Example 3 with IntPredicate

use of com.annimon.stream.function.IntPredicate in project Lightweight-Stream-API by aNNiMON.

the class FilterTest method testFilterWithMap.

@Test
public void testFilterWithMap() {
    final List<Integer> items = Arrays.asList(0, 1, 2, 3, 4);
    IntStream.range(0, items.size()).filter(new IntPredicate() {

        @Override
        public boolean test(int value) {
            return items.get(value) % 2 == 0;
        }
    }).mapToObj(new IntFunction<String>() {

        @Override
        public String apply(int value) {
            return Integer.toString(value);
        }
    }).custom(StreamMatcher.assertElements(contains("0", "2", "4")));
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) IntFunction(com.annimon.stream.function.IntFunction) Test(org.junit.Test)

Example 4 with IntPredicate

use of com.annimon.stream.function.IntPredicate in project Lightweight-Stream-API by aNNiMON.

the class NoneMatchTest method testNoneMatch.

@Test
public void testNoneMatch() {
    IntStream.empty().noneMatch(new IntPredicate() {

        @Override
        public boolean test(int value) {
            throw new IllegalStateException();
        }
    });
    assertFalse(IntStream.of(42).noneMatch(new IntPredicate() {

        @Override
        public boolean test(int value) {
            return value == 42;
        }
    }));
    assertFalse(IntStream.of(5, 7, 9, 10, 7, 5).noneMatch(Functions.remainderInt(2)));
    assertTrue(IntStream.of(5, 7, 9, 11, 7, 5).noneMatch(Functions.remainderInt(2)));
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) Test(org.junit.Test)

Example 5 with IntPredicate

use of com.annimon.stream.function.IntPredicate in project Lightweight-Stream-API by aNNiMON.

the class OnCloseTest method testOnCloseWithOtherOperators.

@Test
public void testOnCloseWithOtherOperators() {
    final boolean[] state = new boolean[] { false };
    IntStream stream = IntStream.of(0, 1, 2, 2, 3, 4, 4, 5).filter(new IntPredicate() {

        @Override
        public boolean test(int value) {
            return value < 4;
        }
    }).onClose(new Runnable() {

        @Override
        public void run() {
            state[0] = true;
        }
    }).distinct().limit(2);
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Aggregations

IntPredicate (com.annimon.stream.function.IntPredicate)7 Test (org.junit.Test)7 IntUnaryOperator (com.annimon.stream.function.IntUnaryOperator)2 IntStream (com.annimon.stream.IntStream)1 IntFunction (com.annimon.stream.function.IntFunction)1