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

org.eclipse.collections.impl.utility.internal.IterableIterate Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2016 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.utility.internal;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.partition.PartitionMutableCollection;
import org.eclipse.collections.api.partition.list.PartitionMutableList;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.utility.Iterate;

/**
 * The IterableIterate class provides a few of the methods from the Smalltalk Collection Protocol for use with general
 * Collection classes. This includes do:, select:, reject:, collect:, inject:into:, detect:, detect:ifNone:, anySatisfy:
 * and allSatisfy:
 */
public final class IterableIterate
{
    private IterableIterate()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * @see Iterate#select(Iterable, Predicate)
     */
    public static  MutableList select(Iterable iterable, Predicate predicate)
    {
        return IterableIterate.select(iterable, predicate, FastList.newList());
    }

    /**
     * @see Iterate#selectAndRejectWith(Iterable, Predicate2, Object)
     */
    public static  Twin> selectAndRejectWith(
            Iterable iterable,
            Predicate2 predicate,
            IV injectedValue)
    {
        return IteratorIterate.selectAndRejectWith(iterable.iterator(), predicate, injectedValue);
    }

    /**
     * @see Iterate#partition(Iterable, Predicate)
     */
    public static  PartitionMutableList partition(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.partition(iterable.iterator(), predicate);
    }

    /**
     * @see Iterate#partitionWith(Iterable, Predicate2, Object)
     * @since 5.0
     */
    public static  PartitionMutableList partitionWith(Iterable iterable, Predicate2 predicate, P parameter)
    {
        return IteratorIterate.partitionWith(iterable.iterator(), predicate, parameter);
    }

    public static > R partitionWhile(Iterable iterable, Predicate predicate, R target)
    {
        return IteratorIterate.partitionWhile(iterable.iterator(), predicate, target);
    }

    public static > R takeWhile(Iterable iterable, Predicate predicate, R target)
    {
        return IteratorIterate.takeWhile(iterable.iterator(), predicate, target);
    }

    public static > R dropWhile(Iterable iterable, Predicate predicate, R target)
    {
        return IteratorIterate.dropWhile(iterable.iterator(), predicate, target);
    }

    /**
     * @see Iterate#count(Iterable, Predicate)
     */
    public static  int count(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.count(iterable.iterator(), predicate);
    }

    /**
     * @see Iterate#countWith(Iterable, Predicate2, Object)
     */
    public static  int countWith(
            Iterable iterable,
            Predicate2 predicate,
            IV injectedValue)
    {
        return IteratorIterate.countWith(iterable.iterator(), predicate, injectedValue);
    }

    /**
     * @see Iterate#collectIf(Iterable, Predicate, Function)
     */
    public static  MutableList collectIf(
            Iterable iterable,
            Predicate predicate,
            Function function)
    {
        return IterableIterate.collectIf(iterable, predicate, function, FastList.newList());
    }

    public static boolean isEmpty(Iterable iterable)
    {
        return !iterable.iterator().hasNext();
    }

    public static boolean notEmpty(Iterable iterable)
    {
        return !IterableIterate.isEmpty(iterable);
    }

    public static  T getFirst(Iterable iterable)
    {
        Iterator iterator = iterable.iterator();
        if (iterator.hasNext())
        {
            return iterator.next();
        }
        return null;
    }

    public static  T getLast(Iterable iterable)
    {
        T last = null;
        Iterator iterator = iterable.iterator();
        while (iterator.hasNext())
        {
            last = iterator.next();
        }
        return last;
    }

    /**
     * @see Iterate#select(Iterable, Predicate, Collection)
     */
    public static > R select(
            Iterable iterable,
            Predicate predicate,
            R targetCollection)
    {
        return IteratorIterate.select(iterable.iterator(), predicate, targetCollection);
    }

    /**
     * @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
     */
    public static > R selectWith(
            Iterable iterable,
            Predicate2 predicate,
            P injectedValue,
            R targetCollection)
    {
        return IteratorIterate.selectWith(iterable.iterator(), predicate, injectedValue, targetCollection);
    }

    /**
     * @see Iterate#selectInstancesOf(Iterable, Class)
     */
    public static  MutableList selectInstancesOf(
            Iterable iterable,
            Class clazz)
    {
        FastList result = FastList.newList();
        IterableIterate.selectInstancesOf(iterable, clazz, result);
        result.trimToSize();
        return result;
    }

    /**
     * @see Iterate#selectInstancesOf(Iterable, Class)
     */
    public static > R selectInstancesOf(
            Iterable iterable,
            Class clazz,
            R targetCollection)
    {
        return IteratorIterate.selectInstancesOf(iterable.iterator(), clazz, targetCollection);
    }

    /**
     * @see Iterate#collectIf(Iterable, Predicate, Function, Collection)
     */
    public static > R collectIf(
            Iterable iterable,
            Predicate predicate,
            Function function,
            R targetCollection)
    {
        return IteratorIterate.collectIf(iterable.iterator(), predicate, function, targetCollection);
    }

    /**
     * @see Iterate#reject(Iterable, Predicate)
     */
    public static  MutableList reject(Iterable iterable, Predicate predicate)
    {
        return IterableIterate.reject(iterable, predicate, FastList.newList());
    }

    /**
     * @see Iterate#reject(Iterable, Predicate, Collection)
     */
    public static > R reject(
            Iterable iterable,
            Predicate predicate,
            R targetCollection)
    {
        return IteratorIterate.reject(iterable.iterator(), predicate, targetCollection);
    }

    /**
     * @see Iterate#rejectWith(Iterable, Predicate2, Object, Collection)
     */
    public static > R rejectWith(
            Iterable iterable,
            Predicate2 predicate,
            P parameter,
            R targetCollection)
    {
        return IteratorIterate.rejectWith(iterable.iterator(), predicate, parameter, targetCollection);
    }

    /**
     * @see Iterate#collect(Iterable, Function)
     */
    public static  MutableList collect(
            Iterable iterable,
            Function function)
    {
        return IterableIterate.collect(iterable, function, FastList.newList());
    }

    /**
     * @see Iterate#collect(Iterable, Function, Collection)
     */
    public static > R collect(
            Iterable iterable,
            Function function,
            R targetCollection)
    {
        return IteratorIterate.collect(iterable.iterator(), function, targetCollection);
    }

    /**
     * @see Iterate#collectBoolean(Iterable, BooleanFunction)
     */
    public static  MutableBooleanCollection collectBoolean(
            Iterable iterable,
            BooleanFunction booleanFunction)
    {
        return IteratorIterate.collectBoolean(iterable.iterator(), booleanFunction);
    }

    /**
     * @see Iterate#collectBoolean(Iterable, BooleanFunction, MutableBooleanCollection)
     */
    public static  R collectBoolean(Iterable iterable, BooleanFunction booleanFunction, R target)
    {
        return IteratorIterate.collectBoolean(iterable.iterator(), booleanFunction, target);
    }

    /**
     * @see Iterate#collectByte(Iterable, ByteFunction)
     */
    public static  MutableByteCollection collectByte(
            Iterable iterable,
            ByteFunction byteFunction)
    {
        return IteratorIterate.collectByte(iterable.iterator(), byteFunction);
    }

    /**
     * @see Iterate#collectByte(Iterable, ByteFunction,
     * MutableByteCollection)
     */
    public static  R collectByte(Iterable iterable, ByteFunction byteFunction, R target)
    {
        return IteratorIterate.collectByte(iterable.iterator(), byteFunction, target);
    }

    /**
     * @see Iterate#collectChar(Iterable, CharFunction)
     */
    public static  MutableCharCollection collectChar(
            Iterable iterable,
            CharFunction charFunction)
    {
        return IteratorIterate.collectChar(iterable.iterator(), charFunction);
    }

    /**
     * @see Iterate#collectChar(Iterable, CharFunction,
     * MutableCharCollection)
     */
    public static  R collectChar(Iterable iterable, CharFunction charFunction, R target)
    {
        return IteratorIterate.collectChar(iterable.iterator(), charFunction, target);
    }

    /**
     * @see Iterate#collectDouble(Iterable, DoubleFunction)
     */
    public static  MutableDoubleCollection collectDouble(
            Iterable iterable,
            DoubleFunction doubleFunction)
    {
        return IteratorIterate.collectDouble(iterable.iterator(), doubleFunction);
    }

    /**
     * @see Iterate#collectDouble(Iterable, DoubleFunction,
     * MutableDoubleCollection)
     */
    public static  R collectDouble(Iterable iterable, DoubleFunction doubleFunction, R target)
    {
        return IteratorIterate.collectDouble(iterable.iterator(), doubleFunction, target);
    }

    /**
     * @see Iterate#collectFloat(Iterable, FloatFunction)
     */
    public static  MutableFloatCollection collectFloat(
            Iterable iterable,
            FloatFunction floatFunction)
    {
        return IteratorIterate.collectFloat(iterable.iterator(), floatFunction);
    }

    /**
     * @see Iterate#collectFloat(Iterable, FloatFunction,
     * MutableFloatCollection)
     */
    public static  R collectFloat(Iterable iterable, FloatFunction floatFunction, R target)
    {
        return IteratorIterate.collectFloat(iterable.iterator(), floatFunction, target);
    }

    /**
     * @see Iterate#collectInt(Iterable, IntFunction)
     */
    public static  MutableIntCollection collectInt(
            Iterable iterable,
            IntFunction intFunction)
    {
        return IteratorIterate.collectInt(iterable.iterator(), intFunction);
    }

    /**
     * @see Iterate#collectInt(Iterable, IntFunction,
     * MutableIntCollection)
     */
    public static  R collectInt(Iterable iterable, IntFunction intFunction, R target)
    {
        return IteratorIterate.collectInt(iterable.iterator(), intFunction, target);
    }

    /**
     * @see Iterate#collectLong(Iterable, LongFunction)
     */
    public static  MutableLongCollection collectLong(
            Iterable iterable,
            LongFunction longFunction)
    {
        return IteratorIterate.collectLong(iterable.iterator(), longFunction);
    }

    /**
     * @see Iterate#collectLong(Iterable, LongFunction,
     * MutableLongCollection)
     */
    public static  R collectLong(Iterable iterable, LongFunction longFunction, R target)
    {
        return IteratorIterate.collectLong(iterable.iterator(), longFunction, target);
    }

    /**
     * @see Iterate#collectShort(Iterable, ShortFunction)
     */
    public static  MutableShortCollection collectShort(
            Iterable iterable,
            ShortFunction shortFunction)
    {
        return IteratorIterate.collectShort(iterable.iterator(), shortFunction);
    }

    /**
     * @see Iterate#collectShort(Iterable, ShortFunction,
     * MutableShortCollection)
     */
    public static  R collectShort(Iterable iterable, ShortFunction shortFunction, R target)
    {
        return IteratorIterate.collectShort(iterable.iterator(), shortFunction, target);
    }

    /**
     * @see Iterate#flatCollect(Iterable, Function)
     */
    public static  MutableList flatCollect(
            Iterable iterable,
            Function> function)
    {
        return IterableIterate.flatCollect(iterable, function, FastList.newList());
    }

    /**
     * @see Iterate#flatCollect(Iterable, Function, Collection)
     */
    public static > R flatCollect(
            Iterable iterable,
            Function> function,
            R targetCollection)
    {
        return IteratorIterate.flatCollect(iterable.iterator(), function, targetCollection);
    }

    /**
     * @see Iterate#forEach(Iterable, Procedure)
     */
    public static  void forEach(Iterable iterable, Procedure procedure)
    {
        IteratorIterate.forEach(iterable.iterator(), procedure);
    }

    /**
     * @see Iterate#forEachWithIndex(Iterable, ObjectIntProcedure)
     */
    public static  void forEachWithIndex(Iterable iterable, ObjectIntProcedure objectIntProcedure)
    {
        IteratorIterate.forEachWithIndex(iterable.iterator(), objectIntProcedure);
    }

    /**
     * @see Iterate#injectInto(Object, Iterable, Function2)
     */
    public static  IV injectInto(
            IV injectValue,
            Iterable iterable,
            Function2 function)
    {
        return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
    }

    /**
     * @see Iterate#injectInto(int, Iterable, IntObjectToIntFunction)
     */
    public static  int injectInto(
            int injectValue,
            Iterable iterable,
            IntObjectToIntFunction function)
    {
        return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
    }

    /**
     * @see Iterate#injectInto(long, Iterable, LongObjectToLongFunction)
     */
    public static  long injectInto(
            long injectValue,
            Iterable iterable,
            LongObjectToLongFunction function)
    {
        return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
    }

    /**
     * @see Iterate#injectInto(double, Iterable, DoubleObjectToDoubleFunction)
     */
    public static  double injectInto(
            double injectValue,
            Iterable iterable,
            DoubleObjectToDoubleFunction function)
    {
        return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
    }

    /**
     * @see Iterate#injectInto(float, Iterable, FloatObjectToFloatFunction)
     */
    public static  float injectInto(
            float injectValue,
            Iterable iterable,
            FloatObjectToFloatFunction function)
    {
        return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
    }

    /**
     * @deprecated in 7.0.
     */
    @Deprecated
    public static > R distinct(
            Iterable iterable,
            R targetCollection)
    {
        return IteratorIterate.distinct(iterable.iterator(), targetCollection);
    }

    /**
     * @since 7.0.
     */
    public static  MutableList distinct(Iterable iterable)
    {
        return IteratorIterate.distinct(iterable.iterator());
    }

    /**
     * @since 7.0.
     */
    public static  MutableList distinct(Iterable iterable, HashingStrategy hashingStrategy)
    {
        return IteratorIterate.distinct(iterable.iterator(), hashingStrategy);
    }

    /**
     * @see Iterate#sumOfInt(Iterable, IntFunction)
     */
    public static  long sumOfInt(Iterable iterable, IntFunction function)
    {
        return IteratorIterate.sumOfInt(iterable.iterator(), function);
    }

    /**
     * @see Iterate#sumOfLong(Iterable, LongFunction)
     */
    public static  long sumOfLong(Iterable iterable, LongFunction function)
    {
        return IteratorIterate.sumOfLong(iterable.iterator(), function);
    }

    /**
     * @see Iterate#sumOfFloat(Iterable, FloatFunction)
     */
    public static  double sumOfFloat(Iterable iterable, FloatFunction function)
    {
        return IteratorIterate.sumOfFloat(iterable.iterator(), function);
    }

    /**
     * @see Iterate#sumOfDouble(Iterable, DoubleFunction)
     */
    public static  double sumOfDouble(Iterable iterable, DoubleFunction function)
    {
        return IteratorIterate.sumOfDouble(iterable.iterator(), function);
    }

    public static  BigDecimal sumOfBigDecimal(Iterable iterable, Function function)
    {
        return IteratorIterate.sumOfBigDecimal(iterable.iterator(), function);
    }

    public static  BigInteger sumOfBigInteger(Iterable iterable, Function function)
    {
        return IteratorIterate.sumOfBigInteger(iterable.iterator(), function);
    }

    public static  MutableMap sumByBigDecimal(Iterable iterable, Function groupBy, Function function)
    {
        return IteratorIterate.sumByBigDecimal(iterable.iterator(), groupBy, function);
    }

    public static  MutableMap sumByBigInteger(Iterable iterable, Function groupBy, Function function)
    {
        return IteratorIterate.sumByBigInteger(iterable.iterator(), groupBy, function);
    }

    public static  ObjectLongMap sumByInt(Iterable iterable, Function groupBy, IntFunction function)
    {
        ObjectLongHashMap result = ObjectLongHashMap.newMap();
        return IterableIterate.injectInto(result, iterable, PrimitiveFunctions.sumByIntFunction(groupBy, function));
    }

    public static  ObjectLongMap sumByLong(Iterable iterable, Function groupBy, LongFunction function)
    {
        ObjectLongHashMap result = ObjectLongHashMap.newMap();
        return IterableIterate.injectInto(result, iterable, PrimitiveFunctions.sumByLongFunction(groupBy, function));
    }

    public static  ObjectDoubleMap sumByFloat(Iterable iterable, Function groupBy, FloatFunction function)
    {
        ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap();
        return IterableIterate.injectInto(result, iterable, PrimitiveFunctions.sumByFloatFunction(groupBy, function));
    }

    public static  ObjectDoubleMap sumByDouble(Iterable iterable, Function groupBy, DoubleFunction function)
    {
        ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap();
        return IterableIterate.injectInto(result, iterable, PrimitiveFunctions.sumByDoubleFunction(groupBy, function));
    }

    /**
     * @see Iterate#injectIntoWith(Object, Iterable, Function3, Object)
     */
    public static  IV injectIntoWith(
            IV injectValue,
            Iterable iterable,
            Function3 function,
            P parameter)
    {
        return IteratorIterate.injectIntoWith(injectValue, iterable.iterator(), function, parameter);
    }

    public static  boolean anySatisfy(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.anySatisfy(iterable.iterator(), predicate);
    }

    public static  boolean anySatisfyWith(Iterable iterable, Predicate2 predicate, P parameter)
    {
        return IteratorIterate.anySatisfyWith(iterable.iterator(), predicate, parameter);
    }

    public static  boolean allSatisfy(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.allSatisfy(iterable.iterator(), predicate);
    }

    public static  boolean allSatisfyWith(Iterable iterable, Predicate2 predicate, P parameter)
    {
        return IteratorIterate.allSatisfyWith(iterable.iterator(), predicate, parameter);
    }

    public static  boolean noneSatisfy(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.noneSatisfy(iterable.iterator(), predicate);
    }

    public static  boolean noneSatisfyWith(Iterable iterable, Predicate2 predicate, P parameter)
    {
        return IteratorIterate.noneSatisfyWith(iterable.iterator(), predicate, parameter);
    }

    public static  T detect(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.detect(iterable.iterator(), predicate);
    }

    public static  T detectWith(Iterable iterable, Predicate2 predicate, P parameter)
    {
        return IteratorIterate.detectWith(iterable.iterator(), predicate, parameter);
    }

    public static  Optional detectOptional(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.detectOptional(iterable.iterator(), predicate);
    }

    public static  Optional detectWithOptional(Iterable iterable, Predicate2 predicate, P parameter)
    {
        return IteratorIterate.detectWithOptional(iterable.iterator(), predicate, parameter);
    }

    /**
     * @see Iterate#removeIf(Iterable, Predicate)
     */
    public static  boolean removeIf(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.removeIf(iterable.iterator(), predicate);
    }

    /**
     * @see Iterate#removeIfWith(Iterable, Predicate2, Object)
     */
    public static  boolean removeIfWith(
            Iterable iterable,
            Predicate2 predicate,
            P parameter)
    {
        return IteratorIterate.removeIfWith(iterable.iterator(), predicate, parameter);
    }

    public static  boolean removeIf(
            Iterable iterable,
            Predicate predicate,
            Procedure procedure)
    {
        return IteratorIterate.removeIf(iterable.iterator(), predicate, procedure);
    }

    public static  boolean removeIfWith(
            Iterable iterable,
            Predicate2 predicate,
            P parameter,
            Procedure procedure)
    {
        return IteratorIterate.removeIfWith(iterable.iterator(), predicate, parameter, procedure);
    }

    /**
     * @see Iterate#detectIndex(Iterable, Predicate)
     */
    public static  int detectIndex(Iterable iterable, Predicate predicate)
    {
        return IteratorIterate.detectIndex(iterable.iterator(), predicate);
    }

    /**
     * @see Iterate#detectIndexWith(Iterable, Predicate2, Object)
     */
    public static  int detectIndexWith(
            Iterable iterable,
            Predicate2 predicate,
            IV injectedValue)
    {
        return IteratorIterate.detectIndexWith(iterable.iterator(), predicate, injectedValue);
    }

    /**
     * @see Iterate#forEachWith(Iterable, Procedure2, Object)
     */
    public static  void forEachWith(
            Iterable iterable,
            Procedure2 procedure,
            P parameter)
    {
        IteratorIterate.forEachWith(iterable.iterator(), procedure, parameter);
    }

    /**
     * @see Iterate#collectWith(Iterable, Function2, Object)
     */
    public static  MutableList collectWith(
            Iterable iterable,
            Function2 function,
            P parameter)
    {
        return IterableIterate.collectWith(iterable, function, parameter, FastList.newList());
    }

    /**
     * @see Iterate#collectWith(Iterable, Function2, Object, Collection)
     */
    public static > R collectWith(
            Iterable iterable,
            Function2 function,
            P parameter,
            R targetCollection)
    {
        return IteratorIterate.collectWith(iterable.iterator(), function, parameter, targetCollection);
    }

    /**
     * @see Iterate#take(Iterable, int)
     */
    public static  Collection take(Iterable iterable, int count)
    {
        if (count < 0)
        {
            throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
        }
        return IterableIterate.take(iterable, count, FastList.newList());
    }

    /**
     * @see Iterate#take(Iterable, int)
     */
    public static > R take(Iterable iterable, int count, R targetCollection)
    {
        if (count < 0)
        {
            throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
        }

        int countCopy = count;
        Iterator iterator = iterable.iterator();
        while (iterator.hasNext() && countCopy-- > 0)
        {
            targetCollection.add(iterator.next());
        }
        return targetCollection;
    }

    /**
     * @see Iterate#drop(Iterable, int)
     */
    public static  Collection drop(Iterable list, int count)
    {
        if (count < 0)
        {
            throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
        }
        return IterableIterate.drop(list, count, FastList.newList());
    }

    /**
     * @see Iterate#drop(Iterable, int)
     */
    public static > R drop(Iterable iterable, int count, R targetCollection)
    {
        if (count < 0)
        {
            throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
        }
        Iterator iterator = iterable.iterator();
        for (int i = 0; iterator.hasNext(); i++)
        {
            T element = iterator.next();
            if (i >= count)
            {
                targetCollection.add(element);
            }
        }
        return targetCollection;
    }

    /**
     * @see RichIterable#appendString(Appendable, String, String, String)
     */
    public static  void appendString(
            Iterable iterable,
            Appendable appendable,
            String start,
            String separator,
            String end)
    {
        try
        {
            appendable.append(start);

            Iterator iterator = iterable.iterator();
            if (iterator.hasNext())
            {
                appendable.append(IterableIterate.stringValueOfItem(iterable, iterator.next()));
                while (iterator.hasNext())
                {
                    appendable.append(separator);
                    appendable.append(IterableIterate.stringValueOfItem(iterable, iterator.next()));
                }
            }

            appendable.append(end);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * @see Iterate#groupBy(Iterable, Function)
     */
    public static  FastListMultimap groupBy(
            Iterable iterable,
            Function function)
    {
        return IterableIterate.groupBy(iterable, function, new FastListMultimap<>());
    }

    /**
     * @see Iterate#groupBy(Iterable, Function, MutableMultimap)
     */
    public static > R groupBy(
            Iterable iterable,
            Function function,
            R target)
    {
        return IteratorIterate.groupBy(iterable.iterator(), function, target);
    }

    /**
     * @see Iterate#groupByEach(Iterable, Function)
     */
    public static  FastListMultimap groupByEach(
            Iterable iterable,
            Function> function)
    {
        return IterableIterate.groupByEach(iterable, function, new FastListMultimap<>());
    }

    /**
     * @see Iterate#groupByEach(Iterable, Function, MutableMultimap)
     */
    public static > R groupByEach(
            Iterable iterable,
            Function> function,
            R target)
    {
        return IteratorIterate.groupByEach(iterable.iterator(), function, target);
    }

    /**
     * @see Iterate#groupByUniqueKey(Iterable, Function)
     */
    public static  MutableMap groupByUniqueKey(
            Iterable iterable,
            Function function)
    {
        return IterableIterate.groupByUniqueKey(iterable, function, UnifiedMap.newMap());
    }

    /**
     * @see Iterate#groupByUniqueKey(Iterable, Function, MutableMap)
     */
    public static > R groupByUniqueKey(
            Iterable iterable,
            Function function,
            R target)
    {
        return IteratorIterate.groupByUniqueKey(iterable.iterator(), function, target);
    }

    public static  T getOnly(Iterable iterable)
    {
        Iterator iterator = iterable.iterator();
        if (!iterator.hasNext())
        {
            throw new IllegalArgumentException("Iterable is empty.");
        }

        T result = iterator.next();
        if (iterator.hasNext())
        {
            throw new IllegalArgumentException("Iterable has multiple values");
        }

        return result;
    }

    /**
     * @see Iterate#zip(Iterable, Iterable)
     */
    public static  MutableList> zip(
            Iterable xs,
            Iterable ys)
    {
        return IterableIterate.zip(xs, ys, FastList.newList());
    }

    /**
     * @see Iterate#zip(Iterable, Iterable, Collection)
     */
    public static >> R zip(
            Iterable xs,
            Iterable ys,
            R target)
    {
        return IteratorIterate.zip(xs.iterator(), ys.iterator(), target);
    }

    /**
     * @see Iterate#zipWithIndex(Iterable)
     */
    public static  MutableList> zipWithIndex(Iterable iterable)
    {
        return IterableIterate.zipWithIndex(iterable, FastList.newList());
    }

    /**
     * @see Iterate#zipWithIndex(Iterable, Collection)
     */
    public static >> R zipWithIndex(Iterable iterable, R target)
    {
        return IteratorIterate.zipWithIndex(iterable.iterator(), target);
    }

    /**
     * @see Iterate#chunk(Iterable, int)
     */
    public static  RichIterable> chunk(Iterable iterable, int size)
    {
        return IteratorIterate.chunk(iterable.iterator(), size);
    }

    public static > T maxBy(Iterable iterable, Function function)
    {
        return IteratorIterate.maxBy(iterable.iterator(), function);
    }

    public static > T minBy(Iterable iterable, Function function)
    {
        return IteratorIterate.minBy(iterable.iterator(), function);
    }

    /**
     * @see Iterate#min(Iterable, Comparator)
     */
    public static  T min(Iterable iterable, Comparator comparator)
    {
        return IteratorIterate.min(iterable.iterator(), comparator);
    }

    /**
     * @see Iterate#max(Iterable, Comparator)
     */
    public static  T max(Iterable iterable, Comparator comparator)
    {
        return IteratorIterate.max(iterable.iterator(), comparator);
    }

    /**
     * @see Iterate#min(Iterable)
     */
    public static  T min(Iterable iterable)
    {
        return IteratorIterate.min(iterable.iterator(), Comparators.naturalOrder());
    }

    /**
     * @see Iterate#max(Iterable)
     */
    public static  T max(Iterable iterable)
    {
        return IteratorIterate.max(iterable.iterator(), Comparators.naturalOrder());
    }

    public static  void forEach(Iterable iterable, int from, int to, Procedure procedure)
    {
        if (from < 0 || to < 0)
        {
            throw new IllegalArgumentException("Neither from nor to may be negative.");
        }

        Iterator iterator = IteratorIterate.advanceIteratorTo(iterable.iterator(), from);
        int i = from;
        while (iterator.hasNext() && i <= to)
        {
            procedure.value(iterator.next());
            i++;
        }
    }

    public static  void forEachWithIndex(List iterable, int from, int to, ObjectIntProcedure objectIntProcedure)
    {
        if (from < 0 || to < 0)
        {
            throw new IllegalArgumentException("Neither from nor to may be negative.");
        }

        Iterator iterator = IteratorIterate.advanceIteratorTo(iterable.iterator(), from);
        int i = from;
        while (iterator.hasNext() && i <= to)
        {
            objectIntProcedure.value(iterator.next(), i++);
        }
    }

    public static  String stringValueOfItem(Iterable iterable, T item)
    {
        return item == iterable
                ? "(this " + iterable.getClass().getSimpleName() + ')'
                : String.valueOf(item);
    }

    public static  MutableMap aggregateInPlaceBy(
            Iterable iterable,
            Function groupBy,
            Function0 zeroValueFactory,
            Procedure2 mutatingAggregator)
    {
        return IteratorIterate.aggregateBy(iterable.iterator(), groupBy, zeroValueFactory, mutatingAggregator);
    }

    public static  MutableMap aggregateBy(
            Iterable iterable,
            Function groupBy,
            Function0 zeroValueFactory,
            Function2 nonMutatingAggregator)
    {
        return IteratorIterate.aggregateBy(iterable.iterator(), groupBy, zeroValueFactory, nonMutatingAggregator);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy