Search in sources :

Example 1 with DoubleStream

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

the class OnCloseTest method testOnCloseFlatMap.

@Test
public void testOnCloseFlatMap() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            counter[0]++;
        }
    };
    DoubleStream stream = DoubleStream.of(2, 3, 4).onClose(runnable).onClose(runnable).flatMap(new DoubleFunction<DoubleStream>() {

        @Override
        public DoubleStream apply(final double i) {
            return DoubleStream.of(2, 3, 4).onClose(runnable);
        }
    });
    stream.count();
    assertThat(counter[0], is(3));
    stream.close();
    assertThat(counter[0], is(5));
}
Also used : DoubleStream(com.annimon.stream.DoubleStream) Test(org.junit.Test)

Example 2 with DoubleStream

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

the class OnCloseTest method testOnCloseConcat.

@Test
public void testOnCloseConcat() {
    final int[] counter = new int[] { 0 };
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            counter[0]++;
        }
    };
    DoubleStream stream1 = DoubleStream.of(0, 1).onClose(runnable);
    DoubleStream stream2 = DoubleStream.of(2, 3).onClose(runnable);
    DoubleStream stream = DoubleStream.concat(stream1, stream2).onClose(runnable);
    stream.count();
    assertThat(counter[0], is(0));
    stream.close();
    assertThat(counter[0], is(3));
}
Also used : DoubleStream(com.annimon.stream.DoubleStream) Test(org.junit.Test)

Example 3 with DoubleStream

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

the class GenerateTest method testStreamGenerate.

@Test
public void testStreamGenerate() {
    DoubleStream stream = DoubleStream.generate(new DoubleSupplier() {

        @Override
        public double getAsDouble() {
            return 1.234;
        }
    });
    assertThat(stream.limit(3), elements(arrayContaining(1.234, 1.234, 1.234)));
}
Also used : DoubleSupplier(com.annimon.stream.function.DoubleSupplier) DoubleStream(com.annimon.stream.DoubleStream) Test(org.junit.Test)

Example 4 with DoubleStream

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

the class ObjFlatMapToDouble 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 DoubleStream 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 : DoubleStream(com.annimon.stream.DoubleStream)

Example 5 with DoubleStream

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

the class ConcatTest method testStreamConcat.

@Test
public void testStreamConcat() {
    DoubleStream a1 = DoubleStream.empty();
    DoubleStream b1 = DoubleStream.empty();
    assertThat(DoubleStream.concat(a1, b1), isEmpty());
    DoubleStream a2 = DoubleStream.of(10.123, Math.PI);
    DoubleStream b2 = DoubleStream.empty();
    DoubleStream.concat(a2, b2).custom(assertElements(arrayContaining(10.123, Math.PI)));
    DoubleStream a3 = DoubleStream.of(10.123, Math.PI);
    DoubleStream b3 = DoubleStream.empty();
    DoubleStream.concat(a3, b3).custom(assertElements(arrayContaining(10.123, Math.PI)));
    DoubleStream a4 = DoubleStream.of(10.123, Math.PI, -1e11, -1e8);
    DoubleStream b4 = DoubleStream.of(1.617, 9.81);
    DoubleStream.concat(a4, b4).custom(assertElements(arrayContaining(10.123, Math.PI, -1e11, -1e8, 1.617, 9.81)));
}
Also used : DoubleStream(com.annimon.stream.DoubleStream) Test(org.junit.Test)

Aggregations

DoubleStream (com.annimon.stream.DoubleStream)12 Test (org.junit.Test)10 CustomOperators (com.annimon.stream.CustomOperators)1 IntStream (com.annimon.stream.IntStream)1 DoubleBinaryOperator (com.annimon.stream.function.DoubleBinaryOperator)1 DoublePredicate (com.annimon.stream.function.DoublePredicate)1 DoubleSupplier (com.annimon.stream.function.DoubleSupplier)1 Function (com.annimon.stream.function.Function)1 LongToDoubleFunction (com.annimon.stream.function.LongToDoubleFunction)1 PrimitiveIterator (com.annimon.stream.iterator.PrimitiveIterator)1