Example usage for java.util Spliterators spliterator

List of usage examples for java.util Spliterators spliterator

Introduction

In this page you can find the example usage for java.util Spliterators spliterator.

Prototype

public static Spliterator.OfDouble spliterator(PrimitiveIterator.OfDouble iterator, long size,
        int characteristics) 

Source Link

Document

Creates a Spliterator.OfDouble using a given DoubleStream.DoubleIterator as the source of elements, and with a given initially reported size.

Usage

From source file:com.simiacryptus.mindseye.applications.HadoopUtil.java

/**
 * To stream stream./* w  w  w  . j  a v  a2 s  .  co  m*/
 *
 * @param <T>            the type parameter
 * @param remoteIterator the remote iterator
 * @return the stream
 */
@Nonnull
public static <T> Stream<T> toStream(final RemoteIterator<T> remoteIterator) {
    return StreamSupport.stream(Spliterators.spliterator(new Iterator<T>() {
        @Override
        public boolean hasNext() {
            try {
                return remoteIterator.hasNext();
            } catch (Throwable e) {
                logger.warn("Error listing files", e);
                return false;
            }
        }

        @Override
        public T next() {
            try {
                return remoteIterator.next();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }, -1, Spliterator.IMMUTABLE), true);
}

From source file:net.dv8tion.jda.core.utils.cache.impl.SortedSnowflakeCacheView.java

@Override
public Spliterator<T> spliterator() {
    return Spliterators.spliterator(iterator(), elements.size(), SPLIT_CHARACTERISTICS);
}

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

/**
 * Creates a stream that wraps a stream with the index.
 * <p>// ww w. j  a va2 s  .com
 * Each input object is decorated with an {@link ObjIntPair}.
 * The {@code int} is the index of the element in the stream.
 * @param <T> the type of the stream
 * @param stream the stream to index
 * @return a stream of pairs, containing the element and index
 */
public static <T> Stream<ObjIntPair<T>> zipWithIndex(Stream<T> stream) {
    Spliterator<T> split1 = stream.spliterator();
    Iterator<T> it1 = Spliterators.iterator(split1);
    Iterator<ObjIntPair<T>> it = new Iterator<ObjIntPair<T>>() {
        int index = 0;

        @Override
        public boolean hasNext() {
            return it1.hasNext();
        }

        @Override
        public ObjIntPair<T> next() {
            return ObjIntPair.of(it1.next(), index++);
        }
    };
    Spliterator<ObjIntPair<T>> split = Spliterators.spliterator(it, split1.getExactSizeIfKnown(),
            split1.characteristics());
    return StreamSupport.stream(split, false);
}

From source file:com.wrmsr.kleist.util.Itertools.java

public static <T> Spliterator<EnumeratedElement<T>> enumerate(Spliterator<T> spliterator) {
    int characteristics = spliterator.characteristics() | Spliterator.NONNULL & ~Spliterator.CONCURRENT;
    return Spliterators.spliterator(enumerate(Spliterators.iterator(spliterator)), spliterator.estimateSize(),
            characteristics);/*from   ww w.j a v a  2s  .c  om*/
}

From source file:com.simiacryptus.mindseye.test.data.MNIST.java

private static <T> Stream<T> toIterator(@Nonnull final Iterator<T> iterator) {
    return StreamSupport.stream(Spliterators.spliterator(iterator, 1, Spliterator.ORDERED), false);
}

From source file:com.simiacryptus.mindseye.test.data.MNIST.java

private static <T> Stream<T> toStream(@Nonnull final Iterator<T> iterator, final int size,
        final boolean parallel) {
    return StreamSupport.stream(Spliterators.spliterator(iterator, size, Spliterator.ORDERED), parallel);
}

From source file:com.wrmsr.kleist.util.Itertools.java

public static <T> Stream<T> lazyStream(Supplier<? extends T> supplier) {
    return StreamSupport.stream(Spliterators.spliterator(lazyIterator(supplier), 1, Spliterator.SIZED), false);
}

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

/**
 * Creates a stream that combines two other streams, continuing until either stream ends.
 * <p>//  w w  w  . j  a v a  2s.  co  m
 * 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);
}

From source file:com.simiacryptus.util.Util.java

/**
 * To iterator stream.//from   ww w  . jav  a 2 s .  co  m
 *
 * @param <T>      the type parameter
 * @param iterator the iterator
 * @return the stream
 */
public static <T> Stream<T> toIterator(@javax.annotation.Nonnull final Iterator<T> iterator) {
    return StreamSupport.stream(Spliterators.spliterator(iterator, 1, Spliterator.ORDERED), false);
}

From source file:com.simiacryptus.util.Util.java

/**
 * To stream stream./*from  w  w  w .  ja v  a 2s  .c  o m*/
 *
 * @param <T>      the type parameter
 * @param iterator the iterator
 * @param size     the size
 * @param parallel the parallel
 * @return the stream
 */
public static <T> Stream<T> toStream(@javax.annotation.Nonnull final Iterator<T> iterator, final int size,
        final boolean parallel) {
    return StreamSupport.stream(Spliterators.spliterator(iterator, size, Spliterator.ORDERED), parallel);
}