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

com.gs.collections.impl.collection.mutable.AbstractCollectionAdapter 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.

There is a newer version: 7.0.3
Show newest version
/*
 * 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.collection.mutable;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;

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.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.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.partition.PartitionMutableCollection;
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.api.tuple.Twin;
import com.gs.collections.impl.bag.mutable.HashBag;
import com.gs.collections.impl.bag.sorted.mutable.TreeBag;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.block.factory.Predicates2;
import com.gs.collections.impl.block.factory.PrimitiveFunctions;
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.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import com.gs.collections.impl.map.mutable.primitive.ObjectLongHashMap;
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;
import com.gs.collections.impl.utility.internal.MutableCollectionIterate;

public abstract class AbstractCollectionAdapter
        implements MutableCollection
{
    protected abstract Collection getDelegate();

    protected  MutableCollection wrap(Collection collection)
    {
        return CollectionAdapter.adapt(collection);
    }

    public boolean notEmpty()
    {
        return !this.getDelegate().isEmpty();
    }

    public T getFirst()
    {
        return Iterate.getFirst(this.getDelegate());
    }

    public T getLast()
    {
        return Iterate.getLast(this.getDelegate());
    }

    public MutableCollection tap(Procedure procedure)
    {
        this.forEach(procedure);
        return this;
    }

    public void forEach(Procedure procedure)
    {
        this.each(procedure);
    }

    public void each(Procedure procedure)
    {
        Iterate.forEach(this.getDelegate(), procedure);
    }

    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        Iterate.forEachWithIndex(this.getDelegate(), objectIntProcedure);
    }

    public boolean removeIf(Predicate predicate)
    {
        return Iterate.removeIf(this.getDelegate(), predicate);
    }

    public 

boolean removeIfWith(Predicate2 predicate, P parameter) { return Iterate.removeIfWith(this.getDelegate(), predicate, parameter); } public T detect(Predicate predicate) { return Iterate.detect(this.getDelegate(), predicate); } public T min(Comparator comparator) { return Iterate.min(this, comparator); } public T max(Comparator comparator) { return Iterate.max(this, comparator); } public T min() { return Iterate.min(this); } public T max() { return Iterate.max(this); } public > T minBy(Function function) { return IterableIterate.minBy(this, function); } public > T maxBy(Function function) { return IterableIterate.maxBy(this, function); } public T detectIfNone(Predicate predicate, Function0 function) { T result = this.detect(predicate); return result == null ? function.value() : result; } public int count(Predicate predicate) { return Iterate.count(this.getDelegate(), predicate); } public boolean anySatisfy(Predicate predicate) { return Iterate.anySatisfy(this.getDelegate(), predicate); } public boolean allSatisfy(Predicate predicate) { return Iterate.allSatisfy(this.getDelegate(), predicate); } public boolean noneSatisfy(Predicate predicate) { return Iterate.noneSatisfy(this.getDelegate(), predicate); } public IV injectInto(IV injectedValue, Function2 function) { return Iterate.injectInto(injectedValue, this.getDelegate(), function); } public int injectInto(int injectedValue, IntObjectToIntFunction function) { return Iterate.injectInto(injectedValue, this.getDelegate(), function); } public long injectInto(long injectedValue, LongObjectToLongFunction function) { return Iterate.injectInto(injectedValue, this.getDelegate(), function); } public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { return Iterate.injectInto(injectedValue, this.getDelegate(), function); } public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { return Iterate.injectInto(injectedValue, this.getDelegate(), function); } public long sumOfInt(IntFunction function) { return Iterate.sumOfInt(this.getDelegate(), function); } public double sumOfFloat(FloatFunction function) { return Iterate.sumOfFloat(this.getDelegate(), function); } public long sumOfLong(LongFunction function) { return Iterate.sumOfLong(this.getDelegate(), function); } public double sumOfDouble(DoubleFunction function) { return Iterate.sumOfDouble(this.getDelegate(), function); } public ObjectLongMap sumByInt(Function groupBy, IntFunction function) { ObjectLongHashMap result = ObjectLongHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function)); } public ObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function)); } public ObjectLongMap sumByLong(Function groupBy, LongFunction function) { ObjectLongHashMap result = ObjectLongHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function)); } public ObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function)); } public MutableCollection select(Predicate predicate) { return this.wrap(Iterate.select(this.getDelegate(), predicate)); } public > R select(Predicate predicate, R target) { return Iterate.select(this.getDelegate(), predicate, target); } public MutableCollection reject(Predicate predicate) { return this.wrap(Iterate.reject(this.getDelegate(), predicate)); } public > R reject(Predicate predicate, R target) { return Iterate.reject(this.getDelegate(), predicate, target); } public MutableCollection selectInstancesOf(Class clazz) { return this.wrap(Iterate.selectInstancesOf(this.getDelegate(), clazz)); } public MutableCollection collect(Function function) { return this.wrap(Iterate.collect(this.getDelegate(), function)); } public MutableBooleanCollection collectBoolean(BooleanFunction booleanFunction) { return Iterate.collectBoolean(this.getDelegate(), booleanFunction); } public R collectBoolean(BooleanFunction booleanFunction, R target) { return Iterate.collectBoolean(this.getDelegate(), booleanFunction, target); } public MutableByteCollection collectByte(ByteFunction byteFunction) { return Iterate.collectByte(this.getDelegate(), byteFunction); } public R collectByte(ByteFunction byteFunction, R target) { return Iterate.collectByte(this.getDelegate(), byteFunction, target); } public MutableCharCollection collectChar(CharFunction charFunction) { return Iterate.collectChar(this.getDelegate(), charFunction); } public R collectChar(CharFunction charFunction, R target) { return Iterate.collectChar(this.getDelegate(), charFunction, target); } public MutableDoubleCollection collectDouble(DoubleFunction doubleFunction) { return Iterate.collectDouble(this.getDelegate(), doubleFunction); } public R collectDouble(DoubleFunction doubleFunction, R target) { return Iterate.collectDouble(this.getDelegate(), doubleFunction, target); } public MutableFloatCollection collectFloat(FloatFunction floatFunction) { return Iterate.collectFloat(this.getDelegate(), floatFunction); } public R collectFloat(FloatFunction floatFunction, R target) { return Iterate.collectFloat(this.getDelegate(), floatFunction, target); } public MutableIntCollection collectInt(IntFunction intFunction) { return Iterate.collectInt(this.getDelegate(), intFunction); } public R collectInt(IntFunction intFunction, R target) { return Iterate.collectInt(this.getDelegate(), intFunction, target); } public MutableLongCollection collectLong(LongFunction longFunction) { return Iterate.collectLong(this.getDelegate(), longFunction); } public R collectLong(LongFunction longFunction, R target) { return Iterate.collectLong(this.getDelegate(), longFunction, target); } public MutableShortCollection collectShort(ShortFunction shortFunction) { return Iterate.collectShort(this.getDelegate(), shortFunction); } public R collectShort(ShortFunction shortFunction, R target) { return Iterate.collectShort(this.getDelegate(), shortFunction, target); } public > R collect(Function function, R target) { return Iterate.collect(this.getDelegate(), function, target); } public MutableCollection flatCollect(Function> function) { return this.wrap(Iterate.flatCollect(this.getDelegate(), function)); } public > R flatCollect( Function> function, R target) { return Iterate.flatCollect(this.getDelegate(), function, target); } public MutableCollection collectIf( Predicate predicate, Function function) { return this.wrap(Iterate.collectIf(this.getDelegate(), predicate, function)); } public > R collectIf( Predicate predicate, Function function, R target) { return Iterate.collectIf(this.getDelegate(), predicate, function, target); } public

Twin> selectAndRejectWith( Predicate2 predicate, P parameter) { return Iterate.selectAndRejectWith(this.getDelegate(), predicate, parameter); } public PartitionMutableCollection partition(Predicate predicate) { return (PartitionMutableCollection) Iterate.partition(this.getDelegate(), predicate); } public

PartitionMutableCollection partitionWith(Predicate2 predicate, P parameter) { return (PartitionMutableCollection) Iterate.partitionWith(this.getDelegate(), predicate, parameter); } public int size() { return this.getDelegate().size(); } public boolean isEmpty() { return this.getDelegate().isEmpty(); } public boolean contains(Object o) { return this.getDelegate().contains(o); } public Iterator iterator() { return this.getDelegate().iterator(); } public Object[] toArray() { return this.getDelegate().toArray(); } public E[] toArray(E[] a) { return this.getDelegate().toArray(a); } public boolean add(T o) { return this.getDelegate().add(o); } public boolean remove(Object o) { return this.getDelegate().remove(o); } public boolean containsAll(Collection collection) { return this.getDelegate().containsAll(collection); } public boolean containsAllIterable(Iterable source) { return Iterate.allSatisfyWith(source, Predicates2.in(), this.getDelegate()); } public boolean containsAllArguments(Object... elements) { return ArrayIterate.allSatisfyWith(elements, Predicates2.in(), this.getDelegate()); } public boolean addAll(Collection collection) { boolean result = false; for (T each : collection) { result |= this.add(each); } return result; } public boolean addAllIterable(Iterable iterable) { return Iterate.addAllIterable(iterable, this); } public boolean removeAll(Collection collection) { int currentSize = this.size(); this.removeIfWith(Predicates2.in(), collection); return currentSize != this.size(); } public boolean removeAllIterable(Iterable iterable) { return this.removeAll(CollectionAdapter.wrapSet(iterable)); } public boolean retainAll(Collection collection) { int currentSize = this.size(); this.removeIfWith(Predicates2.notIn(), collection); return currentSize != this.size(); } public boolean retainAllIterable(Iterable iterable) { return this.retainAll(CollectionAdapter.wrapSet(iterable)); } public void clear() { this.getDelegate().clear(); } public

void forEachWith(Procedure2 procedure, P parameter) { Iterate.forEachWith(this.getDelegate(), procedure, parameter); } public

MutableCollection selectWith( Predicate2 predicate, P parameter) { return this.wrap(Iterate.selectWith(this.getDelegate(), predicate, parameter)); } public > R selectWith( Predicate2 predicate, P parameter, R targetCollection) { return Iterate.selectWith(this.getDelegate(), predicate, parameter, targetCollection); } public

MutableCollection rejectWith( Predicate2 predicate, P parameter) { return this.wrap(Iterate.rejectWith(this.getDelegate(), predicate, parameter)); } public > R rejectWith( Predicate2 predicate, P parameter, R targetCollection) { return Iterate.rejectWith(this.getDelegate(), predicate, parameter, targetCollection); } public MutableCollection collectWith( Function2 function, P parameter) { return this.wrap(Iterate.collectWith(this.getDelegate(), function, parameter)); } public > R collectWith( Function2 function, P parameter, R targetCollection) { return Iterate.collectWith(this.getDelegate(), function, parameter, targetCollection); } public IV injectIntoWith( IV injectValue, Function3 function, P parameter) { return Iterate.injectIntoWith(injectValue, this.getDelegate(), function, parameter); } public MutableList toList() { return Lists.mutable.withAll(this.getDelegate()); } public MutableList toSortedList() { return this.toList().sortThis(); } public MutableList toSortedList(Comparator comparator) { return this.toList().sortThis(comparator); } public > MutableList toSortedListBy(Function function) { return this.toSortedList(Comparators.byFunction(function)); } public MutableSortedSet toSortedSet() { return TreeSortedSet.newSet(null, this); } public MutableSortedSet toSortedSet(Comparator comparator) { return TreeSortedSet.newSet(comparator, this); } public > MutableSortedSet toSortedSetBy( Function function) { return this.toSortedSet(Comparators.byFunction(function)); } public MutableSet toSet() { return UnifiedSet.newSet(this.getDelegate()); } public MutableBag toBag() { return HashBag.newBag(this.getDelegate()); } public MutableSortedBag toSortedBag() { return TreeBag.newBag(this.getDelegate()); } public MutableSortedBag toSortedBag(Comparator comparator) { return TreeBag.newBag(comparator, this.getDelegate()); } public > MutableSortedBag toSortedBagBy(Function function) { return this.toSortedBag(Comparators.byFunction(function)); } public MutableMap toMap( Function keyFunction, Function valueFunction) { UnifiedMap map = UnifiedMap.newMap(this.size()); map.collectKeysAndValues(this.getDelegate(), keyFunction, valueFunction); return map; } public MutableSortedMap toSortedMap( Function keyFunction, Function valueFunction) { return TreeSortedMap.newMap().collectKeysAndValues(this.getDelegate(), keyFunction, valueFunction); } public MutableSortedMap toSortedMap( Comparator comparator, Function keyFunction, Function valueFunction) { return TreeSortedMap.newMap(comparator).collectKeysAndValues(this.getDelegate(), keyFunction, valueFunction); } public LazyIterable asLazy() { return LazyIterate.adapt(this); } public

T detectWith(Predicate2 predicate, P parameter) { return Iterate.detectWith(this.getDelegate(), predicate, parameter); } public

T detectWithIfNone( Predicate2 predicate, P parameter, Function0 function) { T result = this.detectWith(predicate, parameter); return result == null ? function.value() : result; } public

int countWith(Predicate2 predicate, P parameter) { return Iterate.countWith(this.getDelegate(), predicate, parameter); } public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return Iterate.anySatisfyWith(this.getDelegate(), predicate, parameter); } public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { return Iterate.allSatisfyWith(this.getDelegate(), predicate, parameter); } public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return Iterate.noneSatisfyWith(this.getDelegate(), predicate, parameter); } @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 MutableMultimap groupBy( Function function) { return Iterate.groupBy(this.getDelegate(), function); } public > R groupBy( Function function, R target) { return Iterate.groupBy(this.getDelegate(), function, target); } public MutableMultimap groupByEach( Function> function) { return Iterate.groupByEach(this.getDelegate(), function); } public > R groupByEach( Function> function, R target) { return Iterate.groupByEach(this.getDelegate(), function, target); } public MutableMap groupByUniqueKey(Function function) { return Iterate.groupByUniqueKey(this.getDelegate(), function); } public > R groupByUniqueKey( Function function, R target) { return Iterate.groupByUniqueKey(this.getDelegate(), function, target); } public MutableCollection> zip(Iterable that) { return this.wrap(Iterate.zip(this.getDelegate(), that)); } public >> R zip(Iterable that, R target) { return Iterate.zip(this.getDelegate(), that, target); } public MutableCollection> zipWithIndex() { return this.wrap(Iterate.zipWithIndex(this.getDelegate())); } public >> R zipWithIndex(R target) { return Iterate.zipWithIndex(this.getDelegate(), target); } public RichIterable> chunk(int size) { return MutableCollectionIterate.chunk(this, size); } public MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } public MutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } }