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

org.eclipse.collections.impl.utility.Iterate Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2015 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;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;
import java.util.SortedSet;

import org.eclipse.collections.api.InternalIterable;
import org.eclipse.collections.api.RichIterable;
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.multimap.bag.BagMultimap;
import org.eclipse.collections.api.multimap.list.ListMultimap;
import org.eclipse.collections.api.multimap.set.SetMultimap;
import org.eclipse.collections.api.partition.PartitionIterable;
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.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.Procedures2;
import org.eclipse.collections.impl.block.procedure.MapCollectProcedure;
import org.eclipse.collections.impl.block.procedure.MaxComparatorProcedure;
import org.eclipse.collections.impl.block.procedure.MinComparatorProcedure;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap;
import org.eclipse.collections.impl.utility.internal.DefaultSpeciesNewStrategy;
import org.eclipse.collections.impl.utility.internal.IterableIterate;
import org.eclipse.collections.impl.utility.internal.RandomAccessListIterate;

/**
 * The Iterate utility class acts as a router to other utility classes to provide optimized iteration pattern
 * implementations based on the type of iterable.  The lowest common denominator used will normally be IterableIterate.
 * Iterate can be used when a JDK interface is the only type available to the developer, as it can
 * determine the best way to iterate based on instanceof checks.
 *
 * @since 1.0
 */
public final class Iterate
{
    private Iterate()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * The procedure is evaluated for each element of the iterable.
     * 

* Example using a Java 8 lambda expression: *

