Search in sources :

Example 1 with IntStream

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

the class OnCloseTest method testOnClose.

@Test
public void testOnClose() {
    final boolean[] state = new boolean[] { false };
    IntStream stream = IntStream.of(0, 1, 2).onClose(new Runnable() {

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

Example 2 with IntStream

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

the class SortedTest method testSortedLazy.

@Test
public void testSortedLazy() {
    int[] expected = { -7, 0, 3, 6, 9, 19 };
    List<Integer> input = new ArrayList<Integer>(6);
    input.addAll(Arrays.asList(6, 3, 9));
    IntStream stream = Stream.of(input).mapToInt(Functions.toInt()).sorted();
    input.addAll(Arrays.asList(0, -7, 19));
    assertThat(stream.toArray(), is(expected));
}
Also used : ArrayList(java.util.ArrayList) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Example 3 with IntStream

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

the class CustomTest method testCustomIntermediateOperator_Zip.

@Test
@SuppressWarnings("unchecked")
public void testCustomIntermediateOperator_Zip() {
    final DoubleBinaryOperator op = new DoubleBinaryOperator() {

        @Override
        public double applyAsDouble(double left, double right) {
            return left * right;
        }
    };
    DoubleStream s1 = DoubleStream.of(1.01, 2.02, 3.03);
    IntStream s2 = IntStream.range(2, 5);
    DoubleStream result = s1.custom(new CustomOperators.ZipWithIntStream(s2, op));
    assertThat(result, elements(arrayContaining(closeTo(2.02, 0.00001), closeTo(6.06, 0.00001), closeTo(12.12, 0.00001))));
}
Also used : DoubleBinaryOperator(com.annimon.stream.function.DoubleBinaryOperator) DoubleStream(com.annimon.stream.DoubleStream) IntStream(com.annimon.stream.IntStream) CustomOperators(com.annimon.stream.CustomOperators) Test(org.junit.Test)

Example 4 with IntStream

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

the class ObjFlatMapToInt method nextIteration.

@Override
protected void nextIteration() {
    if ((inner != null) && inner.hasNext()) {
        next = inner.next();
        hasNext = true;
        return;
    }
    while (iterator.hasNext()) {
        if (inner == null || !inner.hasNext()) {
            final T arg = iterator.next();
            final IntStream result = mapper.apply(arg);
            if (result != null) {
                inner = result.iterator();
            }
        }
        if ((inner != null) && inner.hasNext()) {
            next = inner.next();
            hasNext = true;
            return;
        }
    }
    hasNext = false;
}
Also used : IntStream(com.annimon.stream.IntStream)

Example 5 with IntStream

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

the class IntFlatMap method hasNext.

@Override
public boolean hasNext() {
    if (inner != null && inner.hasNext()) {
        return true;
    }
    while (iterator.hasNext()) {
        if (innerStream != null) {
            innerStream.close();
            innerStream = null;
        }
        final int arg = iterator.nextInt();
        final IntStream result = mapper.apply(arg);
        if (result == null) {
            continue;
        }
        innerStream = result;
        if (result.iterator().hasNext()) {
            inner = result.iterator();
            return true;
        }
    }
    if (innerStream != null) {
        innerStream.close();
        innerStream = null;
    }
    return false;
}
Also used : IntStream(com.annimon.stream.IntStream)

Aggregations

IntStream (com.annimon.stream.IntStream)13 Test (org.junit.Test)11 CustomOperators (com.annimon.stream.CustomOperators)2 ArrayList (java.util.ArrayList)2 DoubleStream (com.annimon.stream.DoubleStream)1 DoubleBinaryOperator (com.annimon.stream.function.DoubleBinaryOperator)1 Function (com.annimon.stream.function.Function)1 IntBinaryOperator (com.annimon.stream.function.IntBinaryOperator)1 IntPredicate (com.annimon.stream.function.IntPredicate)1 PrimitiveIterator (com.annimon.stream.iterator.PrimitiveIterator)1