Example usage for java.util Spliterator SORTED

List of usage examples for java.util Spliterator SORTED

Introduction

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

Prototype

int SORTED

To view the source code for java.util Spliterator SORTED.

Click Source Link

Document

Characteristic value signifying that encounter order follows a defined sort order.

Usage

From source file:com.github.steveash.guavate.Guavate.java

/**
 * Creates a stream that combines two other streams, continuing until either stream ends.
 * <p>/*from   ww w  . j  a  v a2  s  . c om*/
 * The combiner function is called once for each pair of objects found in the input streams.
 * @param <A> the type of the first stream
 * @param <B> the type of the second stream
 * @param <R> the type of the resulting stream
 * @param stream1 the first stream
 * @param stream2 the first stream
 * @param zipper the function used to combine the pair of objects
 * @return a stream of pairs, one from each stream
 */
private static <A, B, R> Stream<R> zip(Stream<A> stream1, Stream<B> stream2, BiFunction<A, B, R> zipper) {
    // this is private for now, to see if it is really needed on the API
    // it suffers from generics problems at the call site with common zipper functions
    // as such, it is less useful than it might seem
    Spliterator<A> split1 = stream1.spliterator();
    Spliterator<B> split2 = stream2.spliterator();
    // merged stream lacks some characteristics
    int characteristics = split1.characteristics() & split2.characteristics()
            & ~(Spliterator.DISTINCT | Spliterator.SORTED);
    long size = Math.min(split1.getExactSizeIfKnown(), split2.getExactSizeIfKnown());

    Iterator<A> it1 = Spliterators.iterator(split1);
    Iterator<B> it2 = Spliterators.iterator(split2);
    Iterator<R> it = new Iterator<R>() {
        @Override
        public boolean hasNext() {
            return it1.hasNext() && it2.hasNext();
        }

        @Override
        public R next() {
            return zipper.apply(it1.next(), it2.next());
        }
    };
    Spliterator<R> split = Spliterators.spliterator(it, size, characteristics);
    return StreamSupport.stream(split, false);
}