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

com.gs.collections.impl.utility.internal.IteratorIterate Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2013 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.impl.utility.internal;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;

import com.gs.collections.api.RichIterable;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.Function3;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableByteCollection;
import com.gs.collections.api.collection.primitive.MutableCharCollection;
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.collection.primitive.MutableLongCollection;
import com.gs.collections.api.collection.primitive.MutableShortCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.ImmutableMultimap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.partition.PartitionMutableCollection;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.utility.Iterate;

/**
 * The IteratorIterate class implementations of the various iteration patterns for use with java.util.Iterator.
 */
public final class IteratorIterate
{
    private IteratorIterate()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * @see Iterate#selectAndRejectWith(Iterable, Predicate2, Object)
     */
    public static  Twin> selectAndRejectWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter)
    {
        MutableList positiveResult = Lists.mutable.of();
        MutableList negativeResult = Lists.mutable.of();
        while (iterator.hasNext())
        {
            T item = iterator.next();
            (predicate.accept(item, parameter) ? positiveResult : negativeResult).add(item);
        }
        return Tuples.twin(positiveResult, negativeResult);
    }

    /**
     * @see Iterate#partition(Iterable, Predicate)
     */
    public static  PartitionMutableList partition(Iterator iterator, Predicate predicate)
    {
        PartitionMutableList result = new PartitionFastList();
        MutableList selected = result.getSelected();
        MutableList rejected = result.getRejected();

        while (iterator.hasNext())
        {
            T each = iterator.next();
            MutableList bucket = predicate.accept(each) ? selected : rejected;
            bucket.add(each);
        }
        return result;
    }

    /**
     * @see Iterate#partitionWith(Iterable, Predicate2, Object)
     *
     * @since 5.0
     */
    public static  PartitionMutableList partitionWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter)
    {
        PartitionMutableList result = new PartitionFastList();
        MutableList selected = result.getSelected();
        MutableList rejected = result.getRejected();

        while (iterator.hasNext())
        {
            T each = iterator.next();
            MutableList bucket = predicate.accept(each, parameter) ? selected : rejected;
            bucket.add(each);
        }
        return result;
    }

    public static > R partitionWhile(Iterator iterator, Predicate predicate, R target)
    {
        MutableCollection selected = target.getSelected();
        MutableCollection rejected = target.getRejected();

        boolean partitionFound = false;
        while (iterator.hasNext() && !partitionFound)
        {
            T next = iterator.next();
            if (predicate.accept(next))
            {
                selected.add(next);
            }
            else
            {
                rejected.add(next);
                partitionFound = true;
            }
        }
        while (iterator.hasNext())
        {
            rejected.add(iterator.next());
        }
        return target;
    }

    public static > R takeWhile(Iterator iterator, Predicate predicate, R target)
    {
        while (iterator.hasNext())
        {
            T next = iterator.next();
            if (predicate.accept(next))
            {
                target.add(next);
            }
            else
            {
                return target;
            }
        }
        return target;
    }

    public static > R dropWhile(Iterator iterator, Predicate predicate, R target)
    {
        boolean partitionFound = false;
        while (iterator.hasNext() && !partitionFound)
        {
            T next = iterator.next();
            if (!predicate.accept(next))
            {
                target.add(next);
                partitionFound = true;
            }
        }
        while (iterator.hasNext())
        {
            target.add(iterator.next());
        }
        return target;
    }

    /**
     * @see Iterate#count(Iterable, Predicate)
     */
    public static  int count(Iterator iterator, Predicate predicate)
    {
        int count = 0;
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                count++;
            }
        }
        return count;
    }

    /**
     * @see Iterate#countWith(Iterable, Predicate2, Object)
     */
    public static  int countWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter)
    {
        int count = 0;
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next(), parameter))
            {
                count++;
            }
        }
        return count;
    }

    /**
     * @see Iterate#select(Iterable, Predicate, Collection)
     */
    public static > R select(
            Iterator iterator,
            Predicate predicate,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (predicate.accept(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
     */
    public static > R selectWith(
            Iterator iterator,
            Predicate2 predicate,
            P injectedValue,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (predicate.accept(item, injectedValue))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#selectInstancesOf(Iterable, Class)
     */
    public static > R selectInstancesOf(
            Iterator iterator,
            Class clazz,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            Object item = iterator.next();
            if (clazz.isInstance(item))
            {
                targetCollection.add((T) item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
     */
    public static > R collectIf(
            Iterator iterator,
            Predicate predicate,
            Function function,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (predicate.accept(item))
            {
                targetCollection.add(function.valueOf(item));
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#reject(Iterable, Predicate, Collection)
     */
    public static > R reject(
            Iterator iterator,
            Predicate predicate,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (!predicate.accept(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#rejectWith(Iterable, Predicate2, Object, Collection)
     */
    public static > R rejectWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (!predicate.accept(item, parameter))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#collect(Iterable, Function, Collection)
     */
    public static > R collect(
            Iterator iterator,
            Function function,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            targetCollection.add(function.valueOf(iterator.next()));
        }
        return targetCollection;
    }

    /**
     * @see Iterate#collectBoolean(Iterable, BooleanFunction)
     */
    public static  MutableBooleanCollection collectBoolean(
            Iterator iterator,
            BooleanFunction booleanFunction)
    {
        MutableBooleanCollection result = new BooleanArrayList();
        while (iterator.hasNext())
        {
            result.add(booleanFunction.booleanValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectBoolean(Iterable, BooleanFunction, MutableBooleanCollection)
     */
    public static  R collectBoolean(
            Iterator iterator,
            BooleanFunction booleanFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(booleanFunction.booleanValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectByte(Iterable, ByteFunction)
     */
    public static  MutableByteCollection collectByte(
            Iterator iterator,
            ByteFunction byteFunction)
    {
        MutableByteCollection result = new ByteArrayList();
        while (iterator.hasNext())
        {
            result.add(byteFunction.byteValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectByte(Iterable, ByteFunction,
     *      MutableByteCollection)
     */
    public static  R collectByte(
            Iterator iterator,
            ByteFunction byteFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(byteFunction.byteValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectChar(Iterable, CharFunction)
     */
    public static  MutableCharCollection collectChar(
            Iterator iterator,
            CharFunction charFunction)
    {
        MutableCharCollection result = new CharArrayList();
        while (iterator.hasNext())
        {
            result.add(charFunction.charValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectChar(Iterable, CharFunction,
     *      MutableCharCollection)
     */
    public static  R collectChar(
            Iterator iterator,
            CharFunction charFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(charFunction.charValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectDouble(Iterable, DoubleFunction)
     */
    public static  MutableDoubleCollection collectDouble(
            Iterator iterator,
            DoubleFunction doubleFunction)
    {
        MutableDoubleCollection result = new DoubleArrayList();
        while (iterator.hasNext())
        {
            result.add(doubleFunction.doubleValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectDouble(Iterable, DoubleFunction,
     *      MutableDoubleCollection)
     */
    public static  R collectDouble(
            Iterator iterator,
            DoubleFunction doubleFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(doubleFunction.doubleValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectFloat(Iterable, FloatFunction)
     */
    public static  MutableFloatCollection collectFloat(
            Iterator iterator,
            FloatFunction floatFunction)
    {
        MutableFloatCollection result = new FloatArrayList();
        while (iterator.hasNext())
        {
            result.add(floatFunction.floatValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectFloat(Iterable, FloatFunction,
     *      MutableFloatCollection)
     */
    public static  R collectFloat(
            Iterator iterator,
            FloatFunction floatFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(floatFunction.floatValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectInt(Iterable, IntFunction)
     */
    public static  MutableIntCollection collectInt(
            Iterator iterator,
            IntFunction intFunction)
    {
        MutableIntCollection result = new IntArrayList();
        while (iterator.hasNext())
        {
            result.add(intFunction.intValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectInt(Iterable, IntFunction,
     *      MutableIntCollection)
     */
    public static  R collectInt(
            Iterator iterator,
            IntFunction intFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(intFunction.intValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectLong(Iterable, LongFunction)
     */
    public static  MutableLongCollection collectLong(
            Iterator iterator,
            LongFunction longFunction)
    {
        MutableLongCollection result = new LongArrayList();
        while (iterator.hasNext())
        {
            result.add(longFunction.longValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectLong(Iterable, LongFunction,
     *      MutableLongCollection)
     */
    public static  R collectLong(
            Iterator iterator,
            LongFunction longFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(longFunction.longValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#collectShort(Iterable, ShortFunction)
     */
    public static  MutableShortCollection collectShort(
            Iterator iterator,
            ShortFunction shortFunction)
    {
        MutableShortCollection result = new ShortArrayList();
        while (iterator.hasNext())
        {
            result.add(shortFunction.shortValueOf(iterator.next()));
        }
        return result;
    }

    /**
     * @see Iterate#collectShort(Iterable, ShortFunction,
     *      MutableShortCollection)
     */
    public static  R collectShort(
            Iterator iterator,
            ShortFunction shortFunction,
            R target)
    {
        while (iterator.hasNext())
        {
            target.add(shortFunction.shortValueOf(iterator.next()));
        }
        return target;
    }

    /**
     * @see Iterate#flatCollect(Iterable, Function, Collection)
     */
    public static > R flatCollect(
            Iterator iterator,
            Function> function,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            Iterate.addAllTo(function.valueOf(iterator.next()), targetCollection);
        }
        return targetCollection;
    }

    /**
     * @see Iterate#forEach(Iterable, Procedure)
     */
    public static  void forEach(Iterator iterator, Procedure procedure)
    {
        while (iterator.hasNext())
        {
            procedure.value(iterator.next());
        }
    }

    /**
     * @see Iterate#forEachWithIndex(Iterable, ObjectIntProcedure)
     */
    public static  void forEachWithIndex(Iterator iterator, ObjectIntProcedure objectIntProcedure)
    {
        int i = 0;
        while (iterator.hasNext())
        {
            objectIntProcedure.value(iterator.next(), i++);
        }
    }

    /**
     * @see Iterate#detect(Iterable, Predicate)
     */
    public static  T detect(Iterator iterator, Predicate predicate)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (predicate.accept(item))
            {
                return item;
            }
        }
        return null;
    }

    /**
     * @see Iterate#detectWith(Iterable, Predicate2, Object)
     */
    public static  T detectWith(
            Iterator iterator,
            Predicate2 predicate,
            IV injectedValue)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (predicate.accept(item, injectedValue))
            {
                return item;
            }
        }
        return null;
    }

    /**
     * @see Iterate#injectInto(Object, Iterable, Function2)
     */
    public static  IV injectInto(
            IV injectValue,
            Iterator iterator,
            Function2 function)
    {
        IV result = injectValue;
        while (iterator.hasNext())
        {
            result = function.value(result, iterator.next());
        }
        return result;
    }

    /**
     * @see Iterate#injectInto(int, Iterable, IntObjectToIntFunction)
     */
    public static  int injectInto(
            int injectValue,
            Iterator iterator,
            IntObjectToIntFunction function)
    {
        int result = injectValue;
        while (iterator.hasNext())
        {
            result = function.intValueOf(result, iterator.next());
        }
        return result;
    }

    /**
     * @see Iterate#injectInto(long, Iterable, LongObjectToLongFunction)
     */
    public static  long injectInto(
            long injectValue,
            Iterator iterator,
            LongObjectToLongFunction function)
    {
        long result = injectValue;
        while (iterator.hasNext())
        {
            result = function.longValueOf(result, iterator.next());
        }
        return result;
    }

    /**
     * @see Iterate#injectInto(double, Iterable, DoubleObjectToDoubleFunction)
     */
    public static  double injectInto(
            double injectValue,
            Iterator iterator,
            DoubleObjectToDoubleFunction function)
    {
        double result = injectValue;
        while (iterator.hasNext())
        {
            result = function.doubleValueOf(result, iterator.next());
        }
        return result;
    }

    /**
     * @see Iterate#injectInto(float, Iterable, FloatObjectToFloatFunction)
     */
    public static  float injectInto(
            float injectValue,
            Iterator iterator,
            FloatObjectToFloatFunction function)
    {
        float result = injectValue;
        while (iterator.hasNext())
        {
            result = function.floatValueOf(result, iterator.next());
        }
        return result;
    }

    /**
     * @see Iterate#injectIntoWith(Object, Iterable, Function3, Object)
     */
    public static  IV injectIntoWith(IV injectValue, Iterator iterator, Function3 function, P parameter)
    {
        IV result = injectValue;
        while (iterator.hasNext())
        {
            result = function.value(result, iterator.next(), parameter);
        }
        return result;
    }

    /**
     * @see Iterate#anySatisfy(Iterable, Predicate)
     */
    public static  boolean anySatisfy(Iterator iterator, Predicate predicate)
    {
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * @see Iterate#anySatisfyWith(Iterable, Predicate2, Object)
     */
    public static  boolean anySatisfyWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter)
    {
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next(), parameter))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * @see Iterate#allSatisfy(Iterable, Predicate)
     */
    public static  boolean allSatisfy(Iterator iterator, Predicate predicate)
    {
        while (iterator.hasNext())
        {
            if (!predicate.accept(iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

    /**
     * @see Iterate#allSatisfyWith(Iterable, Predicate2, Object)
     */
    public static  boolean allSatisfyWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter)
    {
        while (iterator.hasNext())
        {
            if (!predicate.accept(iterator.next(), parameter))
            {
                return false;
            }
        }
        return true;
    }

    /**
     * @see Iterate#noneSatisfy(Iterable, Predicate)
     */
    public static  boolean noneSatisfy(Iterator iterator, Predicate predicate)
    {
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

    /**
     * @see Iterate#allSatisfyWith(Iterable, Predicate2, Object)
     */
    public static  boolean noneSatisfyWith(
            Iterator iterator,
            Predicate2 predicate,
            P parameter)
    {
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next(), parameter))
            {
                return false;
            }
        }
        return true;
    }

    /**
     * @see Iterate#removeIf(Iterable, Predicate)
     */
    public static  Iterator removeIf(Iterator iterator, Predicate predicate)
    {
        while (iterator.hasNext())
        {
            T each = iterator.next();
            if (predicate.accept(each))
            {
                iterator.remove();
            }
        }
        return iterator;
    }

    /**
     * @see Iterate#removeIfWith(Iterable, Predicate2, Object)
     */
    public static  Iterator removeIfWith(Iterator iterator, Predicate2 predicate, P parameter)
    {
        while (iterator.hasNext())
        {
            T each = iterator.next();
            if (predicate.accept(each, parameter))
            {
                iterator.remove();
            }
        }
        return iterator;
    }

    public static  Iterator removeIf(Iterator iterator, Predicate predicate, Procedure procedure)
    {
        while (iterator.hasNext())
        {
            T each = iterator.next();
            if (predicate.accept(each))
            {
                procedure.value(each);
                iterator.remove();
            }
        }
        return iterator;
    }

    /**
     * @see Iterate#detectIndex(Iterable, Predicate)
     */
    public static  int detectIndex(Iterator iterator, Predicate predicate)
    {
        int i = 0;
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                return i;
            }
            i++;
        }
        return -1;
    }

    /**
     * @see Iterate#detectIndexWith(Iterable, Predicate2, Object)
     */
    public static  int detectIndexWith(Iterator iterator, Predicate2 predicate, IV injectedValue)
    {
        int i = 0;
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next(), injectedValue))
            {
                return i;
            }
            i++;
        }
        return -1;
    }

    /**
     * @see Iterate#forEachWith(Iterable, Procedure2, Object)
     */
    public static  void forEachWith(Iterator iterator, Procedure2 procedure, P parameter)
    {
        while (iterator.hasNext())
        {
            procedure.value(iterator.next(), parameter);
        }
    }

    /**
     * @see Iterate#collectWith(Iterable, Function2, Object, Collection)
     */
    public static > R collectWith(
            Iterator iterator,
            Function2 function,
            P parameter,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            targetCollection.add(function.value(iterator.next(), parameter));
        }
        return targetCollection;
    }

    /**
     * @see Iterate#groupBy(Iterable, Function)
     */
    public static  ImmutableMultimap groupBy(
            Iterator iterator,
            Function function)
    {
        return IteratorIterate.groupBy(iterator, function, new FastListMultimap()).toImmutable();
    }

    /**
     * @see Iterate#groupBy(Iterable, Function, MutableMultimap)
     */
    public static > R groupBy(
            Iterator iterator,
            Function function,
            R target)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            target.put(function.valueOf(item), item);
        }
        return target;
    }

    /**
     * @see Iterate#groupByEach(Iterable, Function, MutableMultimap)
     */
    public static > R groupByEach(
            Iterator iterator,
            Function> function,
            R target)
    {
        while (iterator.hasNext())
        {
            T item = iterator.next();
            Iterable iterable = function.valueOf(item);
            for (V key : iterable)
            {
                target.put(key, item);
            }
        }
        return target;
    }

    public static > R distinct(
            Iterator iterator,
            R targetCollection)
    {
        Set seenSoFar = UnifiedSet.newSet();
        while (iterator.hasNext())
        {
            T item = iterator.next();
            if (seenSoFar.add(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#zip(Iterable, Iterable, Collection)
     */
    public static >> R zip(
            Iterator xs,
            Iterator ys,
            R target)
    {
        while (xs.hasNext() && ys.hasNext())
        {
            target.add(Tuples.pair(xs.next(), ys.next()));
        }
        return target;
    }

    /**
     * @see Iterate#zipWithIndex(Iterable, Collection)
     */
    public static >> R zipWithIndex(Iterator iterator, R target)
    {
        int index = 0;
        while (iterator.hasNext())
        {
            target.add(Tuples.pair(iterator.next(), index));
            index += 1;
        }
        return target;
    }

    /**
     * @see Iterate#chunk(Iterable, int)
     */
    public static  RichIterable> chunk(Iterator iterator, int size)
    {
        if (size <= 0)
        {
            throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
        }

        MutableList> result = Lists.mutable.of();
        while (iterator.hasNext())
        {
            MutableList batch = Lists.mutable.of();
            for (int i = 0; i < size && iterator.hasNext(); i++)
            {
                batch.add(iterator.next());
            }
            result.add(batch);
        }
        return result;
    }

    /**
     * @see Iterate#min(Iterable, Comparator)
     */
    public static  T min(Iterator iterator, Comparator comparator)
    {
        T min = iterator.next();
        while (iterator.hasNext())
        {
            T next = iterator.next();
            if (comparator.compare(next, min) < 0)
            {
                min = next;
            }
        }
        return min;
    }

    /**
     * @see Iterate#max(Iterable, Comparator)
     */
    public static  T max(Iterator iterator, Comparator comparator)
    {
        T max = iterator.next();
        while (iterator.hasNext())
        {
            T next = iterator.next();
            if (comparator.compare(next, max) > 0)
            {
                max = next;
            }
        }
        return max;
    }

    public static  Iterator advanceIteratorTo(Iterator iterator, int from)
    {
        for (int i = 0; i < from; i++)
        {
            iterator.next();
        }
        return iterator;
    }

    public static  long sumOfInt(Iterator iterator, IntFunction function)
    {
        long sum = 0L;
        while (iterator.hasNext())
        {
            sum += (long) function.intValueOf(iterator.next());
        }
        return sum;
    }

    public static  long sumOfLong(Iterator iterator, LongFunction function)
    {
        long sum = 0L;
        while (iterator.hasNext())
        {
            sum += function.longValueOf(iterator.next());
        }
        return sum;
    }

    public static  double sumOfFloat(Iterator iterator, FloatFunction function)
    {
        double sum = 0.0d;
        while (iterator.hasNext())
        {
            sum += (double) function.floatValueOf(iterator.next());
        }
        return sum;
    }

    public static  double sumOfDouble(Iterator iterator, DoubleFunction function)
    {
        double sum = 0.0d;
        while (iterator.hasNext())
        {
            sum += function.doubleValueOf(iterator.next());
        }
        return sum;
    }

    public static  MutableMap aggregateBy(
            Iterator iterator,
            Function groupBy,
            Function0 zeroValueFactory,
            Procedure2 mutatingAggregator)
    {
        MutableMap map = UnifiedMap.newMap();
        IteratorIterate.forEach(iterator, new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator));
        return map;
    }

    public static  MutableMap aggregateBy(
            Iterator iterator,
            Function groupBy,
            Function0 zeroValueFactory,
            Function2 nonMutatingAggregator)
    {
        MutableMap map = UnifiedMap.newMap();
        IteratorIterate.forEach(iterator, new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator));
        return map;
    }

    public static > T minBy(Iterator iterator, Function function)
    {
        T min = iterator.next();
        V minValue = function.valueOf(min);
        while (iterator.hasNext())
        {
            T next = iterator.next();
            V nextValue = function.valueOf(next);
            if (nextValue.compareTo(minValue) < 0)
            {
                min = next;
                minValue = nextValue;
            }
        }
        return min;
    }

    public static > T maxBy(Iterator iterator, Function function)
    {
        T max = iterator.next();
        V maxValue = function.valueOf(max);
        while (iterator.hasNext())
        {
            T next = iterator.next();
            V nextValue = function.valueOf(next);
            if (nextValue.compareTo(maxValue) > 0)
            {
                max = next;
                maxValue = nextValue;
            }
        }
        return max;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy