org.eclipse.collections.impl.bag.mutable.AbstractMutableBag Maven / Gradle / Ivy
/*
* Copyright (c) 2015 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.bag.mutable;
import java.util.concurrent.ExecutorService;
import org.eclipse.collections.api.annotation.Beta;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.ParallelUnsortedBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
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.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
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.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.ByteHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.DoubleHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.FloatHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.LongHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.lazy.parallel.bag.NonParallelUnsortedBag;
import org.eclipse.collections.impl.partition.bag.PartitionHashBag;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
public abstract class AbstractMutableBag
extends AbstractMutableBagIterable
implements MutableBag
{
public ImmutableBag toImmutable()
{
return Bags.immutable.withAll(this);
}
public UnmodifiableBag asUnmodifiable()
{
return UnmodifiableBag.of(this);
}
public SynchronizedBag asSynchronized()
{
return new SynchronizedBag(this);
}
public MutableBag tap(Procedure super T> procedure)
{
this.forEach(procedure);
return this;
}
public MutableBag selectInstancesOf(final Class clazz)
{
final MutableBag result = HashBag.newBag();
this.forEachWithOccurrences(new ObjectIntProcedure()
{
public void value(T each, int occurrences)
{
if (clazz.isInstance(each))
{
result.addOccurrences((S) each, occurrences);
}
}
});
return result;
}
public MutableBag select(Predicate super T> predicate)
{
return this.select(predicate, this.newEmpty());
}
public MutableBag selectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.selectWith(predicate, parameter, this.newEmpty());
}
public MutableBag reject(Predicate super T> predicate)
{
return this.reject(predicate, this.newEmpty());
}
public MutableBag rejectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.rejectWith(predicate, parameter, this.newEmpty());
}
public PartitionMutableBag partition(final Predicate super T> predicate)
{
final PartitionMutableBag result = new PartitionHashBag();
this.forEachWithOccurrences(new ObjectIntProcedure()
{
public void value(T each, int index)
{
MutableBag bucket = predicate.accept(each) ? result.getSelected() : result.getRejected();
bucket.addOccurrences(each, index);
}
});
return result;
}
public PartitionMutableBag partitionWith(final Predicate2 super T, ? super P> predicate, final P parameter)
{
final PartitionMutableBag result = new PartitionHashBag();
this.forEachWithOccurrences(new ObjectIntProcedure()
{
public void value(T each, int index)
{
MutableBag bucket = predicate.accept(each, parameter) ? result.getSelected() : result.getRejected();
bucket.addOccurrences(each, index);
}
});
return result;
}
public MutableBag collect(Function super T, ? extends V> function)
{
return this.collect(function, HashBag.newBag());
}
public MutableBag collectWith(Function2 super T, ? super P, ? extends V> function, P parameter)
{
return this.collectWith(function, parameter, HashBag.newBag());
}
public MutableBag collectIf(Predicate super T> predicate, Function super T, ? extends V> function)
{
return this.collectIf(predicate, function, HashBag.newBag());
}
public MutableBag flatCollect(Function super T, ? extends Iterable> function)
{
return this.flatCollect(function, HashBag.newBag());
}
public MutableBooleanBag collectBoolean(BooleanFunction super T> booleanFunction)
{
return this.collectBoolean(booleanFunction, new BooleanHashBag());
}
public MutableByteBag collectByte(ByteFunction super T> byteFunction)
{
return this.collectByte(byteFunction, new ByteHashBag());
}
public MutableCharBag collectChar(CharFunction super T> charFunction)
{
return this.collectChar(charFunction, new CharHashBag());
}
public MutableDoubleBag collectDouble(DoubleFunction super T> doubleFunction)
{
return this.collectDouble(doubleFunction, new DoubleHashBag());
}
public MutableFloatBag collectFloat(FloatFunction super T> floatFunction)
{
return this.collectFloat(floatFunction, new FloatHashBag());
}
public MutableIntBag collectInt(IntFunction super T> intFunction)
{
return this.collectInt(intFunction, new IntHashBag());
}
public MutableLongBag collectLong(LongFunction super T> longFunction)
{
return this.collectLong(longFunction, new LongHashBag());
}
public MutableShortBag collectShort(ShortFunction super T> shortFunction)
{
return this.collectShort(shortFunction, new ShortHashBag());
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead.
*/
@Deprecated
public MutableBag> zip(Iterable that)
{
return this.zip(that, HashBag.>newBag());
}
/**
* @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead.
*/
@Deprecated
public MutableSet> zipWithIndex()
{
return this.zipWithIndex(UnifiedSet.>newSet());
}
@Beta
public ParallelUnsortedBag asParallel(ExecutorService executorService, int batchSize)
{
if (executorService == null)
{
throw new NullPointerException();
}
if (batchSize < 1)
{
throw new IllegalArgumentException();
}
return new NonParallelUnsortedBag(this);
}
}