Search in sources :

Example 6 with DoubleStream

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

the class DoubleFlatMap method hasNext.

@Override
public boolean hasNext() {
    if (inner != null && inner.hasNext()) {
        return true;
    }
    while (iterator.hasNext()) {
        if (innerStream != null) {
            innerStream.close();
            innerStream = null;
        }
        final double arg = iterator.nextDouble();
        final DoubleStream 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 : DoubleStream(com.annimon.stream.DoubleStream)

Example 7 with DoubleStream

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

the class DoubleStreamMatcherTest method testElements.

@Test
public void testElements() {
    final DoubleStream stream = DoubleStream.of(-0.987, 1.234, Math.PI, 1.618);
    final Double[] expected = new Double[] { -0.987, 1.234, Math.PI, 1.618 };
    final Matcher<DoubleStream> matcher = elements(arrayContaining(expected));
    assertThat(stream, matcher);
    assertTrue(matcher.matches(stream));
    assertFalse(elements(arrayContaining(expected)).matches(DoubleStream.empty()));
    assertThat(matcher, description(allOf(containsString("DoubleStream elements"), containsString(Stream.of(expected).map(new Function<Double, String>() {

        @Override
        public String apply(Double t) {
            return String.format("<%s>", t.toString());
        }
    }).collect(Collectors.joining(", "))))));
}
Also used : Function(com.annimon.stream.function.Function) DoubleStream(com.annimon.stream.DoubleStream) Test(org.junit.Test)

Example 8 with DoubleStream

use of com.annimon.stream.DoubleStream 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 9 with DoubleStream

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

the class MapToDoubleTest method testMapToDouble.

@Test
@SuppressWarnings("unchecked")
public void testMapToDouble() {
    DoubleStream stream = LongStream.rangeClosed(2, 4).mapToDouble(new LongToDoubleFunction() {

        @Override
        public double applyAsDouble(long value) {
            return value / 10d;
        }
    });
    assertThat(stream, DoubleStreamMatcher.elements(array(closeTo(0.2, 0.00001), closeTo(0.3, 0.00001), closeTo(0.4, 0.00001))));
}
Also used : DoubleStream(com.annimon.stream.DoubleStream) LongToDoubleFunction(com.annimon.stream.function.LongToDoubleFunction) Test(org.junit.Test)

Example 10 with DoubleStream

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

the class OfPrimitiveIteratorTest method testStreamOfPrimitiveIterator.

@Test
@SuppressWarnings("unchecked")
public void testStreamOfPrimitiveIterator() {
    DoubleStream stream = DoubleStream.of(new PrimitiveIterator.OfDouble() {

        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < 3;
        }

        @Override
        public double nextDouble() {
            index++;
            return index + 0.0021;
        }
    });
    assertThat(stream, elements(arrayContaining(closeTo(1.0021, 0.00001), closeTo(2.0021, 0.00001), closeTo(3.0021, 0.00001))));
}
Also used : PrimitiveIterator(com.annimon.stream.iterator.PrimitiveIterator) 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