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;
}
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(", "))))));
}
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))));
}
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))));
}
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))));
}
Aggregations