     * Iterate.forEach(people, person -> LOGGER.info(person.getName());
     * 
*

* Example using an anonymous inner class: *

     * Iterate.forEach(people, new Procedure()
     * {
     *     public void value(Person person)
     *     {
     *         LOGGER.info(person.getName());
     *     }
     * });
     * 
*/ public static void forEach(Iterable iterable, Procedure procedure) { if (iterable instanceof InternalIterable) { ((InternalIterable) iterable).forEach(procedure); } else if (iterable instanceof ArrayList) { ArrayListIterate.forEach((ArrayList) iterable, procedure); } else if (iterable instanceof List) { ListIterate.forEach((List) iterable, procedure); } else if (iterable != null) { IterableIterate.forEach(iterable, procedure); } else { throw new IllegalArgumentException("Cannot perform a forEach on null"); } } /** * The procedure2 is evaluated for each element of the iterable with the specified parameter passed * as the second argument. *

* Example using a Java 8 lambda expression: *

     * Iterate.forEachWith(people, (Person person, Person other) ->
     *  {
     *      if (other.equals(person))
     *      {
     *          LOGGER.info(person.getName());
     *      }
     *  }, fred);
     * 
*

*

e.g.
     * Iterate.forEachWith(people, new Procedure2()
     * {
     *     public void value(Person person, Person other)
     *     {
     *         if (person.isRelatedTo(other))
     *         {
     *              LOGGER.info(person.getName());
     *         }
     *     }
     * }, fred);
     * 
*/ public static void forEachWith( Iterable iterable, Procedure2 procedure, P parameter) { if (iterable instanceof InternalIterable) { ((InternalIterable) iterable).forEachWith(procedure, parameter); } else if (iterable instanceof ArrayList) { ArrayListIterate.forEachWith((ArrayList) iterable, procedure, parameter); } else if (iterable instanceof List) { ListIterate.forEachWith((List) iterable, procedure, parameter); } else if (iterable != null) { IterableIterate.forEachWith(iterable, procedure, parameter); } else { throw new IllegalArgumentException("Cannot perform a forEachWith on null"); } } /** * Iterates over a collection passing each element and the current relative int index to the specified instance of * ObjectIntProcedure. *

* Example using a Java 8 lambda expression: *

     * Iterate.forEachWithIndex(people, (Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
     * 
*

* Example using anonymous inner class: *

     * Iterate.forEachWithIndex(people, new ObjectIntProcedure()
     * {
     *     public void value(Person person, int index)
     *     {
     *         LOGGER.info("Index: " + index + " person: " + person.getName());
     *     }
     * });
     * 
*/ public static void forEachWithIndex(Iterable iterable, ObjectIntProcedure objectIntProcedure) { if (iterable instanceof InternalIterable) { ((InternalIterable) iterable).forEachWithIndex(objectIntProcedure); } else if (iterable instanceof ArrayList) { ArrayListIterate.forEachWithIndex((ArrayList) iterable, objectIntProcedure); } else if (iterable instanceof List) { ListIterate.forEachWithIndex((List) iterable, objectIntProcedure); } else if (iterable != null) { IterableIterate.forEachWithIndex(iterable, objectIntProcedure); } else { throw new IllegalArgumentException("Cannot perform a forEachWithIndex on null"); } } /** * Returns a new collection with only the elements that evaluated to true for the specified predicate. *

* Example using a Java 8 lambda expression: *

     * Collection<Person> selected =
     *     Iterate.select(people, person -> person.getAddress().getCity().equals("Metuchen"));
     * 
*

* Example using an anonymous inner class: *

e.g.
     * Collection<Person> selected =
     *     Iterate.select(people, new Predicate<Person>()
     *     {
     *         public boolean value(Person person)
     *         {
     *             return person.getAddress().getCity().equals("Metuchen");
     *         }
     *     });
     * 
*/ public static Collection select(Iterable iterable, Predicate predicate) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).select(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.select((ArrayList) iterable, predicate); } if (iterable instanceof List) { return ListIterate.select((List) iterable, predicate); } if (iterable instanceof Collection) { return IterableIterate.select( iterable, predicate, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable)); } if (iterable != null) { return IterableIterate.select(iterable, predicate); } throw new IllegalArgumentException("Cannot perform a select on null"); } /** * Returns a new collection with only elements that evaluated to true for the specified predicate and parameter. *

* Example using a Java 8 lambda expression: *

     * Collection<Person> selected =
     *     Iterate.selectWith(people, (Person person, Integer age) -> person.getAge() >= age, Integer.valueOf(18));
     * 
*

* Example using an anonymous inner class: *

     * Collection<Person> selected =
     *      Iterate.selectWith(people, new Predicate2<Person, Integer>()
     *     {
     *         public boolean accept(Person person, Integer age)
     *         {
     *             return person.getAge() >= age;
     *         }
     *     }, Integer.valueOf(18));
     * 
*/ public static Collection selectWith( Iterable iterable, Predicate2 predicate, IV parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).selectWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.selectWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof List) { return ListIterate.selectWith((List) iterable, predicate, parameter); } if (iterable instanceof Collection) { return IterableIterate.selectWith( iterable, predicate, parameter, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable)); } if (iterable != null) { return IterableIterate.selectWith(iterable, predicate, parameter, FastList.newList()); } throw new IllegalArgumentException("Cannot perform a selectWith on null"); } /** * Filters a collection into two separate collections based on a predicate returned via a Twin. *

* Example using a Java 8 lambda expression: *

     * Twin<MutableList<Person>>selectedRejected =
     *      Iterate.selectAndRejectWith(people, (Person person, String lastName) -> lastName.equals(person.getLastName()), "Mason");
     * 
*

* Example using an anonymous inner class: *

     * Twin<MutableList<Person>>selectedRejected =
     *      Iterate.selectAndRejectWith(people, new Predicate2<String, String>()
     *      {
     *          public boolean accept(Person person, String lastName)
     *          {
     *              return lastName.equals(person.getLastName());
     *          }
     *      }, "Mason");
     * 
*/ public static Twin> selectAndRejectWith( Iterable iterable, Predicate2 predicate, IV injectedValue) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).selectAndRejectWith(predicate, injectedValue); } if (iterable instanceof ArrayList) { return ArrayListIterate.selectAndRejectWith((ArrayList) iterable, predicate, injectedValue); } if (iterable instanceof List) { return ListIterate.selectAndRejectWith((List) iterable, predicate, injectedValue); } if (iterable != null) { return IterableIterate.selectAndRejectWith(iterable, predicate, injectedValue); } throw new IllegalArgumentException("Cannot perform a selectAndRejectWith on null"); } /** * Filters a collection into a PartitionIterable based on a predicate. *

* Example using a Java 8 lambda expression: *

     * PartitionIterable<Person> newYorkersAndNonNewYorkers =
     *      Iterate.partition(people, person -> person.getAddress().getState().getName().equals("New York"));
     * 
*

* Example using an anonymous inner class: *

     * PartitionIterable<Person> newYorkersAndNonNewYorkers =
     *      Iterate.partition(people, new Predicate<Person>()
     *      {
     *          public boolean value(Person person)
     *          {
     *              return person.getAddress().getState().getName().equals("New York");
     *          }
     *      });
     * 
*/ public static PartitionIterable partition(Iterable iterable, Predicate predicate) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).partition(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.partition((ArrayList) iterable, predicate); } if (iterable instanceof List) { return ListIterate.partition((List) iterable, predicate); } if (iterable != null) { return IterableIterate.partition(iterable, predicate); } throw new IllegalArgumentException("Cannot perform a partition on null"); } /** * Filters a collection into a PartitionIterable based on a predicate. *

* Example using a Java 8 lambda expression: *

     * PartitionIterable<Person>> newYorkersAndNonNewYorkers =
     *     Iterate.partitionWith(people, (Person person, String state) -> person.getAddress().getState().getName().equals(state), "New York");
     * 
*

* Example using an anonymous inner class: *

     * PartitionIterable<Person> newYorkersAndNonNewYorkers =
     *      Iterate.partitionWith(people, new Predicate<Person, String>()
     *      {
     *          public boolean value(Person person, String state)
     *          {
     *              return person.getAddress().getState().getName().equals(state);
     *          }
     *      }, "New York");
     * 
* * @since 5.0. */ public static PartitionIterable partitionWith(Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).partitionWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.partitionWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof List) { return ListIterate.partitionWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.partitionWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform a partition on null"); } /** * Returns a new collection with only the elements that are instances of the Class {@code clazz}. */ public static Collection selectInstancesOf(Iterable iterable, Class clazz) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).selectInstancesOf(clazz); } if (iterable instanceof ArrayList) { return ArrayListIterate.selectInstancesOf((ArrayList) iterable, clazz); } if (iterable instanceof List) { return ListIterate.selectInstancesOf((List) iterable, clazz); } if (iterable instanceof Collection) { return IterableIterate.selectInstancesOf( iterable, clazz, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable)); } if (iterable != null) { return IterableIterate.selectInstancesOf(iterable, clazz); } throw new IllegalArgumentException("Cannot perform a selectInstancesOf on null"); } /** * Returns the total number of elements that evaluate to true for the specified predicate. *

* Example using a Java 8 lambda expression: *

     * int count = Iterate.count(people, person -> person.getAddress().getState().getName().equals("New York"));
     * 
*

* Example using anonymous inner class *

     * int count = Iterate.count(people, new Predicate<Person>()
     * {
     *     public boolean accept(Person person)
     *     {
     *         return person.getAddress().getState().getName().equals("New York");
     *     }
     * });
     * 
*/ public static int count(Iterable iterable, Predicate predicate) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).count(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.count((ArrayList) iterable, predicate); } if (iterable instanceof List) { return ListIterate.count((List) iterable, predicate); } if (iterable != null) { return IterableIterate.count(iterable, predicate); } throw new IllegalArgumentException("Cannot get a count from null"); } /** * Returns the total number of elements that evaluate to true for the specified predicate2 and parameter. *

*

e.g.
     * return Iterate.countWith(lastNames, Predicates2.equal(), "Smith");
     * 
*/ public static int countWith( Iterable iterable, Predicate2 predicate, IV injectedValue) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).countWith(predicate, injectedValue); } if (iterable instanceof ArrayList) { return ArrayListIterate.countWith((ArrayList) iterable, predicate, injectedValue); } if (iterable instanceof List) { return ListIterate.countWith((List) iterable, predicate, injectedValue); } if (iterable != null) { return IterableIterate.countWith(iterable, predicate, injectedValue); } throw new IllegalArgumentException("Cannot get a count from null"); } /** * @see RichIterable#collectIf(Predicate, Function) */ public static Collection collectIf( Iterable iterable, Predicate predicate, Function function) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectIf(predicate, function); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectIf((ArrayList) iterable, predicate, function); } if (iterable instanceof List) { return ListIterate.collectIf((List) iterable, predicate, function); } if (iterable instanceof Collection) { return IterableIterate.collectIf( iterable, predicate, function, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable)); } if (iterable != null) { return IterableIterate.collectIf(iterable, predicate, function); } throw new IllegalArgumentException("Cannot perform a collectIf on null"); } /** * @see RichIterable#collectIf(Predicate, Function, Collection) */ public static > R collectIf( Iterable iterable, Predicate predicate, Function function, R target) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).collectIf(predicate, function, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectIf((ArrayList) iterable, predicate, function, target); } if (iterable instanceof List) { return ListIterate.collectIf((List) iterable, predicate, function, target); } if (iterable != null) { return IterableIterate.collectIf(iterable, predicate, function, target); } throw new IllegalArgumentException("Cannot perform a collectIf on null"); } /** * Same as the select method with two parameters but uses the specified target collection *

* Example using Java 8 lambda: *

     * MutableList<Person> selected =
     *      Iterate.select(people, person -> person.person.getLastName().equals("Smith"), FastList.newList());
     * 
*

* Example using anonymous inner class: *

     * MutableList<Person> selected =
     *      Iterate.select(people, new Predicate<Person>()
     *      {
     *          public boolean accept(Person person)
     *          {
     *         return person.person.getLastName().equals("Smith");
     *     }
     * }, FastList.newList());
     * 
*

* Example using Predicates factory: *

     * MutableList<Person> selected = Iterate.select(collection, Predicates.attributeEqual("lastName", "Smith"), FastList.newList());
     * 
*/ public static > R select( Iterable iterable, Predicate predicate, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).select(predicate, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.select((ArrayList) iterable, predicate, targetCollection); } if (iterable instanceof List) { return ListIterate.select((List) iterable, predicate, targetCollection); } if (iterable != null) { return IterableIterate.select(iterable, predicate, targetCollection); } throw new IllegalArgumentException("Cannot perform a select on null"); } /** * Same as the selectWith method with two parameters but uses the specified target collection. */ public static > R selectWith( Iterable iterable, Predicate2 predicate, P parameter, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).selectWith(predicate, parameter, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.selectWith((ArrayList) iterable, predicate, parameter, targetCollection); } if (iterable instanceof List) { return ListIterate.selectWith((List) iterable, predicate, parameter, targetCollection); } if (iterable != null) { return IterableIterate.selectWith(iterable, predicate, parameter, targetCollection); } throw new IllegalArgumentException("Cannot perform a selectWith on null"); } /** * Returns the first {@code count} elements of the iterable * or all the elements in the iterable if {@code count} is greater than the length of * the iterable. * * @param iterable the collection to take from. * @param count the number of items to take. * @return a new list with the items take from the given collection. * @throws IllegalArgumentException if {@code count} is less than zero */ public static Collection take(Iterable iterable, int count) { if (iterable instanceof ArrayList) { return ArrayListIterate.take((ArrayList) iterable, count); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.take((List) iterable, count); } if (iterable instanceof Collection) { return IterableIterate.take( iterable, count, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable, count)); } if (iterable != null) { return IterableIterate.take(iterable, count); } throw new IllegalArgumentException("Cannot perform a take on null"); } /** * Returns a collection without the first {@code count} elements of the iterable * or an empty iterable if the {@code count} is greater than the length of the iterable. * * @param iterable the collection to drop from. * @param count the number of items to drop. * @return a new list with the items dropped from the given collection. * @throws IllegalArgumentException if {@code count} is less than zero */ public static Collection drop(Iterable iterable, int count) { if (iterable instanceof ArrayList) { return ArrayListIterate.drop((ArrayList) iterable, count); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.drop((List) iterable, count); } if (iterable instanceof Collection) { return IterableIterate.drop( iterable, count, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable, count)); } if (iterable != null) { return IterableIterate.drop(iterable, count); } throw new IllegalArgumentException("Cannot perform a drop on null"); } /** * Returns all elements of the iterable that evaluate to false for the specified predicate. *

* Example using Java 8 lambda: *

     * Collection<Person> rejected =
     *      Iterate.reject(people, person -> person.person.getLastName().equals("Smith"));
     * 
*

* Example using anonymous inner class: *

     * Collection<Person> rejected =
     *      Iterate.reject(people,
     *          new Predicate<Person>()
     *          {
     *              public boolean accept(Person person)
     *              {
     *                  return person.person.getLastName().equals("Smith");
     *              }
     *          });
     * 
*

* Example using Predicates factory: *

     * Collection<Person> rejected =
     *      Iterate.reject(people, Predicates.attributeEqual("lastName", "Smith"));
     * 
*/ public static Collection reject(Iterable iterable, Predicate predicate) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).reject(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.reject((ArrayList) iterable, predicate); } if (iterable instanceof List) { return ListIterate.reject((List) iterable, predicate); } if (iterable instanceof Collection) { return IterableIterate.reject( iterable, predicate, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable)); } if (iterable != null) { return IterableIterate.reject(iterable, predicate); } throw new IllegalArgumentException("Cannot perform a reject on null"); } /** * SortThis is a mutating method. The List passed in is also returned. */ public static , L extends List> L sortThis(L list) { if (list instanceof MutableList) { ((MutableList) list).sortThis(); } else if (list instanceof ArrayList) { ArrayListIterate.sortThis((ArrayList) list); } else { if (list.size() > 1) { Collections.sort(list); } } return list; } /** * SortThis is a mutating method. The List passed in is also returned. */ public static > L sortThis(L list, Comparator comparator) { if (list instanceof MutableList) { ((MutableList) list).sortThis(comparator); } else if (list instanceof ArrayList) { ArrayListIterate.sortThis((ArrayList) list, comparator); } else { if (list.size() > 1) { Collections.sort(list, comparator); } } return list; } /** * SortThis is a mutating method. The List passed in is also returned. */ public static > L sortThis(L list, final Predicate2 predicate) { return Iterate.sortThis(list, new Comparator() { public int compare(T o1, T o2) { if (predicate.accept(o1, o2)) { return -1; } if (predicate.accept(o2, o1)) { return 1; } return 0; } }); } /** * Sort the list by comparing an attribute defined by the function. * SortThisBy is a mutating method. The List passed in is also returned. */ public static , L extends List> L sortThisBy(L list, Function function) { return Iterate.sortThis(list, Comparators.byFunction(function)); } /** * Removes all elements from the iterable that evaluate to true for the specified predicate. */ public static boolean removeIf(Iterable iterable, Predicate predicate) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).removeIf(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.removeIf((ArrayList) iterable, predicate); } if (iterable instanceof List) { return ListIterate.removeIf((List) iterable, predicate); } if (iterable != null) { return IterableIterate.removeIf(iterable, predicate); } throw new IllegalArgumentException("Cannot perform a remove on null"); } /** * Removes all elements of the iterable that evaluate to true for the specified predicate2 and parameter. */ public static boolean removeIfWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).removeIfWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.removeIfWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof List) { return ListIterate.removeIfWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.removeIfWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform a remove on null"); } /** * Similar to {@link #reject(Iterable, Predicate)}, except with an evaluation parameter for the second generic argument in {@link Predicate2}. *

* Example using a Java 8 lambda expression: *

     * Collection<Person> rejected =
     *     Iterate.rejectWith(people, (Person person, Integer age) -> person.getAge() >= age, Integer.valueOf(18));
     * 
*

* Example using an anonymous inner class: *

     * Collection<Person> rejected =
     *      Iterate.rejectWith(people, new Predicate2<Person, Integer>()
     *     {
     *         public boolean accept(Person person, Integer age)
     *         {
     *             return person.getAge() >= age;
     *         }
     *     }, Integer.valueOf(18));
     * 
*/ public static Collection rejectWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).rejectWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.rejectWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof List) { return ListIterate.rejectWith((List) iterable, predicate, parameter); } if (iterable instanceof Collection) { return IterableIterate.rejectWith( iterable, predicate, parameter, DefaultSpeciesNewStrategy.INSTANCE.speciesNew((Collection) iterable)); } if (iterable != null) { return IterableIterate.rejectWith(iterable, predicate, parameter, FastList.newList()); } throw new IllegalArgumentException("Cannot perform a rejectWith on null"); } /** * Same as the reject method with one parameter but uses the specified target collection for the results. *

* Example using Java 8 lambda: *

     * MutableList<Person> rejected =
     *      Iterate.reject(people, person -> person.person.getLastName().equals("Smith"), FastList.newList());
     * 
*

* Example using anonymous inner class: *

     * MutableList<Person> rejected =
     *      Iterate.reject(people,
     *          new Predicate<Person>()
     *          {
     *              public boolean accept(Person person)
     *              {
     *                  return person.person.getLastName().equals("Smith");
     *              }
     *          }, FastList.newList());
     * 
*

* Example using Predicates factory: *

     * MutableList<Person> rejected =
     *      Iterate.reject(people, Predicates.attributeEqual("lastName", "Smith"), FastList.newList());
     * 
*/ public static > R reject( Iterable iterable, Predicate predicate, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).reject(predicate, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.reject((ArrayList) iterable, predicate, targetCollection); } if (iterable instanceof List) { return ListIterate.reject((List) iterable, predicate, targetCollection); } if (iterable != null) { return IterableIterate.reject(iterable, predicate, targetCollection); } throw new IllegalArgumentException("Cannot perform a reject on null"); } /** * Same as the reject method with two parameters but uses the specified target collection. */ public static > R rejectWith( Iterable iterable, Predicate2 predicate, P parameter, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).rejectWith(predicate, parameter, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.rejectWith( (ArrayList) iterable, predicate, parameter, targetCollection); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.rejectWith((List) iterable, predicate, parameter, targetCollection); } if (iterable != null) { return IterableIterate.rejectWith(iterable, predicate, parameter, targetCollection); } throw new IllegalArgumentException("Cannot perform a rejectWith on null"); } /** * Add all elements from the source Iterable to the target collection, return the target collection. */ public static > R addAllTo(Iterable iterable, R targetCollection) { Iterate.addAllIterable(iterable, targetCollection); return targetCollection; } /** * Add all elements from the source Iterable to the target collection, returns true if any element was added. */ public static boolean addAllIterable(Iterable iterable, Collection targetCollection) { if (iterable == null) { throw new NullPointerException(); } if (iterable instanceof Collection) { return targetCollection.addAll((Collection) iterable); } int oldSize = targetCollection.size(); Iterate.forEachWith(iterable, Procedures2.addToCollection(), targetCollection); return targetCollection.size() != oldSize; } /** * Remove all elements present in Iterable from the target collection, return the target collection. */ public static > R removeAllFrom(Iterable iterable, R targetCollection) { Iterate.removeAllIterable(iterable, targetCollection); return targetCollection; } /** * Remove all elements present in Iterable from the target collection, returns true if any element was removed. */ public static boolean removeAllIterable(Iterable iterable, Collection targetCollection) { if (iterable == null) { throw new NullPointerException(); } if (iterable instanceof Collection) { return targetCollection.removeAll((Collection) iterable); } int oldSize = targetCollection.size(); Iterate.forEachWith(iterable, Procedures2.removeFromCollection(), targetCollection); return targetCollection.size() != oldSize; } /** * Returns a new collection with the results of applying the specified function for each element of the iterable. *

* Example using a Java 8 lambda expression: *

     * Collection<String> names =
     *      Iterate.collect(people, person -> person.getFirstName() + " " + person.getLastName());
     * 
*

* Example using an anonymous inner class: *

     * Collection<String> names =
     *      Iterate.collect(people,
     *          new Function<Person, String>()
     *          {
     *              public String value(Person person)
     *              {
     *                  return person.getFirstName() + " " + person.getLastName();
     *              }
     *          });
     * 
*/ public static Collection collect( Iterable iterable, Function function) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collect(function); } if (iterable instanceof ArrayList) { return ArrayListIterate.collect((ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.collect((List) iterable, function); } if (iterable instanceof Collection) { return IterableIterate.collect( iterable, function, DefaultSpeciesNewStrategy.INSTANCE.speciesNew( (Collection) iterable, ((Collection) iterable).size())); } if (iterable != null) { return IterableIterate.collect(iterable, function); } throw new IllegalArgumentException("Cannot perform a collect on null"); } /** * Same as the {@link #collect(Iterable, Function)} method with two parameters, except that the results are gathered into the specified * targetCollection *

* Example using a Java 8 lambda expression: *

     * MutableList<String> names =
     *      Iterate.collect(people, person -> person.getFirstName() + " " + person.getLastName(), FastList.newList());
     * 
*

* Example using an anonymous inner class: *

     * MutableList<String> names =
     *      Iterate.collect(people,
     *          new Function<Person, String>()
     *          {
     *              public String value(Person person)
     *              {
     *                  return person.getFirstName() + " " + person.getLastName();
     *              }
     *          }, FastList.newList());
     * 
*/ public static > R collect( Iterable iterable, Function function, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).collect(function, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.collect((ArrayList) iterable, function, targetCollection); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.collect((List) iterable, function, targetCollection); } if (iterable != null) { return IterableIterate.collect(iterable, function, targetCollection); } throw new IllegalArgumentException("Cannot perform a collect on null"); } /** * Returns a new primitive {@code boolean} collection with the results of applying the specified booleanFunction for each element of the iterable. *

* Example using Java 8 lambda: *

     * MutableBooleanCollection voters =
     *      Iterable.collectBoolean(people, person -> person.canVote());
     * 
*

* Example using anonymous inner class: *

     * MutableBooleanCollection voters =
     *      Iterate.collectBoolean(people,
     *          new BooleanFunction<Person>()
     *          {
     *              public boolean booleanValueOf(Person person)
     *              {
     *                  return person.canVote();
     *              }
     *          });
     * 
*/ public static MutableBooleanCollection collectBoolean( Iterable iterable, BooleanFunction booleanFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectBoolean(booleanFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectBoolean((ArrayList) iterable, booleanFunction); } if (iterable instanceof List) { return ListIterate.collectBoolean((List) iterable, booleanFunction); } if (iterable != null) { return IterableIterate.collectBoolean(iterable, booleanFunction); } throw new IllegalArgumentException("Cannot perform a collectBoolean on null"); } /** * Same as {@link #collectBoolean(Iterable, BooleanFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using Java 8 lambda: *

     * BooleanArrayList voters =
     *      Iterable.collectBoolean(people, person -> person.canVote(), new BooleanArrayList());
     * 
*

* Example using an anonymous inner class: *

     * BooleanArrayList voters =
     *      Iterate.collectBoolean(people,
     *          new BooleanFunction<Person>()
     *          {
     *              public boolean booleanValueOf(Person person)
     *              {
     *                  return person.canVote();
     *              }
     *          }, new BooleanArrayList());
     * 
*/ public static R collectBoolean( Iterable iterable, BooleanFunction booleanFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectBoolean(booleanFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectBoolean((ArrayList) iterable, booleanFunction, target); } if (iterable instanceof List) { return ListIterate.collectBoolean((List) iterable, booleanFunction, target); } if (iterable != null) { return IterableIterate.collectBoolean(iterable, booleanFunction, target); } throw new IllegalArgumentException("Cannot perform a collectBoolean on null"); } /** * Returns a new {@code byte} collection with the results of applying the specified byteFunction for each element of the iterable. *

* Example using Java 8 lambda: *

     * MutableByteCollection bytes =
     *      Iterate.collectByte(people, person -> person.getCode());
     * 
*

* Example using anonymous inner class: *

     * MutableByteCollection bytes =
     *      Iterate.collectByte(people,
     *          new ByteFunction<Person>()
     *          {
     *              public byte byteValueOf(Person person)
     *              {
     *                  return person.getCode();
     *              }
     *          });
     * 
*/ public static MutableByteCollection collectByte( Iterable iterable, ByteFunction byteFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectByte(byteFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectByte((ArrayList) iterable, byteFunction); } if (iterable instanceof List) { return ListIterate.collectByte((List) iterable, byteFunction); } if (iterable != null) { return IterableIterate.collectByte(iterable, byteFunction); } throw new IllegalArgumentException("Cannot perform a collectByte on null"); } /** * Same as {@link #collectByte(Iterable, ByteFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using a Java 8 lambda expression: *

     * ByteArrayList bytes =
     *      Iterate.collectByte(people, person -> person.getCode(), new ByteArrayList());
     * 
*

* Example using an anonymous inner class: *

     * ByteArrayList bytes =
     *      Iterate.collectByte(people,
     *          new ByteFunction<Person>()
     *          {
     *              public byte byteValueOf(Person person)
     *              {
     *                  return person.getCode();
     *              }
     *          }, new ByteArrayList());
     * 
*/ public static R collectByte( Iterable iterable, ByteFunction byteFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectByte(byteFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectByte((ArrayList) iterable, byteFunction, target); } if (iterable instanceof List) { return ListIterate.collectByte((List) iterable, byteFunction, target); } if (iterable != null) { return IterableIterate.collectByte(iterable, byteFunction, target); } throw new IllegalArgumentException("Cannot perform a collectByte on null"); } /** * Returns a new {@code char} collection with the results of applying the specified charFunction for each element of the iterable. *

* Example using Java 8 lambda: *

     * MutableCharCollection chars =
     *      Iterate.collectChar(people, person -> person.getMiddleInitial());
     * 
*

* Example using anonymous inner class: *

     * MutableCharCollection chars =
     *      Iterate.collectChar(people,
     *          new CharFunction<Person>()
     *          {
     *              public char charValueOf(Person person)
     *              {
     *                  return person.getMiddleInitial();
     *              }
     *          });
     * 
*/ public static MutableCharCollection collectChar( Iterable iterable, CharFunction charFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectChar(charFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectChar((ArrayList) iterable, charFunction); } if (iterable instanceof List) { return ListIterate.collectChar((List) iterable, charFunction); } if (iterable != null) { return IterableIterate.collectChar(iterable, charFunction); } throw new IllegalArgumentException("Cannot perform a collectChar on null"); } /** * Same as {@link #collectChar(Iterable, CharFunction)}, except that the results are gathered into the specified {@code target} * collection. *

*

     * CharArrayList chars =
     *      Iterate.collectChar(people, person -> person.getMiddleInitial());
     * 
*

* Example using anonymous inner class: *

     * CharArrayList chars =
     *      Iterate.collectChar(people,
     *          new CharFunction<Person>()
     *          {
     *              public char charValueOf(Person person)
     *              {
     *                  return person.getMiddleInitial();
     *              }
     *          }, new CharArrayList());
     * 
*/ public static R collectChar( Iterable iterable, CharFunction charFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectChar(charFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectChar((ArrayList) iterable, charFunction, target); } if (iterable instanceof List) { return ListIterate.collectChar((List) iterable, charFunction, target); } if (iterable != null) { return IterableIterate.collectChar(iterable, charFunction, target); } throw new IllegalArgumentException("Cannot perform a collectChar on null"); } /** * Returns a new {@code double} collection with the results of applying the specified doubleFunction for each element of the iterable. *

* Example using a Java 8 lambda expression: *

     * MutableDoubleCollection doubles =
     *      Iterate.collectDouble(people, person -> person.getMilesFromNorthPole());
     * 
* Example using an anonymous inner class: *
     * MutableDoubleCollection doubles =
     *      Iterate.collectDouble(people,
     *          new DoubleFunction<Person>()
     *          {
     *              public double doubleValueOf(Person person)
     *              {
     *                  return person.getMilesFromNorthPole();
     *              }
     *          });
     * 
*/ public static MutableDoubleCollection collectDouble( Iterable iterable, DoubleFunction doubleFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectDouble(doubleFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectDouble((ArrayList) iterable, doubleFunction); } if (iterable instanceof List) { return ListIterate.collectDouble((List) iterable, doubleFunction); } if (iterable != null) { return IterableIterate.collectDouble(iterable, doubleFunction); } throw new IllegalArgumentException("Cannot perform a collectDouble on null"); } /** * Same as {@link #collectDouble(Iterable, DoubleFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using a Java 8 lambda expression: *

     * DoubleArrayList doubles =
     *      Iterate.collectDouble(people, person -> person.getMilesFromNorthPole());
     * 
*

* Example using an anonymous inner class: *

     * DoubleArrayList doubles =
     *      Iterate.collectDouble(people,
     *          new DoubleFunction<Person>()
     *          {
     *              public double doubleValueOf(Person person)
     *              {
     *                  return person.getMilesFromNorthPole();
     *              }
     *          }, new DoubleArrayList());
     * 
*/ public static R collectDouble( Iterable iterable, DoubleFunction doubleFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectDouble(doubleFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectDouble((ArrayList) iterable, doubleFunction, target); } if (iterable instanceof List) { return ListIterate.collectDouble((List) iterable, doubleFunction, target); } if (iterable != null) { return IterableIterate.collectDouble(iterable, doubleFunction, target); } throw new IllegalArgumentException("Cannot perform a collectDouble on null"); } /** * Returns a new {@code float} collection with the results of applying the specified floatFunction for each element of the iterable. *

* Example using a Java 8 lambda expression: *

     * MutableFloatCollection floats =
     *      Iterate.collectFloat(people, person -> person.getHeightInInches());
     * 
*

* Example using an anonymous inner class: *

     * MutableFloatCollection floats =
     *      Iterate.collectFloat(people,
     *          new FloatFunction<Person>()
     *          {
     *              public float floatValueOf(Person person)
     *              {
     *                  return person.getHeightInInches();
     *              }
     *          });
     * 
*/ public static MutableFloatCollection collectFloat( Iterable iterable, FloatFunction floatFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectFloat(floatFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectFloat((ArrayList) iterable, floatFunction); } if (iterable instanceof List) { return ListIterate.collectFloat((List) iterable, floatFunction); } if (iterable != null) { return IterableIterate.collectFloat(iterable, floatFunction); } throw new IllegalArgumentException("Cannot perform a collectFloat on null"); } /** * Same as {@link #collectFloat(Iterable, FloatFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using a Java 8 lambda expression: *

     * FloatArrayList floats =
     *      Iterate.collectFloat(people, person -> person.getHeightInInches(), new FloatArrayList());
     * 
*

* Example using an anonymous inner class: *

     * FloatArrayList floats =
     *      Iterate.collectFloat(people,
     *          new FloatFunction<Person>()
     *          {
     *              public float floatValueOf(Person person)
     *              {
     *                  return person.getHeightInInches();
     *              }
     *          }, new FloatArrayList());
     * 
*/ public static R collectFloat( Iterable iterable, FloatFunction floatFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectFloat(floatFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectFloat((ArrayList) iterable, floatFunction, target); } if (iterable instanceof List) { return ListIterate.collectFloat((List) iterable, floatFunction, target); } if (iterable != null) { return IterableIterate.collectFloat(iterable, floatFunction, target); } throw new IllegalArgumentException("Cannot perform a collectFloat on null"); } /** * Returns a new {@code int} collection with the results of applying the specified intFunction for each element of the iterable. *

* Example using a Java 8 lambda expression: *

     * MutableIntCollection ages =
     *      Iterate.collectInt(people, person -> person.getAge());
     * 
*

* Example using an anonymous inner class: *

     * MutableIntCollection ages =
     *      Iterate.collectInt(people,
     *          new IntFunction<Person>()
     *          {
     *              public int intValueOf(Person person)
     *              {
     *                  return person.getAge();
     *              }
     *          });
     * 
*/ public static MutableIntCollection collectInt( Iterable iterable, IntFunction intFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectInt(intFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectInt((ArrayList) iterable, intFunction); } if (iterable instanceof List) { return ListIterate.collectInt((List) iterable, intFunction); } if (iterable != null) { return IterableIterate.collectInt(iterable, intFunction); } throw new IllegalArgumentException("Cannot perform a collectInt on null"); } /** * Same as {@link #collectInt(Iterable, IntFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using a Java 8 lambda expression: *

     * IntArrayList ages =
     *      Iterate.collectInt(people, person -> person.getAge(), new IntArrayList());
     * 
*

* Example using an anonymous inner class: *

     * IntArrayList ages =
     *      Iterate.collectInt(people,
     *          new IntFunction<Person>()
     *          {
     *              public int intValueOf(Person person)
     *              {
     *                  return person.getAge();
     *              }
     *          }, new IntArrayList());
     * 
*/ public static R collectInt( Iterable iterable, IntFunction intFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectInt(intFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectInt((ArrayList) iterable, intFunction, target); } if (iterable instanceof List) { return ListIterate.collectInt((List) iterable, intFunction, target); } if (iterable != null) { return IterableIterate.collectInt(iterable, intFunction, target); } throw new IllegalArgumentException("Cannot perform a collectInt on null"); } /** * Returns a new {@code long} collection with the results of applying the specified longFunction for each element of the iterable. *

* Example using a Java 8 lambda expression: *

     * MutableLongCollection longs =
     *      Iterate.collectLong(people, person -> person.getGuid());
     * 
*

* Example using an anonymous inner class: *

     * MutableLongCollection longs =
     *      Iterate.collectLong(people,
     *          new LongFunction<Person>()
     *          {
     *              public long longValueOf(Person person)
     *              {
     *                  return person.getGuid();
     *              }
     *          });
     * 
*/ public static MutableLongCollection collectLong( Iterable iterable, LongFunction longFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectLong(longFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectLong((ArrayList) iterable, longFunction); } if (iterable instanceof List) { return ListIterate.collectLong((List) iterable, longFunction); } if (iterable != null) { return IterableIterate.collectLong(iterable, longFunction); } throw new IllegalArgumentException("Cannot perform a collectLong on null"); } /** * Same as {@link #collectLong(Iterable, LongFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using a Java 8 lambda expression: *

     * LongArrayList longs =
     *      Iterate.collectLong(people, person -> person.getGuid(), new LongArrayList());
     * 
*

* Example using an anonymous inner class: *

     * LongArrayList longs =
     *      Iterate.collectLong(people,
     *              new LongFunction<Person>()
     *              {
     *                  public long longValueOf(Person person)
     *                  {
     *                      return person.getGuid();
     *                  }
     *              }, new LongArrayList());
     * 
*/ public static R collectLong( Iterable iterable, LongFunction longFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectLong(longFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectLong((ArrayList) iterable, longFunction, target); } if (iterable instanceof List) { return ListIterate.collectLong((List) iterable, longFunction, target); } if (iterable != null) { return IterableIterate.collectLong(iterable, longFunction, target); } throw new IllegalArgumentException("Cannot perform a collectLong on null"); } /** * Returns a new {@code short} collection with the results of applying the specified shortFunction for each element of the iterable. *

* Example using a Java 8 lambda expression: *

     * MutableShortCollection shorts =
     *      Iterate.collectShort(people, person -> person.getNumberOfJunkMailItemsReceivedPerMonth());
     * 
*

* Example using an anonymous inner class: *

     * MutableShortCollection shorts =
     *      Iterate.collectShort(people,
     *          new ShortFunction<Person>()
     *          {
     *              public short shortValueOf(Person person)
     *              {
     *                  return person.getNumberOfJunkMailItemsReceivedPerMonth();
     *              }
     *          });
     * 
*/ public static MutableShortCollection collectShort( Iterable iterable, ShortFunction shortFunction) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectShort(shortFunction); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectShort((ArrayList) iterable, shortFunction); } if (iterable instanceof List) { return ListIterate.collectShort((List) iterable, shortFunction); } if (iterable != null) { return IterableIterate.collectShort(iterable, shortFunction); } throw new IllegalArgumentException("Cannot perform a collectShort on null"); } /** * Same as {@link #collectShort(Iterable, ShortFunction)}, except that the results are gathered into the specified {@code target} * collection. *

* Example using a Java 8 lambda expression: *

     * ShortArrayList shorts =
     *      Iterate.collectShort(people, person -> person.getNumberOfJunkMailItemsReceivedPerMonth(), new ShortArrayList());
     * 
*

* Example using an anonymous inner class: *

     * ShortArrayList shorts =
     *      Iterate.collectShort(people,
     *          new ShortFunction<Person>()
     *          {
     *              public short shortValueOf(Person person)
     *              {
     *                  return person.getNumberOfJunkMailItemsReceivedPerMonth();
     *              }
     *          }, new ShortArrayList());
     * 
*/ public static R collectShort( Iterable iterable, ShortFunction shortFunction, R target) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectShort(shortFunction, target); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectShort((ArrayList) iterable, shortFunction, target); } if (iterable instanceof List) { return ListIterate.collectShort((List) iterable, shortFunction, target); } if (iterable != null) { return IterableIterate.collectShort(iterable, shortFunction, target); } throw new IllegalArgumentException("Cannot perform a collectShort on null"); } /** * @see RichIterable#flatCollect(Function) */ public static Collection flatCollect( Iterable iterable, Function> function) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).flatCollect(function); } if (iterable instanceof ArrayList) { return ArrayListIterate.flatCollect((ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.flatCollect((List) iterable, function); } if (iterable instanceof Collection) { return IterableIterate.flatCollect( iterable, function, DefaultSpeciesNewStrategy.INSTANCE.speciesNew( (Collection) iterable, ((Collection) iterable).size())); } if (iterable != null) { return IterableIterate.flatCollect(iterable, function); } throw new IllegalArgumentException("Cannot perform a flatCollect on null"); } /** * @see RichIterable#flatCollect(Function, Collection) */ public static > R flatCollect( Iterable iterable, Function> function, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).flatCollect(function, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.flatCollect((ArrayList) iterable, function, targetCollection); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.flatCollect((List) iterable, function, targetCollection); } if (iterable != null) { return IterableIterate.flatCollect(iterable, function, targetCollection); } throw new IllegalArgumentException("Cannot perform a flatCollect on null"); } /** * Same as collect with a Function2 and specified parameter which is passed to the function. */ public static Collection collectWith( Iterable iterable, Function2 function, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).collectWith(function, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectWith((ArrayList) iterable, function, parameter); } if (iterable instanceof List) { return ListIterate.collectWith((List) iterable, function, parameter); } if (iterable instanceof Collection) { return IterableIterate.collectWith( iterable, function, parameter, DefaultSpeciesNewStrategy.INSTANCE.speciesNew( (Collection) iterable, ((Collection) iterable).size())); } if (iterable != null) { return IterableIterate.collectWith(iterable, function, parameter); } throw new IllegalArgumentException("Cannot perform a collectWith on null"); } /** * Same as collectWith but with a targetCollection parameter to gather the results. */ public static > R collectWith( Iterable iterable, Function2 function, P parameter, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).collectWith(function, parameter, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.collectWith((ArrayList) iterable, function, parameter, targetCollection); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.collectWith((List) iterable, function, parameter, targetCollection); } if (iterable != null) { return IterableIterate.collectWith(iterable, function, parameter, targetCollection); } throw new IllegalArgumentException("Cannot perform a collectWith on null"); } /** * Flattens a collection of collections into one "flat" collection. * * @param iterable A list of lists, e.g. { { 1, 2, 3 }, { 4, 5 }, { 6 } } * @return A flattened list, e.g. { 1, 2, 3, 4, 5, 6 } */ public static Collection flatten(Iterable> iterable) { return Iterate.flatCollect(iterable, Functions.>identity()); } /** * Same as {@link #flatten(Iterable)} except that the results are gathered into the specified targetCollection. */ public static > R flatten(Iterable> iterable, R targetCollection) { return Iterate.flatCollect(iterable, Functions.>identity(), targetCollection); } /** * Returns the first element of a collection. In the case of a List it is the element at the first index. In the * case of any other Collection, it is the first element that would be returned during an iteration. If the * Collection is empty, the result is {@code null}. *

* WARNING!!! The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so * if you use this method, the first element could be any element from the Set. * * @throws IllegalArgumentException if the Collection is null */ public static T getFirst(Iterable iterable) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).getFirst(); } if (iterable instanceof List) { return ListIterate.getFirst((List) iterable); } if (iterable instanceof SortedSet && !((SortedSet) iterable).isEmpty()) { return ((SortedSet) iterable).first(); } if (iterable instanceof Collection) { return Iterate.isEmpty(iterable) ? null : iterable.iterator().next(); } if (iterable != null) { return IterableIterate.getFirst(iterable); } throw new IllegalArgumentException("Cannot get first from null"); } /** * A null-safe check on a collection to see if it isEmpty. A null collection results in a true. */ public static boolean isEmpty(Iterable iterable) { if (iterable == null) { return true; } if (iterable instanceof RichIterable) { return ((RichIterable) iterable).isEmpty(); } if (iterable instanceof Collection) { return ((Collection) iterable).isEmpty(); } return IterableIterate.isEmpty(iterable); } /** * A null-safe check on a collection to see if it is notEmpty. A null collection results in a false. */ public static boolean notEmpty(Iterable iterable) { return !Iterate.isEmpty(iterable); } /** * Returns the last element of a collection. In the case of a List it is the element at the last index. In the * case of any other Collection, it is the last element that would be returned during an iteration. If the * Collection is empty, the result is {@code null}. *

* WARNING!!! The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so * if you use this method, the last element could be any element from the Set. * * @throws IllegalArgumentException if the Collection is null */ public static T getLast(Iterable iterable) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).getLast(); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.getLast((List) iterable); } if (iterable instanceof SortedSet && !((SortedSet) iterable).isEmpty()) { return ((SortedSet) iterable).last(); } if (iterable instanceof LinkedList && !((LinkedList) iterable).isEmpty()) { return ((LinkedList) iterable).getLast(); } if (iterable != null) { return IterableIterate.getLast(iterable); } throw new IllegalArgumentException("Cannot get last from null"); } /** * Returns the first element of the iterable that evaluates to true for the specified predicate, or null if * no element evaluates to true. *

* Example using a Java 8 lambda expression: *

     * Person person = Iterate.detect(people, person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
     * 
*

* Example using an anonymous inner class: *

     * Person person = Iterate.detect(people, new Predicate<Person>()
     * {
     *     public boolean value(Person person)
     *     {
     *         return person.getFirstName().equals("John") && person.getLastName().equals("Smith");
     *     }
     * });
     * 
*/ public static T detect(Iterable iterable, Predicate predicate) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).detect(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.detect((ArrayList) iterable, predicate); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.detect((List) iterable, predicate); } if (iterable != null) { return IterableIterate.detect(iterable, predicate); } throw new IllegalArgumentException("Cannot perform detect on null"); } /** * Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, * or null if no element evaluates to true. *

* Example using a Java 8 lambda expression: *

     * Person person = Iterate.detectWith(people, (person, fullName) -> person.getFullName().equals(fullName), "John Smith");
     * 
*

* Example using an anonymous inner class: *

     * Person person = Iterate.detectWith(people, new Predicate2<Person, String>()
     * {
     *     public boolean value(Person person, String fullName)
     *     {
     *         return person.getFullName().equals(fullName);
     *     }
     * }, "John Smith");
     * 
*/ public static T detectWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).detectWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.detectWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.detectWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.detectWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform detectWith on null"); } /** * Returns the first element of the iterable that evaluates to true for the specified predicate, or returns the * result ifNone if no element evaluates to true. */ public static T detectIfNone(Iterable iterable, Predicate predicate, T ifNone) { T result = Iterate.detect(iterable, predicate); return result == null ? ifNone : result; } /** * Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, * or returns the result ifNone if no element evaluates to true. */ public static T detectWithIfNone( Iterable iterable, Predicate2 predicate, P parameter, T ifNone) { T result = Iterate.detectWith(iterable, predicate, parameter); return result == null ? ifNone : result; } /** * Searches for the first occurrence where the predicate evaluates to true, returns -1 if the predicate does not evaluate to true. */ public static int detectIndex(Iterable iterable, Predicate predicate) { if (iterable instanceof ArrayList) { return ArrayListIterate.detectIndex((ArrayList) iterable, predicate); } if (iterable instanceof List) { return ListIterate.detectIndex((List) iterable, predicate); } if (iterable != null) { return IterableIterate.detectIndex(iterable, predicate); } throw new IllegalArgumentException("Cannot perform detectIndex on null"); } /** * Searches for the first occurrence where the predicate2 and parameter evaluates to true, returns -1 if the predicate2 and parameter do not evaluate to true. */ public static int detectIndexWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof ArrayList) { return ArrayListIterate.detectIndexWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof List) { return ListIterate.detectIndexWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.detectIndexWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform detectIndexWith on null"); } /** * @see RichIterable#injectInto(Object, Function2) */ public static IV injectInto( IV injectValue, Iterable iterable, Function2 function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).injectInto(injectValue, function); } if (iterable instanceof ArrayList) { return ArrayListIterate.injectInto(injectValue, (ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.injectInto(injectValue, (List) iterable, function); } if (iterable != null) { return IterableIterate.injectInto(injectValue, iterable, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * @see RichIterable#injectInto(int, IntObjectToIntFunction) */ public static int injectInto( int injectValue, Iterable iterable, IntObjectToIntFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).injectInto(injectValue, function); } if (iterable instanceof ArrayList) { return ArrayListIterate.injectInto(injectValue, (ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.injectInto(injectValue, (List) iterable, function); } if (iterable != null) { return IterableIterate.injectInto(injectValue, iterable, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * @see RichIterable#injectInto(long, LongObjectToLongFunction) */ public static long injectInto( long injectValue, Iterable iterable, LongObjectToLongFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).injectInto(injectValue, function); } if (iterable instanceof ArrayList) { return ArrayListIterate.injectInto(injectValue, (ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.injectInto(injectValue, (List) iterable, function); } if (iterable != null) { return IterableIterate.injectInto(injectValue, iterable, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * @see RichIterable#injectInto(double, DoubleObjectToDoubleFunction) */ public static double injectInto( double injectValue, Iterable iterable, DoubleObjectToDoubleFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).injectInto(injectValue, function); } if (iterable instanceof ArrayList) { return ArrayListIterate.injectInto(injectValue, (ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.injectInto(injectValue, (List) iterable, function); } if (iterable != null) { return IterableIterate.injectInto(injectValue, iterable, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * @see RichIterable#injectInto(float, FloatObjectToFloatFunction) */ public static float injectInto( float injectValue, Iterable iterable, FloatObjectToFloatFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).injectInto(injectValue, function); } if (iterable instanceof ArrayList) { return ArrayListIterate.injectInto(injectValue, (ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.injectInto(injectValue, (List) iterable, function); } if (iterable != null) { return IterableIterate.injectInto(injectValue, iterable, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * @see RichIterable#sumOfInt(IntFunction) */ public static long sumOfInt(Iterable iterable, IntFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumOfInt(function); } if (iterable instanceof List) { return ListIterate.sumOfInt((List) iterable, function); } if (iterable != null) { return IterableIterate.sumOfInt(iterable, function); } throw new IllegalArgumentException("Cannot perform an sumOfInt on null"); } /** * @see RichIterable#sumOfLong(LongFunction) */ public static long sumOfLong(Iterable iterable, LongFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumOfLong(function); } if (iterable instanceof List) { return ListIterate.sumOfLong((List) iterable, function); } if (iterable != null) { return IterableIterate.sumOfLong(iterable, function); } throw new IllegalArgumentException("Cannot perform an sumOfLong on null"); } /** * @see RichIterable#sumOfFloat(FloatFunction) */ public static double sumOfFloat(Iterable iterable, FloatFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumOfFloat(function); } if (iterable instanceof List) { return ListIterate.sumOfFloat((List) iterable, function); } if (iterable != null) { return IterableIterate.sumOfFloat(iterable, function); } throw new IllegalArgumentException("Cannot perform an sumOfFloat on null"); } /** * @see RichIterable#sumOfDouble(DoubleFunction) */ public static double sumOfDouble(Iterable iterable, DoubleFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumOfDouble(function); } if (iterable instanceof List) { return ListIterate.sumOfDouble((List) iterable, function); } if (iterable != null) { return IterableIterate.sumOfDouble(iterable, function); } throw new IllegalArgumentException("Cannot perform an sumOfDouble on null"); } /** * Returns the BigDecimal sum of the result of applying the function to each element of the iterable. * * @since 6.0 */ public static BigDecimal sumOfBigDecimal(Iterable iterable, Function function) { if (iterable instanceof List) { return ListIterate.sumOfBigDecimal((List) iterable, function); } if (iterable != null) { return IterableIterate.sumOfBigDecimal(iterable, function); } throw new IllegalArgumentException("Cannot perform an sumOfBigDecimal on null"); } /** * Returns the BigInteger sum of the result of applying the function to each element of the iterable. * * @since 6.0 */ public static BigInteger sumOfBigInteger(Iterable iterable, Function function) { if (iterable instanceof List) { return ListIterate.sumOfBigInteger((List) iterable, function); } if (iterable != null) { return IterableIterate.sumOfBigInteger(iterable, function); } throw new IllegalArgumentException("Cannot perform an sumOfBigDecimal on null"); } /** * Groups and sums the values of the iterable using the two specified functions. * * @since 6.0 */ public static MutableMap sumByBigDecimal(Iterable iterable, Function groupBy, Function function) { if (iterable instanceof List) { return ListIterate.sumByBigDecimal((List) iterable, groupBy, function); } if (iterable != null) { return IterableIterate.sumByBigDecimal(iterable, groupBy, function); } throw new IllegalArgumentException("Cannot perform an sumByBigDecimal on null"); } /** * Groups and sums the values of the iterable using the two specified functions. * * @since 6.0 */ public static MutableMap sumByBigInteger(Iterable iterable, Function groupBy, Function function) { if (iterable instanceof List) { return ListIterate.sumByBigInteger((List) iterable, groupBy, function); } if (iterable != null) { return IterableIterate.sumByBigInteger(iterable, groupBy, function); } throw new IllegalArgumentException("Cannot perform an sumByBigInteger on null"); } /** * @see RichIterable#sumByInt(Function, IntFunction) */ public static ObjectLongMap sumByInt(Iterable iterable, Function groupBy, IntFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumByInt(groupBy, function); } if (iterable instanceof List) { return ListIterate.sumByInt((List) iterable, groupBy, function); } if (iterable != null) { return IterableIterate.sumByInt(iterable, groupBy, function); } throw new IllegalArgumentException("Cannot perform an sumByInt on null"); } /** * @see RichIterable#sumByLong(Function, LongFunction) */ public static ObjectLongMap sumByLong(Iterable iterable, Function groupBy, LongFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumByLong(groupBy, function); } if (iterable instanceof List) { return ListIterate.sumByLong((List) iterable, groupBy, function); } if (iterable != null) { return IterableIterate.sumByLong(iterable, groupBy, function); } throw new IllegalArgumentException("Cannot perform an sumByLong on null"); } /** * @see RichIterable#sumOfFloat(FloatFunction) */ public static ObjectDoubleMap sumByFloat(Iterable iterable, Function groupBy, FloatFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumByFloat(groupBy, function); } if (iterable instanceof List) { return ListIterate.sumByFloat((List) iterable, groupBy, function); } if (iterable != null) { return IterableIterate.sumByFloat(iterable, groupBy, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * @see RichIterable#sumOfDouble(DoubleFunction) */ public static ObjectDoubleMap sumByDouble(Iterable iterable, Function groupBy, DoubleFunction function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).sumByDouble(groupBy, function); } if (iterable instanceof List) { return ListIterate.sumByDouble((List) iterable, groupBy, function); } if (iterable != null) { return IterableIterate.sumByDouble(iterable, groupBy, function); } throw new IllegalArgumentException("Cannot perform an injectInto on null"); } /** * Similar to {@link #injectInto(Object, Iterable, Function2)}, except with a parameter is used as third generic argument in function3. */ public static IV injectIntoWith( IV injectValue, Iterable iterable, Function3 function, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).injectIntoWith(injectValue, function, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.injectIntoWith(injectValue, (ArrayList) iterable, function, parameter); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.injectIntoWith(injectValue, (List) iterable, function, parameter); } if (iterable != null) { return IterableIterate.injectIntoWith(injectValue, iterable, function, parameter); } throw new IllegalArgumentException("Cannot perform an injectIntoWith on null"); } /** * Returns true if the predicate evaluates to true for any element of the iterable. * Returns false if the iterable is empty or if no elements return true for the predicate. */ public static boolean anySatisfy(Iterable iterable, Predicate predicate) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).anySatisfy(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.anySatisfy((ArrayList) iterable, predicate); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.anySatisfy((List) iterable, predicate); } if (iterable != null) { return IterableIterate.anySatisfy(iterable, predicate); } throw new IllegalArgumentException("Cannot perform an anySatisfy on null"); } /** * Returns true if the predicate2 and parameter evaluates to true for any element of the iterable. * Returns false if the iterable is empty or if no elements return true for the predicate2. */ public static boolean anySatisfyWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).anySatisfyWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.anySatisfyWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.anySatisfyWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.anySatisfyWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform an anySatisfyWith on null"); } /** * Returns true if the predicate evaluates to true for every element of the iterable, or returns false. * Returns true if the iterable is empty. */ public static boolean allSatisfy(Iterable iterable, Predicate predicate) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).allSatisfy(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.allSatisfy((ArrayList) iterable, predicate); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.allSatisfy((List) iterable, predicate); } if (iterable != null) { return IterableIterate.allSatisfy(iterable, predicate); } throw new IllegalArgumentException("Cannot perform an allSatisfy on null"); } /** * Returns true if the predicate evaluates to true for every element of the iterable, or returns false. */ public static boolean allSatisfyWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).allSatisfyWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.allSatisfyWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.allSatisfyWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.allSatisfyWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform an allSatisfyWith on null"); } /** * Returns true if the predicate evaluates to false for every element of the iterable, or returns false. * Returns true if the iterable is empty. */ public static boolean noneSatisfy(Iterable iterable, Predicate predicate) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).noneSatisfy(predicate); } if (iterable instanceof ArrayList) { return ArrayListIterate.noneSatisfy((ArrayList) iterable, predicate); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.noneSatisfy((List) iterable, predicate); } if (iterable != null) { return IterableIterate.noneSatisfy(iterable, predicate); } throw new IllegalArgumentException("Cannot perform an allSatisfy on null"); } /** * Returns true if the predicate evaluates to false for every element of the iterable, or returns false. * Returns true if the iterable is empty. */ public static boolean noneSatisfyWith( Iterable iterable, Predicate2 predicate, P parameter) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).noneSatisfyWith(predicate, parameter); } if (iterable instanceof ArrayList) { return ArrayListIterate.noneSatisfyWith((ArrayList) iterable, predicate, parameter); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.noneSatisfyWith((List) iterable, predicate, parameter); } if (iterable != null) { return IterableIterate.noneSatisfyWith(iterable, predicate, parameter); } throw new IllegalArgumentException("Cannot perform an noneSatisfyWith on null"); } /** * Iterate over the specified collection applying the specified Function to each element to calculate * a key and return the results as a Map. */ public static MutableMap toMap( Iterable iterable, Function keyFunction) { MutableMap map = UnifiedMap.newMap(); Iterate.forEach(iterable, new MapCollectProcedure(map, keyFunction)); return map; } /** * Iterate over the specified collection applying the specified Functions to each element to calculate * a key and value, and return the results as a Map. */ public static MutableMap toMap( Iterable iterable, Function keyFunction, Function valueFunction) { return Iterate.addToMap(iterable, keyFunction, valueFunction, UnifiedMap.newMap()); } /** * Iterate over the specified collection applying a specific Function to each element to calculate a * key, and add the results to input Map. * This method will mutate the input Map. */ public static > M addToMap( Iterable iterable, Function keyFunction, M map) { Iterate.forEach(iterable, new MapCollectProcedure(map, keyFunction)); return map; } /** * Iterate over the specified collection applying the specified Functions to each element to calculate * a key and value, and add the results to input Map. * This method will mutate the input Map. */ public static > M addToMap( Iterable iterable, Function keyFunction, Function valueFunction, M map) { Iterate.forEach(iterable, new MapCollectProcedure(map, keyFunction, valueFunction)); return map; } /** * Return the specified collection as a sorted List. */ public static > MutableList toSortedList(Iterable iterable) { return Iterate.toSortedList(iterable, Comparators.naturalOrder()); } /** * Return the specified collection as a sorted List using the specified Comparator. */ public static MutableList toSortedList(Iterable iterable, Comparator comparator) { return FastList.newList(iterable).sortThis(comparator); } /** * Returns the size of an iterable. * In the case of Collections and RichIterables, the method size is called. * All other iterables will force a complete iteration to happen, which can be unnecessarily costly. */ public static int sizeOf(Iterable iterable) { if (iterable instanceof Collection) { return ((Collection) iterable).size(); } if (iterable instanceof RichIterable) { return ((RichIterable) iterable).size(); } return Iterate.count(iterable, Predicates.alwaysTrue()); } /** * Returns true if the iterable contains the value. * In the case of Collections and RichIterables, the method contains is called. * All other iterables will force a complete iteration to happen, which can be unnecessarily costly. */ public static boolean contains(Iterable iterable, Object value) { if (iterable instanceof Collection) { return ((Collection) iterable).contains(value); } if (iterable instanceof RichIterable) { return ((RichIterable) iterable).contains(value); } return IterableIterate.detectIndex(iterable, Predicates.equal(value)) > -1; } /** * Converts the specified iterable to an array. */ public static Object[] toArray(Iterable iterable) { if (iterable == null) { throw new NullPointerException(); } if (iterable instanceof Collection) { return ((Collection) iterable).toArray(); } if (iterable instanceof RichIterable) { return ((RichIterable) iterable).toArray(); } MutableList result = Lists.mutable.empty(); Iterate.addAllTo(iterable, result); return result.toArray(); } /** * Copies the specified iterable into the specified array. */ public static T[] toArray(Iterable iterable, T[] target) { if (iterable instanceof Collection) { return ((Collection) iterable).toArray(target); } if (iterable instanceof RichIterable) { return ((RichIterable) iterable).toArray(target); } MutableList result = Lists.mutable.empty(); Iterate.addAllTo(iterable, result); return result.toArray(target); } /** * @see RichIterable#groupBy(Function) */ public static MutableMultimap groupBy( Iterable iterable, Function function) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).groupBy(function); } if (iterable instanceof ArrayList) { return ArrayListIterate.groupBy((ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.groupBy((List) iterable, function); } if (iterable instanceof Collection) { return IterableIterate.groupBy(iterable, function, FastListMultimap.newMultimap()); } if (iterable != null) { return IterableIterate.groupBy(iterable, function); } throw new IllegalArgumentException("Cannot perform a groupBy on null"); } /** * @see RichIterable#groupBy(Function, MutableMultimap) */ public static > R groupBy( Iterable iterable, Function function, R targetMultimap) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).groupBy(function, targetMultimap); } if (iterable instanceof ArrayList) { return ArrayListIterate.groupBy((ArrayList) iterable, function, targetMultimap); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.groupBy((List) iterable, function, targetMultimap); } if (iterable != null) { return IterableIterate.groupBy(iterable, function, targetMultimap); } throw new IllegalArgumentException("Cannot perform a groupBy on null"); } /** * @see RichIterable#aggregateInPlaceBy(Function, Function0, Procedure2) */ public static MutableMap aggregateInPlaceBy( Iterable iterable, Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).aggregateInPlaceBy(groupBy, zeroValueFactory, mutatingAggregator); } if (iterable instanceof ArrayList) { return ArrayListIterate.aggregateInPlaceBy((ArrayList) iterable, groupBy, zeroValueFactory, mutatingAggregator); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.aggregateInPlaceBy((List) iterable, groupBy, zeroValueFactory, mutatingAggregator); } if (iterable != null) { return IterableIterate.aggregateInPlaceBy(iterable, groupBy, zeroValueFactory, mutatingAggregator); } throw new IllegalArgumentException("Cannot perform an aggregateInPlaceBy on null"); } /** * @see RichIterable#aggregateBy(Function, Function0, Function2) */ public static MutableMap aggregateBy( Iterable iterable, Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator); } if (iterable instanceof ArrayList) { return ArrayListIterate.aggregateBy((ArrayList) iterable, groupBy, zeroValueFactory, nonMutatingAggregator); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.aggregateBy((List) iterable, groupBy, zeroValueFactory, nonMutatingAggregator); } if (iterable != null) { return IterableIterate.aggregateBy(iterable, groupBy, zeroValueFactory, nonMutatingAggregator); } throw new IllegalArgumentException("Cannot perform an aggregateBy on null"); } /** * @see RichIterable#groupByEach(Function) */ public static MutableMultimap groupByEach( Iterable iterable, Function> function) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).groupByEach(function); } if (iterable instanceof ArrayList) { return ArrayListIterate.groupByEach((ArrayList) iterable, function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.groupByEach((List) iterable, function); } if (iterable instanceof Collection) { return IterableIterate.groupByEach(iterable, function, FastListMultimap.newMultimap()); } if (iterable != null) { return IterableIterate.groupByEach(iterable, function); } throw new IllegalArgumentException("Cannot perform a groupByEach on null"); } /** * @see RichIterable#groupByEach(Function, MutableMultimap) */ public static > R groupByEach( Iterable iterable, Function> function, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).groupByEach(function, targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.groupByEach((ArrayList) iterable, function, targetCollection); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.groupByEach((List) iterable, function, targetCollection); } if (iterable != null) { return IterableIterate.groupByEach(iterable, function, targetCollection); } throw new IllegalArgumentException("Cannot perform a groupByEach on null"); } /** * @see RichIterable#groupByUniqueKey(Function) */ public static MutableMap groupByUniqueKey( Iterable iterable, Function function) { if (iterable instanceof List) { return ListIterate.groupByUniqueKey((List) iterable, function); } if (iterable != null) { return IterableIterate.groupByUniqueKey(iterable, function); } throw new IllegalArgumentException("Cannot perform a groupByUniqueKey on null"); } /** * @see RichIterable#groupByUniqueKey(Function, MutableMap) */ public static > R groupByUniqueKey( Iterable iterable, Function function, R target) { if (iterable instanceof List) { return ListIterate.groupByUniqueKey((List) iterable, function, target); } if (iterable != null) { return IterableIterate.groupByUniqueKey(iterable, function, target); } throw new IllegalArgumentException("Cannot perform a groupByUniqueKey on null"); } /** * @see RichIterable#min(Comparator) */ public static T min(Iterable iterable, Comparator comparator) { MinComparatorProcedure procedure = new MinComparatorProcedure(comparator); Iterate.forEach(iterable, procedure); return procedure.getResult(); } /** * @see RichIterable#max(Comparator) */ public static T max(Iterable iterable, Comparator comparator) { MaxComparatorProcedure procedure = new MaxComparatorProcedure(comparator); Iterate.forEach(iterable, procedure); return procedure.getResult(); } /** * @see RichIterable#min() */ public static T min(Iterable iterable) { return Iterate.min(iterable, Comparators.naturalOrder()); } /** * @see RichIterable#max() */ public static T max(Iterable iterable) { return Iterate.max(iterable, Comparators.naturalOrder()); } public static T getOnly(Iterable iterable) { if (iterable != null) { return IterableIterate.getOnly(iterable); } throw new IllegalArgumentException("Cannot perform getOnly on null"); } /** * @see RichIterable#zip(Iterable) */ public static Collection> zip(Iterable xs, Iterable ys) { if (xs instanceof MutableCollection) { return ((MutableCollection) xs).zip(ys); } if (xs instanceof ArrayList) { return ArrayListIterate.zip((ArrayList) xs, ys); } if (xs instanceof RandomAccess) { return RandomAccessListIterate.zip((List) xs, ys); } if (xs != null) { return IterableIterate.zip(xs, ys); } throw new IllegalArgumentException("Cannot perform a zip on null"); } /** * @see RichIterable#zip(Iterable, Collection) */ public static >> R zip( Iterable xs, Iterable ys, R targetCollection) { if (xs instanceof RichIterable) { return ((RichIterable) xs).zip(ys, targetCollection); } if (xs instanceof ArrayList) { return ArrayListIterate.zip((ArrayList) xs, ys, targetCollection); } if (xs instanceof RandomAccess) { return RandomAccessListIterate.zip((List) xs, ys, targetCollection); } if (xs != null) { return IterableIterate.zip(xs, ys, targetCollection); } throw new IllegalArgumentException("Cannot perform a zip on null"); } /** * @see RichIterable#zipWithIndex() */ public static Collection> zipWithIndex(Iterable iterable) { if (iterable instanceof MutableCollection) { return ((MutableCollection) iterable).zipWithIndex(); } if (iterable instanceof ArrayList) { return ArrayListIterate.zipWithIndex((ArrayList) iterable); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.zipWithIndex((List) iterable); } if (iterable instanceof Collection) { return IterableIterate.zipWithIndex( iterable, DefaultSpeciesNewStrategy.INSTANCE.>speciesNew( (Collection) iterable, ((Collection) iterable).size())); } if (iterable != null) { return IterableIterate.zipWithIndex(iterable); } throw new IllegalArgumentException("Cannot perform a zipWithIndex on null"); } /** * @see RichIterable#zipWithIndex(Collection) */ public static >> R zipWithIndex( Iterable iterable, R targetCollection) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).zipWithIndex(targetCollection); } if (iterable instanceof ArrayList) { return ArrayListIterate.zipWithIndex((ArrayList) iterable, targetCollection); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.zipWithIndex((List) iterable, targetCollection); } if (iterable != null) { return IterableIterate.zipWithIndex(iterable, targetCollection); } throw new IllegalArgumentException("Cannot perform a zipWithIndex on null"); } /** * @see RichIterable#chunk(int) */ public static RichIterable> chunk(Iterable iterable, int size) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).chunk(size); } if (iterable != null) { return IterableIterate.chunk(iterable, size); } throw new IllegalArgumentException("Cannot perform a chunk on null"); } /** * @see RichIterable#makeString() */ public static String makeString(Iterable iterable) { return Iterate.makeString(iterable, ", "); } /** * @see RichIterable#makeString(String) */ public static String makeString(Iterable iterable, String separator) { return Iterate.makeString(iterable, "", separator, ""); } /** * @see RichIterable#makeString(String, String, String) */ public static String makeString(Iterable iterable, String start, String separator, String end) { Appendable stringBuilder = new StringBuilder(); Iterate.appendString(iterable, stringBuilder, start, separator, end); return stringBuilder.toString(); } /** * @see RichIterable#appendString(Appendable) */ public static void appendString(Iterable iterable, Appendable appendable) { Iterate.appendString(iterable, appendable, ", "); } /** * @see RichIterable#appendString(Appendable, String) */ public static void appendString(Iterable iterable, Appendable appendable, String separator) { Iterate.appendString(iterable, appendable, "", separator, ""); } /** * @see RichIterable#appendString(Appendable, String, String, String) */ public static void appendString( Iterable iterable, Appendable appendable, String start, String separator, String end) { if (iterable instanceof MutableCollection) { ((MutableCollection) iterable).appendString(appendable, start, separator, end); } else if (iterable instanceof RandomAccess) { RandomAccessListIterate.appendString((List) iterable, appendable, start, separator, end); } else if (iterable != null) { IterableIterate.appendString(iterable, appendable, start, separator, end); } else { throw new IllegalArgumentException("Cannot perform an appendString on null"); } } /** * Returns the maximum element out of the iterable based on the natural order of the attribute returned by the function. */ public static > T maxBy(Iterable iterable, Function function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).maxBy(function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.maxBy((List) iterable, function); } if (iterable != null) { return IterableIterate.maxBy(iterable, function); } throw new IllegalArgumentException("Cannot perform a maxBy on null"); } /** * Returns the minimum element out of the iterable based on the natural order of the attribute returned by the function. */ public static > T minBy(Iterable iterable, Function function) { if (iterable instanceof RichIterable) { return ((RichIterable) iterable).minBy(function); } if (iterable instanceof RandomAccess) { return RandomAccessListIterate.minBy((List) iterable, function); } if (iterable != null) { return IterableIterate.minBy(iterable, function); } throw new IllegalArgumentException("Cannot perform a minBy on null"); } /** * Flip the keys and values of the multimap. */ public static HashBagMultimap flip(BagMultimap bagMultimap) { final HashBagMultimap result = new HashBagMultimap(); bagMultimap.forEachKeyMultiValues(new Procedure2>() { public void value(final K key, Iterable values) { Iterate.forEach(values, new Procedure() { public void value(V value) { result.put(value, key); } }); } }); return result; } /** * Flip the keys and values of the multimap. */ public static HashBagMultimap flip(ListMultimap listMultimap) { final HashBagMultimap result = new HashBagMultimap(); listMultimap.forEachKeyMultiValues(new Procedure2>() { public void value(final K key, Iterable values) { Iterate.forEach(values, new Procedure() { public void value(V value) { result.put(value, key); } }); } }); return result; } /** * Flip the keys and values of the multimap. */ public static UnifiedSetMultimap flip(SetMultimap setMultimap) { final UnifiedSetMultimap result = new UnifiedSetMultimap(); setMultimap.forEachKeyMultiValues(new Procedure2>() { public void value(final K key, Iterable values) { Iterate.forEach(values, new Procedure() { public void value(V value) { result.put(value, key); } }); } }); return result; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy