com.gs.collections.impl.utility.internal.IterableIterate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2011 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.utility.internal;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.Function3;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableByteCollection;
import com.gs.collections.api.collection.primitive.MutableCharCollection;
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.collection.primitive.MutableLongCollection;
import com.gs.collections.api.collection.primitive.MutableShortCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.partition.PartitionMutableCollection;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.utility.Iterate;
/**
* The IterableIterate class provides a few of the methods from the Smalltalk Collection Protocol for use with general
* Collection classes. This includes do:, select:, reject:, collect:, inject:into:, detect:, detect:ifNone:, anySatisfy:
* and allSatisfy:
*/
public final class IterableIterate
{
private IterableIterate()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
/**
* @see Iterate#select(Iterable, Predicate)
*/
public static MutableList select(Iterable iterable, Predicate super T> predicate)
{
return IterableIterate.select(iterable, predicate, FastList.newList());
}
/**
* @see Iterate#selectAndRejectWith(Iterable, Predicate2, Object)
*/
public static Twin> selectAndRejectWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.selectAndRejectWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#partition(Iterable, Predicate)
*/
public static PartitionMutableList partition(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.partition(iterable.iterator(), predicate);
}
/**
* @see Iterate#partitionWith(Iterable, Predicate2, Object)
*
* @since 5.0
*/
public static PartitionMutableList partitionWith(Iterable iterable, Predicate2 super T, ? super P> predicate, P parameter)
{
return IteratorIterate.partitionWith(iterable.iterator(), predicate, parameter);
}
public static > R partitionWhile(Iterable iterable, Predicate super T> predicate, R target)
{
return IteratorIterate.partitionWhile(iterable.iterator(), predicate, target);
}
public static > R takeWhile(Iterable iterable, Predicate super T> predicate, R target)
{
return IteratorIterate.takeWhile(iterable.iterator(), predicate, target);
}
public static > R dropWhile(Iterable iterable, Predicate super T> predicate, R target)
{
return IteratorIterate.dropWhile(iterable.iterator(), predicate, target);
}
/**
* @see Iterate#count(Iterable, Predicate)
*/
public static int count(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.count(iterable.iterator(), predicate);
}
/**
* @see Iterate#countWith(Iterable, Predicate2, Object)
*/
public static int countWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.countWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#collectIf(Iterable, Predicate, Function)
*/
public static MutableList collectIf(
Iterable iterable,
Predicate super T> predicate,
Function super T, ? extends V> function)
{
return IterableIterate.collectIf(iterable, predicate, function, FastList.newList());
}
public static boolean isEmpty(Iterable> iterable)
{
return !iterable.iterator().hasNext();
}
public static boolean notEmpty(Iterable> iterable)
{
return !IterableIterate.isEmpty(iterable);
}
public static T getFirst(Iterable iterable)
{
Iterator iterator = iterable.iterator();
if (iterator.hasNext())
{
return iterator.next();
}
return null;
}
public static T getLast(Iterable iterable)
{
T last = null;
Iterator iterator = iterable.iterator();
while (iterator.hasNext())
{
last = iterator.next();
}
return last;
}
/**
* @see Iterate#select(Iterable, Predicate, Collection)
*/
public static > R select(
Iterable iterable,
Predicate super T> predicate,
R targetCollection)
{
return IteratorIterate.select(iterable.iterator(), predicate, targetCollection);
}
/**
* @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
*/
public static > R selectWith(
Iterable iterable,
Predicate2 super T, ? super P> predicate,
P injectedValue,
R targetCollection)
{
return IteratorIterate.selectWith(iterable.iterator(), predicate, injectedValue, targetCollection);
}
/**
* @see Iterate#selectInstancesOf(Iterable, Class)
*/
public static MutableList selectInstancesOf(
Iterable> iterable,
Class clazz)
{
FastList result = FastList.newList();
IterableIterate.selectInstancesOf(iterable, clazz, result);
result.trimToSize();
return result;
}
/**
* @see Iterate#selectInstancesOf(Iterable, Class)
*/
public static > R selectInstancesOf(
Iterable> iterable,
Class clazz,
R targetCollection)
{
return IteratorIterate.selectInstancesOf(iterable.iterator(), clazz, targetCollection);
}
/**
* @see Iterate#collectIf(Iterable, Predicate, Function, Collection)
*/
public static > R collectIf(
Iterable iterable,
Predicate super T> predicate,
Function super T, ? extends V> function,
R targetCollection)
{
return IteratorIterate.collectIf(iterable.iterator(), predicate, function, targetCollection);
}
/**
* @see Iterate#reject(Iterable, Predicate)
*/
public static MutableList reject(Iterable iterable, Predicate super T> predicate)
{
return IterableIterate.reject(iterable, predicate, FastList.newList());
}
/**
* @see Iterate#reject(Iterable, Predicate, Collection)
*/
public static > R reject(
Iterable iterable,
Predicate super T> predicate,
R targetCollection)
{
return IteratorIterate.reject(iterable.iterator(), predicate, targetCollection);
}
/**
* @see Iterate#rejectWith(Iterable, Predicate2, Object, Collection)
*/
public static > R rejectWith(
Iterable iterable,
Predicate2 super T, ? super P> predicate,
P parameter,
R targetCollection)
{
return IteratorIterate.rejectWith(iterable.iterator(), predicate, parameter, targetCollection);
}
/**
* @see Iterate#collect(Iterable, Function)
*/
public static MutableList collect(
Iterable iterable,
Function super T, ? extends V> function)
{
return IterableIterate.collect(iterable, function, FastList.newList());
}
/**
* @see Iterate#collect(Iterable, Function, Collection)
*/
public static > R collect(
Iterable iterable,
Function super T, ? extends V> function,
R targetCollection)
{
return IteratorIterate.collect(iterable.iterator(), function, targetCollection);
}
/**
* @see Iterate#collectBoolean(Iterable, BooleanFunction)
*/
public static MutableBooleanCollection collectBoolean(
Iterable iterable,
BooleanFunction super T> booleanFunction)
{
return IteratorIterate.collectBoolean(iterable.iterator(), booleanFunction);
}
/**
* @see Iterate#collectBoolean(Iterable, BooleanFunction, MutableBooleanCollection)
*/
public static R collectBoolean(Iterable iterable, BooleanFunction super T> booleanFunction, R target)
{
return IteratorIterate.collectBoolean(iterable.iterator(), booleanFunction, target);
}
/**
* @see Iterate#collectByte(Iterable, ByteFunction)
*/
public static MutableByteCollection collectByte(
Iterable iterable,
ByteFunction super T> byteFunction)
{
return IteratorIterate.collectByte(iterable.iterator(), byteFunction);
}
/**
* @see Iterate#collectByte(Iterable, ByteFunction,
* MutableByteCollection)
*/
public static R collectByte(Iterable iterable, ByteFunction super T> byteFunction, R target)
{
return IteratorIterate.collectByte(iterable.iterator(), byteFunction, target);
}
/**
* @see Iterate#collectChar(Iterable, CharFunction)
*/
public static MutableCharCollection collectChar(
Iterable iterable,
CharFunction super T> charFunction)
{
return IteratorIterate.collectChar(iterable.iterator(), charFunction);
}
/**
* @see Iterate#collectChar(Iterable, CharFunction,
* MutableCharCollection)
*/
public static R collectChar(Iterable iterable, CharFunction super T> charFunction, R target)
{
return IteratorIterate.collectChar(iterable.iterator(), charFunction, target);
}
/**
* @see Iterate#collectDouble(Iterable, DoubleFunction)
*/
public static MutableDoubleCollection collectDouble(
Iterable iterable,
DoubleFunction super T> doubleFunction)
{
return IteratorIterate.collectDouble(iterable.iterator(), doubleFunction);
}
/**
* @see Iterate#collectDouble(Iterable, DoubleFunction,
* MutableDoubleCollection)
*/
public static R collectDouble(Iterable iterable, DoubleFunction super T> doubleFunction, R target)
{
return IteratorIterate.collectDouble(iterable.iterator(), doubleFunction, target);
}
/**
* @see Iterate#collectFloat(Iterable, FloatFunction)
*/
public static MutableFloatCollection collectFloat(
Iterable iterable,
FloatFunction super T> floatFunction)
{
return IteratorIterate.collectFloat(iterable.iterator(), floatFunction);
}
/**
* @see Iterate#collectFloat(Iterable, FloatFunction,
* MutableFloatCollection)
*/
public static R collectFloat(Iterable iterable, FloatFunction super T> floatFunction, R target)
{
return IteratorIterate.collectFloat(iterable.iterator(), floatFunction, target);
}
/**
* @see Iterate#collectInt(Iterable, IntFunction)
*/
public static MutableIntCollection collectInt(
Iterable iterable,
IntFunction super T> intFunction)
{
return IteratorIterate.collectInt(iterable.iterator(), intFunction);
}
/**
* @see Iterate#collectInt(Iterable, IntFunction,
* MutableIntCollection)
*/
public static R collectInt(Iterable iterable, IntFunction super T> intFunction, R target)
{
return IteratorIterate.collectInt(iterable.iterator(), intFunction, target);
}
/**
* @see Iterate#collectLong(Iterable, LongFunction)
*/
public static MutableLongCollection collectLong(
Iterable iterable,
LongFunction super T> longFunction)
{
return IteratorIterate.collectLong(iterable.iterator(), longFunction);
}
/**
* @see Iterate#collectLong(Iterable, LongFunction,
* MutableLongCollection)
*/
public static R collectLong(Iterable iterable, LongFunction super T> longFunction, R target)
{
return IteratorIterate.collectLong(iterable.iterator(), longFunction, target);
}
/**
* @see Iterate#collectShort(Iterable, ShortFunction)
*/
public static MutableShortCollection collectShort(
Iterable iterable,
ShortFunction super T> shortFunction)
{
return IteratorIterate.collectShort(iterable.iterator(), shortFunction);
}
/**
* @see Iterate#collectShort(Iterable, ShortFunction,
* MutableShortCollection)
*/
public static R collectShort(Iterable iterable, ShortFunction super T> shortFunction, R target)
{
return IteratorIterate.collectShort(iterable.iterator(), shortFunction, target);
}
/**
* @see Iterate#flatCollect(Iterable, Function)
*/
public static MutableList flatCollect(
Iterable iterable,
Function super T, ? extends Iterable> function)
{
return IterableIterate.flatCollect(iterable, function, FastList.newList());
}
/**
* @see Iterate#flatCollect(Iterable, Function, Collection)
*/
public static > R flatCollect(
Iterable iterable,
Function super T, ? extends Iterable> function,
R targetCollection)
{
return IteratorIterate.flatCollect(iterable.iterator(), function, targetCollection);
}
/**
* @see Iterate#forEach(Iterable, Procedure)
*/
public static void forEach(Iterable iterable, Procedure super T> procedure)
{
IteratorIterate.forEach(iterable.iterator(), procedure);
}
/**
* @see Iterate#forEachWithIndex(Iterable, ObjectIntProcedure)
*/
public static void forEachWithIndex(Iterable iterable, ObjectIntProcedure super T> objectIntProcedure)
{
IteratorIterate.forEachWithIndex(iterable.iterator(), objectIntProcedure);
}
/**
* @see Iterate#detect(Iterable, Predicate)
*/
public static T detect(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.detect(iterable.iterator(), predicate);
}
/**
* @see Iterate#detectWith(Iterable, Predicate2, Object)
*/
public static T detectWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.detectWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#injectInto(Object, Iterable, Function2)
*/
public static IV injectInto(
IV injectValue,
Iterable iterable,
Function2 super IV, ? super T, ? extends IV> function)
{
return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
}
/**
* @see Iterate#injectInto(int, Iterable, IntObjectToIntFunction)
*/
public static int injectInto(
int injectValue,
Iterable iterable,
IntObjectToIntFunction super T> function)
{
return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
}
/**
* @see Iterate#injectInto(long, Iterable, LongObjectToLongFunction)
*/
public static long injectInto(
long injectValue,
Iterable iterable,
LongObjectToLongFunction super T> function)
{
return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
}
/**
* @see Iterate#injectInto(double, Iterable, DoubleObjectToDoubleFunction)
*/
public static double injectInto(
double injectValue,
Iterable iterable,
DoubleObjectToDoubleFunction super T> function)
{
return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
}
/**
* @see Iterate#injectInto(float, Iterable, FloatObjectToFloatFunction)
*/
public static float injectInto(
float injectValue,
Iterable iterable,
FloatObjectToFloatFunction super T> function)
{
return IteratorIterate.injectInto(injectValue, iterable.iterator(), function);
}
public static > R distinct(
Iterable iterable,
R targetCollection)
{
return IteratorIterate.distinct(iterable.iterator(), targetCollection);
}
/**
* @see Iterate#sumOfInt(Iterable, IntFunction)
*/
public static long sumOfInt(Iterable iterable, IntFunction super T> function)
{
return IteratorIterate.sumOfInt(iterable.iterator(), function);
}
/**
* @see Iterate#sumOfLong(Iterable, LongFunction)
*/
public static long sumOfLong(Iterable iterable, LongFunction super T> function)
{
return IteratorIterate.sumOfLong(iterable.iterator(), function);
}
/**
* @see Iterate#sumOfFloat(Iterable, FloatFunction)
*/
public static double sumOfFloat(Iterable iterable, FloatFunction super T> function)
{
return IteratorIterate.sumOfFloat(iterable.iterator(), function);
}
/**
* @see Iterate#sumOfDouble(Iterable, DoubleFunction)
*/
public static double sumOfDouble(Iterable iterable, DoubleFunction super T> function)
{
return IteratorIterate.sumOfDouble(iterable.iterator(), function);
}
/**
* @see Iterate#injectIntoWith(Object, Iterable, Function3, Object)
*/
public static IV injectIntoWith(
IV injectValue,
Iterable iterable,
Function3 super IV, ? super T, ? super P, ? extends IV> function,
P parameter)
{
return IteratorIterate.injectIntoWith(injectValue, iterable.iterator(), function, parameter);
}
/**
* @see Iterate#anySatisfy(Iterable, Predicate)
*/
public static boolean anySatisfy(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.anySatisfy(iterable.iterator(), predicate);
}
/**
* @see Iterate#anySatisfyWith(Iterable, Predicate2, Object)
*/
public static boolean anySatisfyWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.anySatisfyWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#allSatisfy(Iterable, Predicate)
*/
public static boolean allSatisfy(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.allSatisfy(iterable.iterator(), predicate);
}
/**
* @see Iterate#allSatisfyWith(Iterable, Predicate2, Object)
*/
public static boolean allSatisfyWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.allSatisfyWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#allSatisfy(Iterable, Predicate)
*/
public static boolean noneSatisfy(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.noneSatisfy(iterable.iterator(), predicate);
}
/**
* @see Iterate#noneSatisfyWith(Iterable, Predicate2, Object)
*/
public static boolean noneSatisfyWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.noneSatisfyWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#removeIf(Iterable, Predicate)
*/
public static Iterable removeIf(Iterable iterable, Predicate super T> predicate)
{
IteratorIterate.removeIf(iterable.iterator(), predicate);
return iterable;
}
/**
* @see Iterate#removeIfWith(Iterable, Predicate2, Object)
*/
public static Iterable removeIfWith(
Iterable iterable,
Predicate2 super T, ? super P> predicate,
P parameter)
{
IteratorIterate.removeIfWith(iterable.iterator(), predicate, parameter);
return iterable;
}
public static Iterable removeIf(
Iterable iterable,
Predicate super T> predicate,
Procedure super T> procedure)
{
IteratorIterate.removeIf(iterable.iterator(), predicate, procedure);
return iterable;
}
/**
* @see Iterate#detectIndex(Iterable, Predicate)
*/
public static int detectIndex(Iterable iterable, Predicate super T> predicate)
{
return IteratorIterate.detectIndex(iterable.iterator(), predicate);
}
/**
* @see Iterate#detectIndexWith(Iterable, Predicate2, Object)
*/
public static int detectIndexWith(
Iterable iterable,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return IteratorIterate.detectIndexWith(iterable.iterator(), predicate, injectedValue);
}
/**
* @see Iterate#forEachWith(Iterable, Procedure2, Object)
*/
public static void forEachWith(
Iterable iterable,
Procedure2 super T, ? super P> procedure,
P parameter)
{
IteratorIterate.forEachWith(iterable.iterator(), procedure, parameter);
}
/**
* @see Iterate#collectWith(Iterable, Function2, Object)
*/
public static MutableList collectWith(
Iterable iterable,
Function2 super T, ? super P, ? extends V> function,
P parameter)
{
return IterableIterate.collectWith(iterable, function, parameter, FastList.newList());
}
/**
* @see Iterate#collectWith(Iterable, Function2, Object, Collection)
*/
public static > R collectWith(
Iterable iterable,
Function2 super T, ? super P, ? extends A> function,
P parameter,
R targetCollection)
{
return IteratorIterate.collectWith(iterable.iterator(), function, parameter, targetCollection);
}
/**
* @see Iterate#take(Iterable, int)
*/
public static Collection take(Iterable iterable, int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
return IterableIterate.take(iterable, count, FastList.newList(count));
}
/**
* @see Iterate#take(Iterable, int)
*/
public static > R take(Iterable iterable, int count, R targetCollection)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
Iterator iterator = iterable.iterator();
while (iterator.hasNext() && count-- > 0)
{
targetCollection.add(iterator.next());
}
return targetCollection;
}
/**
* @see Iterate#drop(Iterable, int)
*/
public static Collection drop(Iterable list, int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
return IterableIterate.drop(list, count, FastList.newList());
}
/**
* @see Iterate#drop(Iterable, int)
*/
public static > R drop(Iterable iterable, int count, R targetCollection)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
Iterator iterator = iterable.iterator();
for (int i = 0; iterator.hasNext(); i++)
{
T element = iterator.next();
if (i >= count)
{
targetCollection.add(element);
}
}
return targetCollection;
}
/**
* @see RichIterable#appendString(Appendable, String, String, String)
*/
public static void appendString(
Iterable iterable,
Appendable appendable,
String start,
String separator,
String end)
{
try
{
appendable.append(start);
Iterator iterator = iterable.iterator();
if (iterator.hasNext())
{
appendable.append(stringValueOfItem(iterable, iterator.next()));
while (iterator.hasNext())
{
appendable.append(separator);
appendable.append(stringValueOfItem(iterable, iterator.next()));
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/**
* @see Iterate#groupBy(Iterable, Function)
*/
public static FastListMultimap groupBy(
Iterable iterable,
Function super T, ? extends V> function)
{
return IterableIterate.groupBy(iterable, function, new FastListMultimap());
}
/**
* @see Iterate#groupBy(Iterable, Function, MutableMultimap)
*/
public static > R groupBy(
Iterable iterable,
Function super T, ? extends V> function,
R target)
{
return IteratorIterate.groupBy(iterable.iterator(), function, target);
}
/**
* @see Iterate#groupByEach(Iterable, Function)
*/
public static FastListMultimap groupByEach(
Iterable iterable,
Function super T, ? extends Iterable> function)
{
return IterableIterate.groupByEach(iterable, function, new FastListMultimap());
}
/**
* @see Iterate#groupByEach(Iterable, Function, MutableMultimap)
*/
public static > R groupByEach(
Iterable iterable,
Function super T, ? extends Iterable> function,
R target)
{
return IteratorIterate.groupByEach(iterable.iterator(), function, target);
}
public static T getOnly(Iterable iterable)
{
Iterator extends T> iterator = iterable.iterator();
if (!iterator.hasNext())
{
throw new IllegalArgumentException("Iterable is empty.");
}
T result = iterator.next();
if (iterator.hasNext())
{
throw new IllegalArgumentException("Iterable has multiple values");
}
return result;
}
/**
* @see Iterate#zip(Iterable, Iterable)
*/
public static MutableList> zip(
Iterable xs,
Iterable ys)
{
return IterableIterate.zip(xs, ys, FastList.>newList());
}
/**
* @see Iterate#zip(Iterable, Iterable, Collection)
*/
public static >> R zip(
Iterable xs,
Iterable ys,
R target)
{
return IteratorIterate.zip(xs.iterator(), ys.iterator(), target);
}
/**
* @see Iterate#zipWithIndex(Iterable)
*/
public static MutableList> zipWithIndex(Iterable iterable)
{
return IterableIterate.zipWithIndex(iterable, FastList.>newList());
}
/**
* @see Iterate#zipWithIndex(Iterable, Collection)
*/
public static >> R zipWithIndex(Iterable iterable, R target)
{
return IteratorIterate.zipWithIndex(iterable.iterator(), target);
}
/**
* @see Iterate#chunk(Iterable, int)
*/
public static RichIterable> chunk(Iterable iterable, int size)
{
return IteratorIterate.chunk(iterable.iterator(), size);
}
public static > T maxBy(Iterable iterable, Function super T, ? extends V> function)
{
return IteratorIterate.maxBy(iterable.iterator(), function);
}
public static > T minBy(Iterable iterable, Function super T, ? extends V> function)
{
return IteratorIterate.minBy(iterable.iterator(), function);
}
/**
* @see Iterate#min(Iterable, Comparator)
*/
public static T min(Iterable iterable, Comparator super T> comparator)
{
return IteratorIterate.min(iterable.iterator(), comparator);
}
/**
* @see Iterate#max(Iterable, Comparator)
*/
public static T max(Iterable iterable, Comparator super T> comparator)
{
return IteratorIterate.max(iterable.iterator(), comparator);
}
/**
* @see Iterate#min(Iterable)
*/
public static T min(Iterable iterable)
{
return IteratorIterate.min(iterable.iterator(), Comparators.naturalOrder());
}
/**
* @see Iterate#max(Iterable)
*/
public static T max(Iterable iterable)
{
return IteratorIterate.max(iterable.iterator(), Comparators.naturalOrder());
}
public static void forEach(Iterable iterable, int from, int to, Procedure super T> procedure)
{
if (from < 0 || to < 0)
{
throw new IllegalArgumentException("Neither from nor to may be negative.");
}
Iterator iterator = IteratorIterate.advanceIteratorTo(iterable.iterator(), from);
int i = from;
while (iterator.hasNext() && i <= to)
{
procedure.value(iterator.next());
i++;
}
}
public static void forEachWithIndex(List iterable, int from, int to, ObjectIntProcedure super T> objectIntProcedure)
{
if (from < 0 || to < 0)
{
throw new IllegalArgumentException("Neither from nor to may be negative.");
}
Iterator iterator = IteratorIterate.advanceIteratorTo(iterable.iterator(), from);
int i = from;
while (iterator.hasNext() && i <= to)
{
objectIntProcedure.value(iterator.next(), i++);
}
}
public static String stringValueOfItem(Iterable iterable, T item)
{
return item == iterable
? "(this " + iterable.getClass().getSimpleName() + ')'
: String.valueOf(item);
}
public static MutableMap aggregateInPlaceBy(
Iterable iterable,
Function super T, ? extends K> groupBy,
Function0 extends V> zeroValueFactory,
Procedure2 super V, ? super T> mutatingAggregator)
{
return IteratorIterate.aggregateBy(iterable.iterator(), groupBy, zeroValueFactory, mutatingAggregator);
}
public static MutableMap aggregateBy(
Iterable iterable,
Function super T, ? extends K> groupBy,
Function0 extends V> zeroValueFactory,
Function2 super V, ? super T, ? extends V> nonMutatingAggregator)
{
return IteratorIterate.aggregateBy(iterable.iterator(), groupBy, zeroValueFactory, nonMutatingAggregator);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy