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

The following examples show how to use java.util.stream.Stream#spliterator() . 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
Source File: GroupingOnGatingClosedOpenedSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: RollingSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: AccumulatingEntriesSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: InterruptingSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: GroupingOnSplittingSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: ZippingSpliteratorTest.java    From streams-utils with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: CrossProductNoDoublesSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: GatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: FilteringAllMaxSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: GatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: CrossProductNaturalyOrderedSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: CrossProductSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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
Source File: GatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: GatingSpliteratorTest.java    From streams-utils with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: ReactiveStreamX.java    From cyclops with 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 16
Source File: ReloadTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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 17
Source File: StreamUtils.java    From vertexium with 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 18
Source File: Util.java    From javan-warty-pig with 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
Source File: GroupingSpliterator.java    From terracotta-platform with Apache License 2.0 4 votes vote down vote up
public GroupingSpliterator(Stream<String> lines) {
  this(lines.spliterator());
}
 
Example 20
Source File: Functional.java    From mldht with 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;
}