Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.gs.collections.impl.AbstractRichIterable Maven / Gradle / Ivy
Go to download
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2015 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;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Comparator;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.bag.sorted.MutableSortedBag;
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.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.map.MutableMap;
import com.gs.collections.api.map.primitive.ObjectDoubleMap;
import com.gs.collections.api.map.primitive.ObjectLongMap;
import com.gs.collections.api.map.sorted.MutableSortedMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.sorted.MutableSortedSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.bag.sorted.mutable.TreeBag;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.block.factory.Functions;
import com.gs.collections.impl.block.factory.Predicates;
import com.gs.collections.impl.block.factory.Predicates2;
import com.gs.collections.impl.block.factory.PrimitiveFunctions;
import com.gs.collections.impl.block.factory.Procedures;
import com.gs.collections.impl.block.factory.Procedures2;
import com.gs.collections.impl.block.procedure.AppendStringProcedure;
import com.gs.collections.impl.block.procedure.CollectIfProcedure;
import com.gs.collections.impl.block.procedure.CollectProcedure;
import com.gs.collections.impl.block.procedure.CountProcedure;
import com.gs.collections.impl.block.procedure.FlatCollectProcedure;
import com.gs.collections.impl.block.procedure.GroupByUniqueKeyProcedure;
import com.gs.collections.impl.block.procedure.InjectIntoProcedure;
import com.gs.collections.impl.block.procedure.MapCollectProcedure;
import com.gs.collections.impl.block.procedure.MaxByProcedure;
import com.gs.collections.impl.block.procedure.MaxComparatorProcedure;
import com.gs.collections.impl.block.procedure.MaxProcedure;
import com.gs.collections.impl.block.procedure.MinByProcedure;
import com.gs.collections.impl.block.procedure.MinComparatorProcedure;
import com.gs.collections.impl.block.procedure.MinProcedure;
import com.gs.collections.impl.block.procedure.MultimapEachPutProcedure;
import com.gs.collections.impl.block.procedure.MultimapPutProcedure;
import com.gs.collections.impl.block.procedure.RejectProcedure;
import com.gs.collections.impl.block.procedure.SelectProcedure;
import com.gs.collections.impl.block.procedure.SumOfDoubleProcedure;
import com.gs.collections.impl.block.procedure.SumOfFloatProcedure;
import com.gs.collections.impl.block.procedure.SumOfIntProcedure;
import com.gs.collections.impl.block.procedure.SumOfLongProcedure;
import com.gs.collections.impl.block.procedure.ZipWithIndexProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectBooleanProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectByteProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectCharProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectDoubleProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectFloatProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectIntProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectLongProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectShortProcedure;
import com.gs.collections.impl.block.procedure.primitive.InjectIntoDoubleProcedure;
import com.gs.collections.impl.block.procedure.primitive.InjectIntoFloatProcedure;
import com.gs.collections.impl.block.procedure.primitive.InjectIntoIntProcedure;
import com.gs.collections.impl.block.procedure.primitive.InjectIntoLongProcedure;
import com.gs.collections.impl.factory.Bags;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.factory.Maps;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.factory.SortedMaps;
import com.gs.collections.impl.factory.SortedSets;
import com.gs.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import com.gs.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import com.gs.collections.impl.utility.ArrayIterate;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.LazyIterate;
import com.gs.collections.impl.utility.internal.IterableIterate;
public abstract class AbstractRichIterable implements RichIterable
{
public boolean contains(Object object)
{
return this.anySatisfyWith(Predicates2.equal(), object);
}
public boolean containsAllIterable(Iterable> source)
{
return Iterate.allSatisfyWith(source, Predicates2.in(), this);
}
public boolean containsAllArguments(Object... elements)
{
return ArrayIterate.allSatisfyWith(elements, Predicates2.in(), this);
}
public Object[] toArray()
{
final Object[] result = new Object[this.size()];
this.forEachWithIndex(new ObjectIntProcedure()
{
public void value(T each, int index)
{
result[index] = each;
}
});
return result;
}
public E[] toArray(E[] array)
{
int size = this.size();
final E[] result = array.length < size
? (E[]) Array.newInstance(array.getClass().getComponentType(), size)
: array;
this.forEachWithIndex(new ObjectIntProcedure()
{
public void value(Object each, int index)
{
result[index] = (E) each;
}
});
if (result.length > size)
{
result[size] = null;
}
return result;
}
public boolean isEmpty()
{
return this.size() == 0;
}
public boolean notEmpty()
{
return !this.isEmpty();
}
public MutableList toList()
{
MutableList list = Lists.mutable.empty();
this.forEachWith(Procedures2.addToCollection(), list);
return list;
}
public MutableList toSortedList()
{
return this.toList().sortThis();
}
public MutableList toSortedList(Comparator super T> comparator)
{
return this.toList().sortThis(comparator);
}
public > MutableList toSortedListBy(Function super T, ? extends V> function)
{
return this.toSortedList(Comparators.byFunction(function));
}
public MutableSortedSet toSortedSet()
{
MutableSortedSet treeSet = SortedSets.mutable.empty();
this.forEachWith(Procedures2.addToCollection(), treeSet);
return treeSet;
}
public MutableSortedSet toSortedSet(Comparator super T> comparator)
{
MutableSortedSet treeSet = SortedSets.mutable.with(comparator);
this.forEachWith(Procedures2.addToCollection(), treeSet);
return treeSet;
}
public > MutableSortedSet toSortedSetBy(Function super T, ? extends V> function)
{
return this.toSortedSet(Comparators.byFunction(function));
}
public MutableSet toSet()
{
MutableSet set = Sets.mutable.empty();
this.forEachWith(Procedures2.addToCollection(), set);
return set;
}
public MutableBag toBag()
{
MutableBag bag = Bags.mutable.empty();
this.forEachWith(Procedures2.addToCollection(), bag);
return bag;
}
public MutableSortedBag toSortedBag()
{
MutableSortedBag sortedBag = TreeBag.newBag();
this.forEachWith(Procedures2.addToCollection(), sortedBag);
return sortedBag;
}
public MutableSortedBag toSortedBag(Comparator super T> comparator)
{
MutableSortedBag sortedBag = TreeBag.newBag(comparator);
this.forEachWith(Procedures2.addToCollection(), sortedBag);
return sortedBag;
}
public > MutableSortedBag toSortedBagBy(Function super T, ? extends V> function)
{
return this.toSortedBag(Comparators.byFunction(function));
}
public MutableMap toMap(
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction)
{
MutableMap map = Maps.mutable.empty();
this.forEach(new MapCollectProcedure(map, keyFunction, valueFunction));
return map;
}
public MutableSortedMap toSortedMap(
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction)
{
MutableSortedMap sortedMap = SortedMaps.mutable.empty();
this.forEach(new MapCollectProcedure(sortedMap, keyFunction, valueFunction));
return sortedMap;
}
public MutableSortedMap toSortedMap(
Comparator super K> comparator,
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction)
{
MutableSortedMap sortedMap = SortedMaps.mutable.with(comparator);
this.forEach(new MapCollectProcedure(sortedMap, keyFunction, valueFunction));
return sortedMap;
}
public > R select(Predicate super T> predicate, R target)
{
this.forEach(new SelectProcedure(predicate, target));
return target;
}
public > R selectWith(
Predicate2 super T, ? super P> predicate,
P parameter,
R target)
{
return this.select(Predicates.bind(predicate, parameter), target);
}
public > R reject(Predicate super T> predicate, R target)
{
this.forEach(new RejectProcedure(predicate, target));
return target;
}
public > R rejectWith(
Predicate2 super T, ? super P> predicate,
P parameter,
R target)
{
return this.reject(Predicates.bind(predicate, parameter), target);
}
public > R collect(Function super T, ? extends V> function, R target)
{
this.forEach(new CollectProcedure(function, target));
return target;
}
public > R collectWith(
Function2 super T, ? super P, ? extends V> function,
P parameter,
R target)
{
return this.collect(Functions.bind(function, parameter), target);
}
public > R collectIf(
Predicate super T> predicate,
Function super T, ? extends V> function,
R target)
{
this.forEach(new CollectIfProcedure(target, function, predicate));
return target;
}
public T detectIfNone(Predicate super T> predicate, Function0 extends T> function)
{
T result = this.detect(predicate);
return result == null ? function.value() : result;
}
public T detectWithIfNone(
Predicate2 super T, ? super P> predicate,
P parameter,
Function0 extends T> function)
{
return this.detectIfNone(Predicates.bind(predicate, parameter), function);
}
public T min(Comparator super T> comparator)
{
MinComparatorProcedure procedure = new MinComparatorProcedure(comparator);
this.forEach(procedure);
return procedure.getResult();
}
public T max(Comparator super T> comparator)
{
MaxComparatorProcedure procedure = new MaxComparatorProcedure(comparator);
this.forEach(procedure);
return procedure.getResult();
}
public T min()
{
MinProcedure procedure = new MinProcedure();
this.forEach(procedure);
return procedure.getResult();
}
public T max()
{
MaxProcedure procedure = new MaxProcedure();
this.forEach(procedure);
return procedure.getResult();
}
public > T minBy(Function super T, ? extends V> function)
{
MinByProcedure minByProcedure = new MinByProcedure(function);
this.forEach(minByProcedure);
return minByProcedure.getResult();
}
public > T maxBy(Function super T, ? extends V> function)
{
MaxByProcedure maxByProcedure = new MaxByProcedure(function);
this.forEach(maxByProcedure);
return maxByProcedure.getResult();
}
public LazyIterable asLazy()
{
return LazyIterate.adapt(this);
}
public > R flatCollect(
Function super T, ? extends Iterable> function,
R target)
{
this.forEach(new FlatCollectProcedure(function, target));
return target;
}
public T detect(Predicate super T> predicate)
{
return IterableIterate.detect(this, predicate);
}
public T detectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.detect(Predicates.bind(predicate, parameter));
}
public boolean anySatisfy(Predicate super T> predicate)
{
return IterableIterate.anySatisfy(this, predicate);
}
public boolean allSatisfy(Predicate super T> predicate)
{
return IterableIterate.allSatisfy(this, predicate);
}
public boolean noneSatisfy(Predicate super T> predicate)
{
return IterableIterate.noneSatisfy(this, predicate);
}
public
boolean anySatisfyWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.anySatisfy(Predicates.bind(predicate, parameter));
}
public
boolean allSatisfyWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.allSatisfy(Predicates.bind(predicate, parameter));
}
public
boolean noneSatisfyWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.noneSatisfy(Predicates.bind(predicate, parameter));
}
public int count(Predicate super T> predicate)
{
CountProcedure procedure = new CountProcedure(predicate);
this.forEach(procedure);
return procedure.getCount();
}
public int countWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.count(Predicates.bind(predicate, parameter));
}
public IV injectInto(IV injectedValue, Function2 super IV, ? super T, ? extends IV> function)
{
InjectIntoProcedure procedure = new InjectIntoProcedure(injectedValue, function);
this.forEach(procedure);
return procedure.getResult();
}
public int injectInto(int injectedValue, IntObjectToIntFunction super T> function)
{
InjectIntoIntProcedure procedure = new InjectIntoIntProcedure(injectedValue, function);
this.forEach(procedure);
return procedure.getResult();
}
public long injectInto(long injectedValue, LongObjectToLongFunction super T> function)
{
InjectIntoLongProcedure procedure = new InjectIntoLongProcedure(injectedValue, function);
this.forEach(procedure);
return procedure.getResult();
}
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction super T> function)
{
InjectIntoDoubleProcedure procedure = new InjectIntoDoubleProcedure(injectedValue, function);
this.forEach(procedure);
return procedure.getResult();
}
public float injectInto(float injectedValue, FloatObjectToFloatFunction super T> function)
{
InjectIntoFloatProcedure procedure = new InjectIntoFloatProcedure(injectedValue, function);
this.forEach(procedure);
return procedure.getResult();
}
public long sumOfInt(IntFunction super T> function)
{
SumOfIntProcedure procedure = new SumOfIntProcedure(function);
this.forEach(procedure);
return procedure.getResult();
}
public double sumOfFloat(FloatFunction super T> function)
{
SumOfFloatProcedure procedure = new SumOfFloatProcedure(function);
this.forEach(procedure);
return procedure.getResult();
}
public long sumOfLong(LongFunction super T> function)
{
SumOfLongProcedure procedure = new SumOfLongProcedure(function);
this.forEach(procedure);
return procedure.getResult();
}
public double sumOfDouble(DoubleFunction super T> function)
{
SumOfDoubleProcedure procedure = new SumOfDoubleProcedure(function);
this.forEach(procedure);
return procedure.getResult();
}
public ObjectLongMap sumByInt(Function groupBy, IntFunction super T> function)
{
ObjectLongHashMap result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function));
}
public ObjectDoubleMap sumByFloat(Function groupBy, FloatFunction super T> function)
{
ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function));
}
public ObjectLongMap sumByLong(Function groupBy, LongFunction super T> function)
{
ObjectLongHashMap result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function));
}
public ObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction super T> function)
{
ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function));
}
public void forEachWithIndex(ObjectIntProcedure super T> objectIntProcedure)
{
IterableIterate.forEachWithIndex(this, objectIntProcedure);
}
public final void forEach(Procedure super T> procedure)
{
this.each(procedure);
}
public void forEachWith(Procedure2 super T, ? super P> procedure, P parameter)
{
this.forEach(Procedures.bind(procedure, parameter));
}
public >> R zip(Iterable that, R target)
{
return IterableIterate.zip(this, that, target);
}
public >> R zipWithIndex(R target)
{
this.forEach(ZipWithIndexProcedure.create(target));
return target;
}
/**
* Returns a string representation of this collection. The string representation consists of a list of the
* collection's elements in the order they are returned by its iterator, enclosed in square brackets
* ("[]" ). Adjacent elements are separated by the characters ", " (comma and space). Elements
* are converted to strings as by String.valueOf(Object) .
*
* This implementation creates an empty string buffer, appends a left square bracket, and iterates over the
* collection appending the string representation of each element in turn. After appending each element except the
* last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the
* string buffer, and returned.
*
* @return a string representation of this collection.
*/
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
public String makeString()
{
return this.makeString(", ");
}
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
public void appendString(Appendable appendable, String separator)
{
AppendStringProcedure appendStringProcedure = new AppendStringProcedure(appendable, separator);
this.forEach(appendStringProcedure);
}
public void appendString(Appendable appendable, String start, String separator, String end)
{
AppendStringProcedure appendStringProcedure = new AppendStringProcedure(appendable, separator);
try
{
appendable.append(start);
this.forEach(appendStringProcedure);
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public boolean containsAll(Collection> collection)
{
return this.containsAllIterable(collection);
}
public R collectBoolean(BooleanFunction super T> booleanFunction, R target)
{
this.forEach(new CollectBooleanProcedure(booleanFunction, target));
return target;
}
public R collectByte(ByteFunction super T> byteFunction, R target)
{
this.forEach(new CollectByteProcedure(byteFunction, target));
return target;
}
public R collectChar(CharFunction super T> charFunction, R target)
{
this.forEach(new CollectCharProcedure(charFunction, target));
return target;
}
public R collectDouble(DoubleFunction super T> doubleFunction, R target)
{
this.forEach(new CollectDoubleProcedure(doubleFunction, target));
return target;
}
public R collectFloat(FloatFunction super T> floatFunction, R target)
{
this.forEach(new CollectFloatProcedure(floatFunction, target));
return target;
}
public R collectInt(IntFunction super T> intFunction, R target)
{
this.forEach(new CollectIntProcedure(intFunction, target));
return target;
}
public R collectLong(LongFunction super T> longFunction, R target)
{
this.forEach(new CollectLongProcedure(longFunction, target));
return target;
}
public R collectShort(ShortFunction super T> shortFunction, R target)
{
this.forEach(new CollectShortProcedure(shortFunction, target));
return target;
}
public > R groupBy(
Function super T, ? extends V> function,
R target)
{
this.forEach(MultimapPutProcedure.on(target, function));
return target;
}
public > R groupByEach(
Function super T, ? extends Iterable> function,
R target)
{
this.forEach(MultimapEachPutProcedure.on(target, function));
return target;
}
public > R groupByUniqueKey(
Function super T, ? extends V> function,
R target)
{
this.forEach(new GroupByUniqueKeyProcedure(target, function));
return target;
}
}