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

org.eclipse.collections.impl.collection.mutable.AbstractCollectionAdapter Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2016 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.collection.mutable;

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

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.partition.PartitionMutableCollection;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.bag.mutable.HashBag;
import org.eclipse.collections.impl.bag.sorted.mutable.TreeBag;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.sorted.mutable.TreeSortedSet;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.LazyIterate;
import org.eclipse.collections.impl.utility.internal.IterableIterate;
import org.eclipse.collections.impl.utility.internal.MutableCollectionIterate;

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

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

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

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

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

    @Override
    public T getOnly()
    {
        Iterator iterator = this.getDelegate().iterator();

        if (!iterator.hasNext())
        {
            throw new IllegalStateException("Size must be 1 but was 0");
        }

        T result = iterator.next();
        if (iterator.hasNext())
        {
            throw new IllegalStateException("Size must be 1 but was greater than 1");
        }

        return result;
    }

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

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

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

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

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

    @Override
    public 

boolean removeIfWith(Predicate2 predicate, P parameter) { return Iterate.removeIfWith(this.getDelegate(), predicate, parameter); } @Override public T detect(Predicate predicate) { return Iterate.detect(this.getDelegate(), predicate); } @Override public

T detectWith(Predicate2 predicate, P parameter) { return Iterate.detectWith(this.getDelegate(), predicate, parameter); } @Override public Optional detectOptional(Predicate predicate) { return Iterate.detectOptional(this.getDelegate(), predicate); } @Override public

Optional detectWithOptional(Predicate2 predicate, P parameter) { return Iterate.detectWithOptional(this.getDelegate(), predicate, parameter); } @Override public T detectIfNone(Predicate predicate, Function0 function) { T result = this.detect(predicate); return result == null ? function.value() : result; } @Override public

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

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

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

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

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

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

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

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

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

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return Iterate.noneSatisfyWith(this.getDelegate(), predicate, parameter); } @Override public String toString() { return this.makeString("[", ", ", "]"); } @Override public void appendString(Appendable appendable, String start, String separator, String end) { IterableIterate.appendString(this, appendable, start, separator, end); } @Override public MutableMultimap groupBy( Function function) { return Iterate.groupBy(this.getDelegate(), function); } @Override public > R groupBy( Function function, R target) { return Iterate.groupBy(this.getDelegate(), function, target); } @Override public MutableMultimap groupByEach( Function> function) { return Iterate.groupByEach(this.getDelegate(), function); } @Override public > R groupByEach( Function> function, R target) { return Iterate.groupByEach(this.getDelegate(), function, target); } @Override public MutableMap groupByUniqueKey(Function function) { return Iterate.groupByUniqueKey(this.getDelegate(), function); } @Override public > R groupByUniqueKey( Function function, R target) { return Iterate.groupByUniqueKey(this.getDelegate(), function, target); } @Override public MutableCollection> zip(Iterable that) { return this.wrap(Iterate.zip(this.getDelegate(), that)); } @Override public >> R zip(Iterable that, R target) { return Iterate.zip(this.getDelegate(), that, target); } @Override public MutableCollection> zipWithIndex() { return this.wrap(Iterate.zipWithIndex(this.getDelegate())); } @Override public >> R zipWithIndex(R target) { return Iterate.zipWithIndex(this.getDelegate(), target); } @Override public RichIterable> chunk(int size) { return MutableCollectionIterate.chunk(this, size); } @Override public MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } @Override public MutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } }