com.gs.collections.impl.utility.internal.RandomAccessListIterate 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 2013 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.impl.utility.internal;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
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.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.list.primitive.MutableBooleanList;
import com.gs.collections.api.list.primitive.MutableByteList;
import com.gs.collections.api.list.primitive.MutableCharList;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.list.primitive.MutableLongList;
import com.gs.collections.api.list.primitive.MutableShortList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.utility.Iterate;
/**
* The ListIterate class provides a few of the methods from the Smalltalk Collection Protocol for use with ArrayLists.
* This includes do:, select:, reject:, collect:, inject:into:, detect:, detect:ifNone:, anySatisfy: and allSatisfy:
*/
public final class RandomAccessListIterate
{
private RandomAccessListIterate()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
public static void toArray(List list, T[] target, int startIndex, int sourceSize)
{
for (int i = 0; i < sourceSize; i++)
{
target[startIndex + i] = list.get(i);
}
}
/**
* @see Iterate#select(Iterable, Predicate)
*/
public static MutableList select(List list, Predicate super T> predicate)
{
return RandomAccessListIterate.select(list, predicate, FastList.newList());
}
/**
* @see Iterate#selectWith(Iterable, Predicate2, Object)
*/
public static MutableList selectWith(
List list,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return RandomAccessListIterate.selectWith(list, predicate, injectedValue, FastList.newList());
}
/**
* @see Iterate#select(Iterable, Predicate, Collection)
*/
public static > R select(
List list,
Predicate super T> predicate,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
*/
public static > R selectWith(
List list,
Predicate2 super T, ? super P> predicate,
P parameter,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item, parameter))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#selectInstancesOf(Iterable, Class)
*/
public static MutableList selectInstancesOf(
List> list,
Class clazz)
{
int size = list.size();
FastList result = FastList.newList(size);
for (int i = 0; i < size; i++)
{
Object item = list.get(i);
if (clazz.isInstance(item))
{
result.add((T) item);
}
}
result.trimToSize();
return result;
}
/**
* @see Iterate#count(Iterable, Predicate)
*/
public static int count(
List list,
Predicate super T> predicate)
{
int count = 0;
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i)))
{
count++;
}
}
return count;
}
public static int countWith(
List list,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
int count = 0;
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i), injectedValue))
{
count++;
}
}
return count;
}
/**
* @see Iterate#collectIf(Iterable, Predicate, Function)
*/
public static MutableList collectIf(
List list,
Predicate super T> predicate,
Function super T, ? extends A> function)
{
return RandomAccessListIterate.collectIf(list, predicate, function, FastList.newList());
}
/**
* @see Iterate#collectIf(Iterable, Predicate, Function, Collection)
*/
public static > R collectIf(
List list,
Predicate super T> predicate,
Function super T, ? extends A> function,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item))
{
targetCollection.add(function.valueOf(item));
}
}
return targetCollection;
}
/**
* @see Iterate#reject(Iterable, Predicate)
*/
public static MutableList reject(List list, Predicate super T> predicate)
{
return RandomAccessListIterate.reject(list, predicate, FastList.newList());
}
/**
* @see Iterate#rejectWith(Iterable, Predicate2, Object)
*/
public static MutableList rejectWith(
List list,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
return RandomAccessListIterate.rejectWith(list, predicate, injectedValue, FastList.newList());
}
/**
* @see Iterate#reject(Iterable, Predicate, Collection)
*/
public static > R reject(
List list,
Predicate super T> predicate,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (!predicate.accept(item))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#reject(Iterable, Predicate, Collection)
*/
public static > R rejectWith(
List list,
Predicate2 super T, ? super P> predicate,
P parameter,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (!predicate.accept(item, parameter))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#collect(Iterable, Function)
*/
public static MutableList collect(
List list,
Function super T, ? extends A> function)
{
return collect(list, function, FastList.newList(list.size()));
}
/**
* @see Iterate#collect(Iterable, Function, Collection)
*/
public static > R collect(
List list,
Function super T, ? extends A> function,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
targetCollection.add(function.valueOf(list.get(i)));
}
return targetCollection;
}
/**
* @see Iterate#collectBoolean(Iterable, BooleanFunction)
*/
public static MutableBooleanList collectBoolean(
List list,
BooleanFunction super T> booleanFunction)
{
return RandomAccessListIterate.collectBoolean(list, booleanFunction, new BooleanArrayList(list.size()));
}
/**
* @see Iterate#collectBoolean(Iterable, BooleanFunction)
*/
public static R collectBoolean(
List list,
BooleanFunction super T> booleanFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(booleanFunction.booleanValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectByte(Iterable, ByteFunction)
*/
public static MutableByteList collectByte(
List list,
ByteFunction super T> byteFunction)
{
return RandomAccessListIterate.collectByte(list, byteFunction, new ByteArrayList(list.size()));
}
/**
* @see Iterate#collectByte(Iterable, ByteFunction)
*/
public static R collectByte(
List list,
ByteFunction super T> byteFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(byteFunction.byteValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectChar(Iterable, CharFunction)
*/
public static MutableCharList collectChar(
List list,
CharFunction super T> charFunction)
{
return RandomAccessListIterate.collectChar(list, charFunction, new CharArrayList(list.size()));
}
/**
* @see Iterate#collectChar(Iterable, CharFunction)
*/
public static R collectChar(
List list,
CharFunction super T> charFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(charFunction.charValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectDouble(Iterable, DoubleFunction)
*/
public static MutableDoubleList collectDouble(
List list,
DoubleFunction super T> doubleFunction)
{
return RandomAccessListIterate.collectDouble(list, doubleFunction, new DoubleArrayList(list.size()));
}
/**
* @see Iterate#collectDouble(Iterable, DoubleFunction)
*/
public static R collectDouble(
List list,
DoubleFunction super T> doubleFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(doubleFunction.doubleValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectFloat(Iterable, FloatFunction)
*/
public static MutableFloatList collectFloat(
List list,
FloatFunction super T> floatFunction)
{
return RandomAccessListIterate.collectFloat(list, floatFunction, new FloatArrayList(list.size()));
}
/**
* @see Iterate#collectFloat(Iterable, FloatFunction)
*/
public static R collectFloat(
List list,
FloatFunction super T> floatFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(floatFunction.floatValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectInt(Iterable, IntFunction)
*/
public static MutableIntList collectInt(
List list,
IntFunction super T> intFunction)
{
return RandomAccessListIterate.collectInt(list, intFunction, new IntArrayList(list.size()));
}
/**
* @see Iterate#collectInt(Iterable, IntFunction)
*/
public static R collectInt(
List list,
IntFunction super T> intFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(intFunction.intValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectLong(Iterable, LongFunction)
*/
public static MutableLongList collectLong(
List list,
LongFunction super T> longFunction)
{
return RandomAccessListIterate.collectLong(list, longFunction, new LongArrayList(list.size()));
}
/**
* @see Iterate#collectLong(Iterable, LongFunction)
*/
public static R collectLong(
List list,
LongFunction super T> longFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(longFunction.longValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectShort(Iterable, ShortFunction)
*/
public static MutableShortList collectShort(
List list,
ShortFunction super T> shortFunction)
{
return RandomAccessListIterate.collectShort(list, shortFunction, new ShortArrayList(list.size()));
}
/**
* @see Iterate#collectShort(Iterable, ShortFunction)
*/
public static R collectShort(
List list,
ShortFunction super T> shortFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(shortFunction.shortValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#flatCollect(Iterable, Function)
*/
public static MutableList flatCollect(
List list,
Function super T, ? extends Iterable> function)
{
return flatCollect(list, function, FastList.newList(list.size()));
}
/**
* @see Iterate#flatCollect(Iterable, Function, Collection)
*/
public static > R flatCollect(
List list,
Function super T, ? extends Iterable> function,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
Iterate.addAllTo(function.valueOf(list.get(i)), targetCollection);
}
return targetCollection;
}
/**
* Returns the last element of a list.
*/
public static T getLast(List collection)
{
return Iterate.isEmpty(collection) ? null : collection.get(collection.size() - 1);
}
/**
* @see Iterate#forEach(Iterable, Procedure)
*/
public static void forEach(List list, Procedure super T> procedure)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
procedure.value(list.get(i));
}
}
/**
* Iterates over the section of the list covered by the specified indexes. The indexes are both inclusive. If the
* from is less than the to, the list is iterated in forward order. If the from is greater than the to, then the
* list is iterated in the reverse order.
*
*
* e.g.
* MutableList people = FastList.newListWith(ted, mary, bob, sally);
* ListIterate.forEach(people, 0, 1, new Procedure()
* {
* public void value(Person person)
* {
* LOGGER.info(person.getName());
* }
* });
*
*
* This code would output ted and mary's names.
*/
public static void forEach(List list, int from, int to, Procedure super T> procedure)
{
if (from < 0 || to < 0)
{
throw new IllegalArgumentException("Neither from nor to may be negative.");
}
if (from <= to)
{
for (int i = from; i <= to; i++)
{
procedure.value(list.get(i));
}
}
else
{
for (int i = from; i >= to; i--)
{
procedure.value(list.get(i));
}
}
}
/**
* Iterates over the section of the list covered by the specified indexes. The indexes are both inclusive. If the
* from is less than the to, the list is iterated in forward order. If the from is greater than the to, then the
* list is iterated in the reverse order. The index passed into the ObjectIntProcedure is the actual index of the
* range.
*
*
* e.g.
* MutableList people = FastList.newListWith(ted, mary, bob, sally);
* ListIterate.forEachWithIndex(people, 0, 1, new ObjectIntProcedure()
* {
* public void value(Person person, int index)
* {
* LOGGER.info(person.getName() + " at index: " + index);
* }
* });
*
*
* This code would output ted and mary's names.
*/
public static void forEachWithIndex(List list, int from, int to, ObjectIntProcedure super T> objectIntProcedure)
{
if (from < 0 || to < 0)
{
throw new IllegalArgumentException("Neither from nor to may be negative.");
}
if (from <= to)
{
for (int i = from; i <= to; i++)
{
objectIntProcedure.value(list.get(i), i);
}
}
else
{
for (int i = from; i >= to; i--)
{
objectIntProcedure.value(list.get(i), i);
}
}
}
/**
* For each element in both of the Lists, operation is evaluated with both elements as parameters.
*/
public static void forEachInBoth(List list1, List list2, Procedure2 super T1, ? super T2> procedure)
{
if (list1 != null && list2 != null)
{
int size1 = list1.size();
int size2 = list2.size();
if (size1 == size2)
{
for (int i = 0; i < size1; i++)
{
procedure.value(list1.get(i), list2.get(i));
}
}
else
{
throw new IllegalArgumentException("Attempt to call forEachInBoth with two Lists of different sizes :"
+ size1
+ ':'
+ size2);
}
}
}
/**
* Iterates over a collection passing each element and the current relative int index to the specified instance of
* ObjectIntProcedure.
*/
public static void forEachWithIndex(List list, ObjectIntProcedure super T> objectIntProcedure)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
objectIntProcedure.value(list.get(i), i);
}
}
public static T detect(List list, Predicate super T> predicate)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item))
{
return item;
}
}
return null;
}
public static T detectWith(List list, Predicate2 super T, ? super IV> predicate, IV injectedValue)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item, injectedValue))
{
return item;
}
}
return null;
}
public static IV injectInto(IV injectValue, List list, Function2 super IV, ? super T, ? extends IV> function)
{
IV result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.value(result, list.get(i));
}
return result;
}
public static int injectInto(int injectValue, List list, IntObjectToIntFunction super T> function)
{
int result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.intValueOf(result, list.get(i));
}
return result;
}
public static long injectInto(long injectValue, List list, LongObjectToLongFunction super T> function)
{
long result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.longValueOf(result, list.get(i));
}
return result;
}
public static double injectInto(double injectValue, List list, DoubleObjectToDoubleFunction super T> function)
{
double result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.doubleValueOf(result, list.get(i));
}
return result;
}
public static float injectInto(float injectValue, List list, FloatObjectToFloatFunction super T> function)
{
float result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.floatValueOf(result, list.get(i));
}
return result;
}
public static long sumOfInt(List list, IntFunction super T> function)
{
long result = 0;
for (int i = 0; i < list.size(); i++)
{
result += (long) function.intValueOf(list.get(i));
}
return result;
}
public static long sumOfLong(List list, LongFunction super T> function)
{
long result = 0L;
for (int i = 0; i < list.size(); i++)
{
result += function.longValueOf(list.get(i));
}
return result;
}
public static double sumOfFloat(List list, FloatFunction super T> function)
{
double result = 0.0d;
for (int i = 0; i < list.size(); i++)
{
result += (double) function.floatValueOf(list.get(i));
}
return result;
}
public static double sumOfDouble(List list, DoubleFunction super T> function)
{
double result = 0.0d;
for (int i = 0; i < list.size(); i++)
{
result += function.doubleValueOf(list.get(i));
}
return result;
}
public static boolean anySatisfy(List list, Predicate super T> predicate)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i)))
{
return true;
}
}
return false;
}
public static boolean anySatisfyWith(List list, Predicate2 super T, ? super IV> predicate, IV injectedValue)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i), injectedValue))
{
return true;
}
}
return false;
}
public static boolean allSatisfy(List list, Predicate super T> predicate)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (!predicate.accept(list.get(i)))
{
return false;
}
}
return true;
}
public static boolean allSatisfyWith(List list, Predicate2 super T, ? super IV> predicate, IV injectedValue)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (!predicate.accept(list.get(i), injectedValue))
{
return false;
}
}
return true;
}
public static boolean noneSatisfy(List list, Predicate super T> predicate)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i)))
{
return false;
}
}
return true;
}
public static boolean noneSatisfyWith(List list, Predicate2 super T, ? super P> predicate, P injectedValue)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i), injectedValue))
{
return false;
}
}
return true;
}
public static Twin> selectAndRejectWith(
List list,
Predicate2 super T, ? super IV> predicate,
IV injectedValue)
{
MutableList positiveResult = Lists.mutable.of();
MutableList negativeResult = Lists.mutable.of();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
(predicate.accept(item, injectedValue) ? positiveResult : negativeResult).add(item);
}
return Tuples.twin(positiveResult, negativeResult);
}
public static PartitionMutableList partition(List list, Predicate super T> predicate)
{
PartitionFastList partitionFastList = new PartitionFastList();
int size = list.size();
for (int i = 0; i < size; i++)
{
T each = list.get(i);
MutableList bucket = predicate.accept(each) ? partitionFastList.getSelected() : partitionFastList.getRejected();
bucket.add(each);
}
return partitionFastList;
}
public static PartitionMutableList partitionWith(List list, Predicate2 super T, ? super P> predicate, P parameter)
{
PartitionFastList partitionFastList = new PartitionFastList();
int size = list.size();
for (int i = 0; i < size; i++)
{
T each = list.get(i);
MutableList bucket = predicate.accept(each, parameter) ? partitionFastList.getSelected() : partitionFastList.getRejected();
bucket.add(each);
}
return partitionFastList;
}
public static List removeIf(List list, Predicate super T> predicate)
{
for (int i = 0; i < list.size(); i++)
{
T each = list.get(i);
if (predicate.accept(each))
{
list.remove(i--);
}
}
return list;
}
public static List removeIfWith(List list, Predicate2 super T, ? super P> predicate, P parameter)
{
for (int i = 0; i < list.size(); i++)
{
T each = list.get(i);
if (predicate.accept(each, parameter))
{
list.remove(i--);
}
}
return list;
}
public static List removeIf(List list, Predicate super T> predicate, Procedure super T> procedure)
{
for (int i = 0; i < list.size(); i++)
{
T each = list.get(i);
if (predicate.accept(each))
{
procedure.value(each);
list.remove(i--);
}
}
return list;
}
/**
* Searches for the first occurence where the predicate evaluates to true.
*/
public static int detectIndex(List list, Predicate super T> predicate)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i)))
{
return i;
}
}
return -1;
}
/**
* Searches for the first occurence where the predicate evaluates to true.
*/
public static int detectIndexWith(List list, Predicate2 super T, ? super IV> predicate, IV injectedValue)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i), injectedValue))
{
return i;
}
}
return -1;
}
public static IV injectIntoWith(
IV injectedValue,
List list,
Function3 super IV, ? super T, ? super P, ? extends IV> function,
P parameter)
{
IV result = injectedValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.value(result, list.get(i), parameter);
}
return result;
}
public static void forEachWith(List list, Procedure2 super T, ? super P> procedure, P parameter)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
procedure.value(list.get(i), parameter);
}
}
public static > R collectWith(
List list,
Function2 super T, ? super P, ? extends A> function,
P parameter,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
targetCollection.add(function.value(list.get(i), parameter));
}
return targetCollection;
}
public static > R distinct(List list, R targetCollection)
{
MutableSet seenSoFar = UnifiedSet.newSet();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (seenSoFar.add(item))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#take(Iterable, int)
*/
public static MutableList take(List list, int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
return RandomAccessListIterate.take(list, count, FastList.newList(Math.min(list.size(), count)));
}
/**
* @see Iterate#take(Iterable, int)
*/
public static > R take(List list, int count, R targetList)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
int end = Math.min(list.size(), count);
for (int i = 0; i < end; i++)
{
targetList.add(list.get(i));
}
return targetList;
}
/**
* @see Iterate#drop(Iterable, int)
*/
public static MutableList drop(List list, int count)
{
return RandomAccessListIterate.drop(list, count, FastList.newList(list.size() - Math.min(list.size(), count)));
}
/**
* @see Iterate#drop(Iterable, int)
*/
public static > R drop(List list, int count, R targetList)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be greater than zero, but was: " + count);
}
int start = Math.min(list.size(), count);
targetList.addAll(list.subList(start, list.size()));
return targetList;
}
/**
* @see RichIterable#appendString(Appendable, String, String, String)
*/
public static void appendString(
List list,
Appendable appendable,
String start,
String separator,
String end)
{
try
{
appendable.append(start);
if (Iterate.notEmpty(list))
{
appendable.append(IterableIterate.stringValueOfItem(list, list.get(0)));
int size = list.size();
for (int i = 1; i < size; i++)
{
appendable.append(separator);
appendable.append(IterableIterate.stringValueOfItem(list, list.get(i)));
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/**
* @see Iterate#groupBy(Iterable, Function)
*/
public static FastListMultimap groupBy(
List list,
Function super T, ? extends V> function)
{
return RandomAccessListIterate.groupBy(list, function, FastListMultimap.newMultimap());
}
/**
* @see Iterate#groupBy(Iterable, Function, MutableMultimap)
*/
public static > R groupBy(
List list,
Function super T, ? extends V> function,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
target.put(function.valueOf(item), item);
}
return target;
}
public static FastListMultimap groupByEach(
List list,
Function super T, ? extends Iterable> function)
{
return RandomAccessListIterate.groupByEach(list, function, FastListMultimap.newMultimap());
}
public static > R groupByEach(
List list,
Function super T, ? extends Iterable> function,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
Iterable iterable = function.valueOf(item);
for (V key : iterable)
{
target.put(key, item);
}
}
return target;
}
public static > T minBy(List list, Function super T, ? extends V> function)
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
T min = list.get(0);
V minValue = function.valueOf(min);
int size = list.size();
for (int i = 1; i < size; i++)
{
T next = list.get(i);
V nextValue = function.valueOf(next);
if (nextValue.compareTo(minValue) < 0)
{
min = next;
minValue = nextValue;
}
}
return min;
}
public static > T maxBy(List list, Function super T, ? extends V> function)
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
T max = list.get(0);
V maxValue = function.valueOf(max);
int size = list.size();
for (int i = 1; i < size; i++)
{
T next = list.get(i);
V nextValue = function.valueOf(next);
if (nextValue.compareTo(maxValue) > 0)
{
max = next;
maxValue = nextValue;
}
}
return max;
}
public static T min(List list, Comparator super T> comparator)
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
T candidate = list.get(0);
int size = list.size();
for (int i = 1; i < size; i++)
{
T next = list.get(i);
if (comparator.compare(next, candidate) < 0)
{
candidate = next;
}
}
return candidate;
}
public static T max(List list, Comparator super T> comparator)
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
T candidate = list.get(0);
int size = list.size();
for (int i = 1; i < size; i++)
{
T next = list.get(i);
if (comparator.compare(next, candidate) > 0)
{
candidate = next;
}
}
return candidate;
}
public static T min(List list)
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
T candidate = list.get(0);
int size = list.size();
for (int i = 1; i < size; i++)
{
T next = list.get(i);
if (((Comparable super T>) next).compareTo(candidate) < 0)
{
candidate = next;
}
}
return candidate;
}
public static T max(List list)
{
if (list.isEmpty())
{
throw new NoSuchElementException();
}
T candidate = list.get(0);
int size = list.size();
for (int i = 1; i < size; i++)
{
T next = list.get(i);
if (((Comparable) next).compareTo(candidate) > 0)
{
candidate = next;
}
}
return candidate;
}
public static MutableList> zip(
List list,
Iterable iterable)
{
return RandomAccessListIterate.zip(list, iterable, FastList.>newList());
}
public static >> R zip(
List list,
Iterable iterable,
R target)
{
Iterator yIterator = iterable.iterator();
int size = list.size();
for (int i = 0; i < size && yIterator.hasNext(); i++)
{
target.add(Tuples.pair(list.get(i), yIterator.next()));
}
return target;
}
public static MutableList> zipWithIndex(List list)
{
return RandomAccessListIterate.zipWithIndex(list, FastList.>newList());
}
public static >> R zipWithIndex(
List list,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(Tuples.pair(list.get(i), i));
}
return target;
}
public static MutableMap aggregateInPlaceBy(
List list,
Function super T, ? extends K> groupBy,
Function0 extends V> zeroValueFactory,
Procedure2 super V, ? super T> mutatingAggregator)
{
MutableMap map = UnifiedMap.newMap();
RandomAccessListIterate.forEach(list, new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator));
return map;
}
public static MutableMap aggregateBy(
List list,
Function super T, ? extends K> groupBy,
Function0 extends V> zeroValueFactory,
Function2 super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap map = UnifiedMap.newMap();
RandomAccessListIterate.forEach(list, new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator));
return map;
}
public static MutableList takeWhile(List list, Predicate super T> predicate)
{
MutableList result = FastList.newList();
int size = list.size();
for (int i = 0; i < size; i++)
{
T each = list.get(i);
if (predicate.accept(each))
{
result.add(each);
}
else
{
return result;
}
}
return result;
}
public static MutableList dropWhile(List list, Predicate super T> predicate)
{
MutableList result = FastList.newList();
int size = list.size();
for (int i = 0; i < size; i++)
{
T each = list.get(i);
if (!predicate.accept(each))
{
result.add(each);
for (int j = i + 1; j < size; j++)
{
T eachNotDropped = list.get(j);
result.add(eachNotDropped);
}
return result;
}
}
return result;
}
public static PartitionMutableList partitionWhile(List list, Predicate super T> predicate)
{
PartitionMutableList result = new PartitionFastList