Example usage for java.util Spliterator trySplit

List of usage examples for java.util Spliterator trySplit

Introduction

In this page you can find the example usage for java.util Spliterator trySplit.

Prototype

Spliterator<T> trySplit();

Source Link

Document

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

Usage

From source file:org.javersion.util.PersistentHashSetTest.java

@Test
public void split_till_the_end() {
    PersistentHashSet<Integer> ints = new PersistentHashSet<Integer>().conjAll(integers());
    List<Spliterator<Integer>> spliterators = new ArrayList<>();
    spliterators.add(ints.spliterator());
    int size = 0;
    while (size != spliterators.size()) {
        size = spliterators.size();/*from www .  j  a v  a  2  s . com*/
        for (int i = size - 1; i >= 0; i--) {
            Spliterator<Integer> spliterator = spliterators.get(i);
            Spliterator<Integer> split = spliterator.trySplit();
            if (split != null) {
                spliterators.add(split);
            }
        }
    }
    final MutableLong sum = new MutableLong(0);
    for (Spliterator<Integer> spliterator : spliterators) {
        while (spliterator.tryAdvance(i -> sum.add(i)))
            ;
    }
    Assertions.assertThat(sum.longValue()).isEqualTo(ints.stream().map(Long::new).reduce(Long::sum).get());
}

From source file:org.javersion.util.PersistentHashSetTest.java

@Test
public void try_split_single_entry() {
    PersistentHashSet<Integer> set = new PersistentHashSet<Integer>().conj(5);
    assertThat(set.spliterator().trySplit()).isNull();

    Spliterator<Integer> spliterator = set.spliterator();
    assertThat(spliterator.tryAdvance(i -> assertThat(i).isEqualTo(5))).isTrue();
    assertThat(spliterator.trySplit()).isNull();
}

From source file:org.javersion.util.PersistentHashSetTest.java

@Test
public void try_split_sub_spliterator() {
    PersistentHashSet<Integer> set = new PersistentHashSet<Integer>().conj(1).conj(33);

    Spliterator<Integer> spliterator = set.spliterator();
    assertThat(spliterator.tryAdvance(i -> assertThat(i).isEqualTo(1))).isTrue();
    assertThat(spliterator.trySplit()).isNull();
    assertThat(spliterator.tryAdvance(i -> assertThat(i).isEqualTo(33))).isTrue();
    assertThat(spliterator.tryAdvance(i -> {
    })).isFalse();//from www  .  jav a 2s . c  o  m
}