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 2014 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.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.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.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.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.mutable.HashBag;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.block.factory.Predicates;
import com.gs.collections.impl.block.procedure.CollectIfProcedure;
import com.gs.collections.impl.block.procedure.CollectProcedure;
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.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.factory.Lists;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.sorted.mutable.TreeSortedMap;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.sorted.mutable.TreeSortedSet;
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.anySatisfy(Predicates.equal(object));
}
public boolean containsAllIterable(Iterable> source)
{
return Iterate.allSatisfy(source, Predicates.in(this));
}
public boolean containsAllArguments(Object... elements)
{
return ArrayIterate.allSatisfy(elements, Predicates.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)
{
final E[] result = array.length < this.size()
? (E[]) Array.newInstance(array.getClass().getComponentType(), this.size())
: array;
this.forEachWithIndex(new ObjectIntProcedure()
{
public void value(Object each, int index)
{
result[index] = (E) each;
}
});
if (result.length > this.size())
{
result[this.size()] = null;
}
return result;
}
public boolean isEmpty()
{
return this.size() == 0;
}
public boolean notEmpty()
{
return this.size() > 0;
}
public MutableList toList()
{
return Lists.mutable.ofAll(this);
}
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()
{
return TreeSortedSet.newSet(null, this);
}
public MutableSortedSet toSortedSet(Comparator super T> comparator)
{
return TreeSortedSet.newSet(comparator, this);
}
public > MutableSortedSet toSortedSetBy(Function super T, ? extends V> function)
{
return this.toSortedSet(Comparators.byFunction(function));
}
public MutableSet toSet()
{
return UnifiedSet.newSet(this);
}
public MutableBag toBag()
{
return HashBag.newBag(this);
}
public MutableMap toMap(
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction)
{
return UnifiedMap.newMap(this.size()).collectKeysAndValues(this, keyFunction, valueFunction);
}
public MutableSortedMap toSortedMap(
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction)
{
return TreeSortedMap.newMap().collectKeysAndValues(this, keyFunction, valueFunction);
}
public MutableSortedMap toSortedMap(Comparator super K> comparator,
Function super T, ? extends K> keyFunction,
Function super T, ? extends V> valueFunction)
{
return TreeSortedMap.newMap(comparator).collectKeysAndValues(this, keyFunction, valueFunction);
}
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 targetCollection)
{
return IterableIterate.selectWith(this, predicate, parameter, targetCollection);
}
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 targetCollection)
{
return IterableIterate.rejectWith(this, predicate, parameter, targetCollection);
}
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 targetCollection)
{
return IterableIterate.collectWith(this, function, parameter, targetCollection);
}
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)
{
T result = this.detectWith(predicate, parameter);
return result == null ? function.value() : result;
}
public T min(Comparator super T> comparator)
{
return Iterate.min(this, comparator);
}
public T max(Comparator super T> comparator)
{
return Iterate.max(this, comparator);
}
public T min()
{
return Iterate.min(this);
}
public T max()
{
return Iterate.max(this);
}
public > T minBy(Function super T, ? extends V> function)
{
return IterableIterate.minBy(this, function);
}
public > T maxBy(Function super T, ? extends V> function)
{
return IterableIterate.maxBy(this, function);
}
public LazyIterable asLazy()
{
return LazyIterate.adapt(this);
}
public > R flatCollect(
Function super T, ? extends Iterable> function, R target)
{
return IterableIterate.flatCollect(this, function, 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 IterableIterate.detectWith(this, predicate, parameter);
}
public int count(Predicate super T> predicate)
{
return IterableIterate.count(this, predicate);
}
public
int countWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return IterableIterate.countWith(this, 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 IterableIterate.anySatisfyWith(this, predicate, parameter);
}
public
boolean allSatisfyWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return IterableIterate.allSatisfyWith(this, predicate, parameter);
}
public
boolean noneSatisfyWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return IterableIterate.noneSatisfyWith(this, predicate, parameter);
}
public IV injectInto(IV injectedValue, Function2 super IV, ? super T, ? extends IV> function)
{
return IterableIterate.injectInto(injectedValue, this, function);
}
public int injectInto(int injectedValue, IntObjectToIntFunction super T> function)
{
return IterableIterate.injectInto(injectedValue, this, function);
}
public long injectInto(long injectedValue, LongObjectToLongFunction super T> function)
{
return IterableIterate.injectInto(injectedValue, this, function);
}
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction super T> function)
{
return IterableIterate.injectInto(injectedValue, this, function);
}
public float injectInto(float injectedValue, FloatObjectToFloatFunction super T> function)
{
return IterableIterate.injectInto(injectedValue, this, function);
}
public long sumOfInt(IntFunction super T> function)
{
return IterableIterate.sumOfInt(this, function);
}
public double sumOfFloat(FloatFunction super T> function)
{
return IterableIterate.sumOfFloat(this, function);
}
public long sumOfLong(LongFunction super T> function)
{
return IterableIterate.sumOfLong(this, function);
}
public double sumOfDouble(DoubleFunction super T> function)
{
return IterableIterate.sumOfDouble(this, function);
}
public void forEachWithIndex(ObjectIntProcedure super T> objectIntProcedure)
{
IterableIterate.forEachWithIndex(this, objectIntProcedure);
}
public void forEachWith(Procedure2 super T, ? super P> procedure, P parameter)
{
IterableIterate.forEachWith(this, procedure, parameter);
}
public >> R zip(Iterable that, R target)
{
return IterableIterate.zip(this, that, target);
}
public >> R zipWithIndex(R target)
{
return IterableIterate.zipWithIndex(this, 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)
{
this.appendString(appendable, "", separator, "");
}
public void appendString(
Appendable appendable,
String start,
String separator,
String end)
{
IterableIterate.appendString(this, appendable, start, separator, end);
}
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;
}
}