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