Java Code Examples for java.util.stream.Stream#spliterator()

The following examples show how to use java.util.stream.Stream#spliterator() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("1", "2", "3", "4", "5", "6", "7");
    int groupingFactor = 2;

    Stream<Stream<String>> testedStream = StreamsUtils.roll(strings, groupingFactor);
    TryAdvanceCheckingSpliterator<Stream<String>> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false).flatMap(Function.identity());

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(12L);
}
 
Example 2
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three", "", "", "", "", "");
    Predicate<String> interruptor = String::isEmpty;

    Stream<String> testedStream = StreamsUtils.interrupt(strings, interruptor);
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 3
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three");
    Stream<Integer> ints = Stream.of(1, 2, 3);
    BiFunction<String, Integer, String> zip = (s, i) -> s + " - " + i;

    Stream<String> zippedStream = StreamsUtils.zip(strings, ints, zip);
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(zippedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 4
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    Predicate<String> splitter = s -> s.startsWith("o");

    Stream<Stream<String>> testedStream = StreamsUtils.group(strings, splitter);
    TryAdvanceCheckingSpliterator<Stream<String>> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false).flatMap(Function.identity());

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(11L);
}
 
Example 5
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<Map.Entry<Integer, String>> entries =
            Stream.of(
                    new AbstractMap.SimpleEntry<>(1, "1"),
                    new AbstractMap.SimpleEntry<>(2, "2"),
                    new AbstractMap.SimpleEntry<>(3, "3")
            );
    Stream<Map.Entry<Integer, String>> accumulatingStream = StreamsUtils.accumulateEntries(entries, String::concat);
    TryAdvanceCheckingSpliterator<Map.Entry<Integer, String>> spliterator = new TryAdvanceCheckingSpliterator<>(accumulatingStream.spliterator());
    Stream<Map.Entry<Integer, String>> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 6
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("o", "1", "2", "3", "4", "5", "6", "7", "8", "9", "c");
    Predicate<String> open = s -> s.startsWith("o");
    Predicate<String> close = s -> s.startsWith("c");

    Stream<Stream<String>> testedStream = StreamsUtils.group(strings, open, true, close, false);
    TryAdvanceCheckingSpliterator<Stream<String>> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false).flatMap(Function.identity());

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(10L);
}
 
Example 7
Source Project: vertexium   File: StreamUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static <TItem, TReturn> TReturn ifEmpty(
    Stream<TItem> stream,
    Supplier<TReturn> trueFunc,
    Function<Stream<TItem>, TReturn> falseFunc
) {
    Spliterator<TItem> split = stream.spliterator();
    AtomicReference<TItem> firstItem = new AtomicReference<>();
    if (split.tryAdvance(firstItem::set)) {
        Stream<TItem> newStream = Stream.concat(Stream.of(firstItem.get()), StreamSupport.stream(split, stream.isParallel()));
        return falseFunc.apply(newStream);
    } else {
        return trueFunc.get();
    }
}
 
Example 8
@Test(expectedExceptions = { ConcurrentModificationException.class })
public void testSpliteratorTryAdvance() {
    ServiceLoader<ScriptEngineFactory> sl = load(ScriptEngineFactory.class);
    Stream<Provider<ScriptEngineFactory>> stream = sl.stream();
    Spliterator<Provider<ScriptEngineFactory>> spliterator = stream.spliterator();
    sl.reload();
    spliterator.tryAdvance(System.out::println);
}
 
Example 9
Source Project: cyclops   File: ReactiveStreamX.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected <R> ReactiveSeq<R> createSeq(Stream<R> stream) {
    if (stream instanceof ReactiveSeq)
        return (ReactiveSeq) stream;
    if (stream instanceof Iterable)
        return new ReactiveStreamX<>(new IterableSourceOperator<>((Iterable<R>) stream));
    return new ReactiveStreamX<>(new SpliteratorToOperator<>(stream.spliterator()));
}
 
Example 10
@Test
public void should_conform_to_specified_trySplit_behavior_with_an_empty_stream() {
    // Given
    Stream<String> strings = Stream.empty();
    Stream<String> testedStream = StreamsUtils.gate(strings, s -> !s.isEmpty());
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(0L);
}
 
Example 11
@Test
public void should_conform_to_specified_trySplit_behavior_with_a_fully_invalid__stream() {
    // Given
    Stream<String> strings = Stream.of("", "", "");
    Stream<String> testedStream = StreamsUtils.gate(strings, s -> !s.isEmpty());
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(0L);
}
 
Example 12
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("a", "d", "c", "b");
    Stream<Map.Entry<String, String>> stream = StreamsUtils.crossProduct(strings);
    TryAdvanceCheckingSpliterator<Map.Entry<String, String>> spliterator = new TryAdvanceCheckingSpliterator<>(stream.spliterator());
    Stream<Map.Entry<String, String>> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(16L);
}
 
Example 13
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("a", "d", "c", "b");
    Stream<Map.Entry<String, String>> stream = StreamsUtils.crossProductNoDoubles(strings);
    TryAdvanceCheckingSpliterator<Map.Entry<String, String>> spliterator = new TryAdvanceCheckingSpliterator<>(stream.spliterator());
    Stream<Map.Entry<String, String>> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(12L);
}
 
Example 14
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Stream.of("a", "d", "c", "b");
    Stream<Map.Entry<String, String>> stream = StreamsUtils.crossProductNaturallyOrdered(strings);
    TryAdvanceCheckingSpliterator<Map.Entry<String, String>> spliterator = new TryAdvanceCheckingSpliterator<>(stream.spliterator());
    Stream<Map.Entry<String, String>> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(6L);
}
 
Example 15
@Test
public void should_conform_to_specified_trySplit_behavior_with_a_fully_valid__stream() {
    // Given
    Stream<String> strings = Stream.of("one", "two", "three");
    Stream<String> testedStream = StreamsUtils.gate(strings, s -> !s.isEmpty());
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 16
@Test
public void should_conform_to_specified_trySplit_behavior() {
    // Given
    Stream<String> strings = Arrays.asList("one", "two", "three").stream();
    Stream<String> testedStream = StreamsUtils.filteringAllMax(strings);
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(1L);
}
 
Example 17
@Test
public void should_conform_to_specified_trySplit_behavior_with_a_normal_stream() {
    // Given
    Stream<String> strings = Stream.of("", "", "", "", "one", "two", "three");
    Stream<String> testedStream = StreamsUtils.gate(strings, s -> !s.isEmpty());
    TryAdvanceCheckingSpliterator<String> spliterator = new TryAdvanceCheckingSpliterator<>(testedStream.spliterator());
    Stream<String> monitoredStream = StreamSupport.stream(spliterator, false);

    // When
    long count = monitoredStream.count();

    // Then
    assertThat(count).isEqualTo(3L);
}
 
Example 18
Source Project: javan-warty-pig   File: Util.java    License: MIT License 4 votes vote down vote up
public static <T> Stream<T> streamCharacteristics(Stream<T> stream, IntConsumer consumer) {
  Spliterator<T> spliterator = stream.spliterator();
  consumer.accept(spliterator.characteristics());
  return StreamSupport.stream(spliterator, stream.isParallel());
}
 
Example 19
public GroupingSpliterator(Stream<String> lines) {
  this(lines.spliterator());
}
 
Example 20
Source Project: mldht   File: Functional.java    License: Mozilla Public License 2.0 3 votes vote down vote up
/**
 * workaround for https://bugs.openjdk.java.net/browse/JDK-8075939
 */
public static <R, T> Stream<R> shortCircuitingflatMap(Stream<T> st, Function<T, Stream<R>> flatMapper) {
	Spliterator<T> sourceSpliterator = st.spliterator();
	
	ShortCircuitFlatMapSpliterator<R,T> sinkSpliterator = new ShortCircuitFlatMapSpliterator<>(sourceSpliterator, flatMapper);
	Stream<R> resultStream = StreamSupport.stream(sinkSpliterator, false);
	
	resultStream = resultStream.onClose(st::close).onClose(sinkSpliterator::close);
	
	
	return resultStream;
}