Search in sources :

Example 1 with LongStream

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

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

Example 2 with LongStream

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

the class LongFlatMap method hasNext.

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

Example 3 with LongStream

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

the class CustomTest method testCustomLongermediateOperator_Zip.

@Test
public void testCustomLongermediateOperator_Zip() {
    final LongBinaryOperator op = new LongBinaryOperator() {

        @Override
        public long applyAsLong(long left, long right) {
            return left + right;
        }
    };
    LongStream s1 = LongStream.of(1, 3, 5, 7, 9);
    LongStream s2 = LongStream.of(2, 4, 6, 8);
    long[] expected = { 3, 7, 11, 15 };
    LongStream result = s1.custom(new CustomOperators.ZipLong(s2, op));
    assertThat(result.toArray(), is(expected));
}
Also used : LongBinaryOperator(com.annimon.stream.function.LongBinaryOperator) LongStream(com.annimon.stream.LongStream) CustomOperators(com.annimon.stream.CustomOperators) Test(org.junit.Test)

Example 4 with LongStream

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

the class LongStreamMatcherTest method testElements.

@Test
public void testElements() {
    final LongStream stream = LongStream.of(-813, 123456, Integer.MAX_VALUE);
    final Long[] expected = new Long[] { -813L, 123456L, (long) Integer.MAX_VALUE };
    final Matcher<LongStream> matcher = elements(arrayContaining(expected));
    assertThat(stream, matcher);
    assertTrue(matcher.matches(stream));
    assertFalse(elements(arrayContaining(expected)).matches(LongStream.empty()));
    assertThat(matcher, description(allOf(containsString("LongStream elements"), containsString(Stream.of(expected).map(new Function<Long, String>() {

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

Example 5 with LongStream

use of com.annimon.stream.LongStream 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]++;
        }
    };
    LongStream stream1 = LongStream.range(0, 2).onClose(runnable);
    LongStream stream2 = LongStream.range(0, 2).onClose(runnable);
    LongStream stream = LongStream.concat(stream1, stream2).onClose(runnable);
    stream.count();
    assertThat(counter[0], is(0));
    stream.close();
    assertThat(counter[0], is(3));
}
Also used : LongStream(com.annimon.stream.LongStream) Test(org.junit.Test)

Aggregations

LongStream (com.annimon.stream.LongStream)11 Test (org.junit.Test)9 CustomOperators (com.annimon.stream.CustomOperators)1 Function (com.annimon.stream.function.Function)1 LongBinaryOperator (com.annimon.stream.function.LongBinaryOperator)1 LongPredicate (com.annimon.stream.function.LongPredicate)1 LongSupplier (com.annimon.stream.function.LongSupplier)1 PrimitiveIterator (com.annimon.stream.iterator.PrimitiveIterator)1