All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.annimon.stream.internal.Operators Maven / Gradle / Ivy

The newest version!
package com.annimon.stream.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.annimon.stream.function.IntFunction;
import com.annimon.stream.iterator.PrimitiveIterator;

public final class Operators {

    private Operators() {
    }

    public static  List toList(Iterator iterator) {
        final List result = new ArrayList<>();
        while (iterator.hasNext()) {
            result.add(iterator.next());
        }
        return result;
    }

    public static  R[] toArray(Iterator iterator, IntFunction generator) {
        final List container = Operators. toList(iterator);
        final int size = container.size();

        Compat.checkMaxArraySize(size);

        //noinspection unchecked
        T[] source = container.toArray(Compat. newArray(size));
        R[] boxed = generator.apply(size);

        //noinspection SuspiciousSystemArraycopy
        System.arraycopy(source, 0, boxed, 0, size);
        return boxed;
    }

    public static int[] toIntArray(PrimitiveIterator.OfInt iterator) {
        final SpinedBuffer.OfInt b = new SpinedBuffer.OfInt();
        while (iterator.hasNext()) {
            b.accept(iterator.nextInt());
        }
        return b.asPrimitiveArray();
    }

    public static long[] toLongArray(PrimitiveIterator.OfLong iterator) {
        final SpinedBuffer.OfLong b = new SpinedBuffer.OfLong();
        while (iterator.hasNext()) {
            b.accept(iterator.nextLong());
        }
        return b.asPrimitiveArray();
    }

    public static double[] toDoubleArray(PrimitiveIterator.OfDouble iterator) {
        final SpinedBuffer.OfDouble b = new SpinedBuffer.OfDouble();
        while (iterator.hasNext()) {
            b.accept(iterator.nextDouble());
        }
        return b.asPrimitiveArray();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy