/* * 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.io.Serializable; import java.util.Collection; import java.util.Collections; import com.gs.collections.api.bag.ImmutableBag; import com.gs.collections.api.bag.MutableBag; 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.predicate.primitive.IntPredicate; import com.gs.collections.api.block.procedure.Procedure; import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure; import com.gs.collections.api.list.MutableList; import com.gs.collections.api.map.MutableMap; import com.gs.collections.api.multimap.bag.MutableBagMultimap; 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.api.tuple.primitive.ObjectIntPair; import com.gs.collections.impl.collection.mutable.AbstractSynchronizedMutableCollection; import com.gs.collections.impl.collection.mutable.SynchronizedCollectionSerializationProxy; import com.gs.collections.impl.factory.Bags; import net.jcip.annotations.GuardedBy; /** * A synchronized view of a {@link MutableBag}. It is imperative that the user manually synchronize on the collection when iterating over it using the * standard JDK iterator or JDK 5 for loop, as per {@link Collections#synchronizedCollection(Collection)}. * * @see MutableBag#asSynchronized() * @since 1.0 */ public class SynchronizedBag<T> extends AbstractSynchronizedMutableCollection<T> implements MutableBag<T>, Serializable { private static final long serialVersionUID = 2L; SynchronizedBag(MutableBag<T> bag) { super(bag); } public SynchronizedBag(MutableBag<T> bag, Object newLock) { super(bag, newLock); } /** * This method will take a MutableBag and wrap it directly in a SynchronizedBag. */ public static <E, B extends MutableBag<E>> SynchronizedBag<E> of(B bag) { return new SynchronizedBag<E>(bag); } @Override @GuardedBy("getLock()") protected MutableBag<T> getDelegate() { return (MutableBag<T>) super.getDelegate(); } public MutableBag<T> with(T element) { this.add(element); return this; } public MutableBag<T> without(T element) { this.remove(element); return this; } public MutableBag<T> withAll(Iterable<? extends T> elements) { this.addAllIterable(elements); return this; } public MutableBag<T> withoutAll(Iterable<? extends T> elements) { this.removeAllIterable(elements); return this; } public MutableBag<T> newEmpty() { synchronized (this.getLock()) { return this.getDelegate().newEmpty().asSynchronized(); } } protected Object writeReplace() { return new SynchronizedCollectionSerializationProxy<T>(this.getDelegate()); } public void addOccurrences(T item, int occurrences) { synchronized (this.getLock()) { this.getDelegate().addOccurrences(item, occurrences); } } public boolean removeOccurrences(Object item, int occurrences) { synchronized (this.getLock()) { return this.getDelegate().removeOccurrences(item, occurrences); } } public boolean setOccurrences(T item, int occurrences) { synchronized (this.getLock()) { return this.getDelegate().setOccurrences(item, occurrences); } } public MutableMap<T, Integer> toMapOfItemToCount() { synchronized (this.getLock()) { return this.getDelegate().toMapOfItemToCount(); } } public MutableBag<T> selectByOccurrences(IntPredicate predicate) { synchronized (this.getLock()) { return this.getDelegate().selectByOccurrences(predicate); } } public MutableList<ObjectIntPair<T>> topOccurrences(int count) { synchronized (this.getLock()) { return this.getDelegate().topOccurrences(count); } } public MutableList<ObjectIntPair<T>> bottomOccurrences(int count) { synchronized (this.getLock()) { return this.getDelegate().bottomOccurrences(count); } } public void forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure) { synchronized (this.getLock()) { this.getDelegate().forEachWithOccurrences(objectIntProcedure); } } public int occurrencesOf(Object item) { synchronized (this.getLock()) { return this.getDelegate().occurrencesOf(item); } } public int sizeDistinct() { synchronized (this.getLock()) { return this.getDelegate().sizeDistinct(); } } public String toStringOfItemToCount() { synchronized (this.getLock()) { return this.getDelegate().toStringOfItemToCount(); } } public MutableBag<T> tap(Procedure<? super T> procedure) { synchronized (this.getLock()) { this.forEach(procedure); return this; } } public MutableBag<T> select(Predicate<? super T> predicate) { synchronized (this.getLock()) { return this.getDelegate().select(predicate); } } public <P> MutableBag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter) { synchronized (this.getLock()) { return this.getDelegate().selectWith(predicate, parameter); } } public MutableBag<T> reject(Predicate<? super T> predicate) { synchronized (this.getLock()) { return this.getDelegate().reject(predicate); } } public <P> MutableBag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter) { synchronized (this.getLock()) { return this.getDelegate().rejectWith(predicate, parameter); } } public PartitionMutableBag<T> partition(Predicate<? super T> predicate) { synchronized (this.getLock()) { return this.getDelegate().partition(predicate); } } public <P> PartitionMutableBag<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter) { synchronized (this.getLock()) { return this.getDelegate().partitionWith(predicate, parameter); } } public MutableBooleanBag collectBoolean(BooleanFunction<? super T> booleanFunction) { synchronized (this.getLock()) { return this.getDelegate().collectBoolean(booleanFunction); } } public MutableByteBag collectByte(ByteFunction<? super T> byteFunction) { synchronized (this.getLock()) { return this.getDelegate().collectByte(byteFunction); } } public MutableCharBag collectChar(CharFunction<? super T> charFunction) { synchronized (this.getLock()) { return this.getDelegate().collectChar(charFunction); } } public MutableDoubleBag collectDouble(DoubleFunction<? super T> doubleFunction) { synchronized (this.getLock()) { return this.getDelegate().collectDouble(doubleFunction); } } public MutableFloatBag collectFloat(FloatFunction<? super T> floatFunction) { synchronized (this.getLock()) { return this.getDelegate().collectFloat(floatFunction); } } public MutableIntBag collectInt(IntFunction<? super T> function) { synchronized (this.getLock()) { return this.getDelegate().collectInt(function); } } public MutableLongBag collectLong(LongFunction<? super T> longFunction) { synchronized (this.getLock()) { return this.getDelegate().collectLong(longFunction); } } public MutableShortBag collectShort(ShortFunction<? super T> shortFunction) { synchronized (this.getLock()) { return this.getDelegate().collectShort(shortFunction); } } public <S> MutableBag<S> selectInstancesOf(Class<S> clazz) { synchronized (this.getLock()) { return this.getDelegate().selectInstancesOf(clazz); } } public <V> MutableBag<V> collect(Function<? super T, ? extends V> function) { synchronized (this.getLock()) { return this.getDelegate().collect(function); } } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Deprecated public MutableSet<Pair<T, Integer>> zipWithIndex() { synchronized (this.getLock()) { return this.getDelegate().zipWithIndex(); } } public <P, V> MutableBag<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter) { synchronized (this.getLock()) { return this.getDelegate().collectWith(function, parameter); } } public <V> MutableBag<V> collectIf( Predicate<? super T> predicate, Function<? super T, ? extends V> function) { synchronized (this.getLock()) { return this.getDelegate().collectIf(predicate, function); } } public <V> MutableBag<V> flatCollect(Function<? super T, ? extends Iterable<V>> function) { synchronized (this.getLock()) { return this.getDelegate().flatCollect(function); } } public <V> MutableBagMultimap<V, T> groupBy(Function<? super T, ? extends V> function) { synchronized (this.getLock()) { return this.getDelegate().groupBy(function); } } public <V> MutableBagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function) { synchronized (this.getLock()) { return this.getDelegate().groupByEach(function); } } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Deprecated public <S> MutableBag<Pair<T, S>> zip(Iterable<S> that) { synchronized (this.getLock()) { return this.getDelegate().zip(that); } } public MutableBag<T> asUnmodifiable() { synchronized (this.getLock()) { return UnmodifiableBag.of(this); } } public MutableBag<T> asSynchronized() { return this; } public ImmutableBag<T> toImmutable() { return Bags.immutable.withAll(this); } }