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

com.gs.collections.impl.collection.mutable.SynchronizedMutableCollection 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.io.Serializable;
import java.util.Collection;
import java.util.Collections;

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.collection.ImmutableCollection;
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.multimap.MutableMultimap;
import com.gs.collections.api.partition.PartitionMutableCollection;
import com.gs.collections.api.tuple.Pair;
import net.jcip.annotations.ThreadSafe;

/**
 * A synchronized view of a {@link MutableCollection}. 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 MutableCollection#asSynchronized()
 */
@ThreadSafe
public class SynchronizedMutableCollection
        extends AbstractSynchronizedMutableCollection implements Serializable
{
    private static final long serialVersionUID = 2L;

    SynchronizedMutableCollection(MutableCollection newCollection)
    {
        this(newCollection, null);
    }

    SynchronizedMutableCollection(MutableCollection newCollection, Object newLock)
    {
        super(newCollection, newLock);
    }

    /**
     * This method will take a MutableCollection and wrap it directly in a SynchronizedMutableCollection.  It will
     * take any other non-GS-collection and first adapt it will a CollectionAdapter, and then return a
     * SynchronizedMutableCollection that wraps the adapter.
     */
    public static > SynchronizedMutableCollection of(C collection)
    {
        return new SynchronizedMutableCollection(CollectionAdapter.adapt(collection));
    }

    /**
     * This method will take a MutableCollection and wrap it directly in a SynchronizedMutableCollection.  It will
     * take any other non-GS-collection and first adapt it will a CollectionAdapter, and then return a
     * SynchronizedMutableCollection that wraps the adapter.  Additionally, a developer specifies which lock to use
     * with the collection.
     */
    public static > SynchronizedMutableCollection of(C collection, Object lock)
    {
        return new SynchronizedMutableCollection(CollectionAdapter.adapt(collection), lock);
    }

    protected Object writeReplace()
    {
        return new SynchronizedCollectionSerializationProxy(this.getDelegate());
    }

    public MutableCollection with(T element)
    {
        this.add(element);
        return this;
    }

    public MutableCollection without(T element)
    {
        this.remove(element);
        return this;
    }

    public MutableCollection withAll(Iterable elements)
    {
        this.addAllIterable(elements);
        return this;
    }

    public MutableCollection withoutAll(Iterable elements)
    {
        this.removeAllIterable(elements);
        return this;
    }

    public MutableCollection asUnmodifiable()
    {
        synchronized (this.lock)
        {
            return new UnmodifiableMutableCollection(this);
        }
    }

    public MutableCollection asSynchronized()
    {
        return this;
    }

    public ImmutableCollection toImmutable()
    {
        synchronized (this.lock)
        {
            return this.getDelegate().toImmutable();
        }
    }

    public MutableCollection newEmpty()
    {
        synchronized (this.getLock())
        {
            return this.getDelegate().newEmpty().asSynchronized();
        }
    }

    public MutableCollection tap(Procedure procedure)
    {
        synchronized (this.getLock())
        {
            this.getDelegate().each(procedure);
            return this;
        }
    }

    public MutableCollection select(Predicate predicate)
    {
        synchronized (this.getLock())
        {
            return this.getDelegate().select(predicate);
        }
    }

    public 

MutableCollection selectWith(Predicate2 predicate, P parameter) { synchronized (this.getLock()) { return this.getDelegate().selectWith(predicate, parameter); } } public MutableCollection reject(Predicate predicate) { synchronized (this.getLock()) { return this.getDelegate().reject(predicate); } } public

MutableCollection rejectWith(Predicate2 predicate, P parameter) { synchronized (this.getLock()) { return this.getDelegate().rejectWith(predicate, parameter); } } public PartitionMutableCollection partition(Predicate predicate) { synchronized (this.getLock()) { return this.getDelegate().partition(predicate); } } public

PartitionMutableCollection partitionWith(Predicate2 predicate, P parameter) { synchronized (this.getLock()) { return this.getDelegate().partitionWith(predicate, parameter); } } public MutableBooleanCollection collectBoolean(BooleanFunction booleanFunction) { synchronized (this.getLock()) { return this.getDelegate().collectBoolean(booleanFunction); } } public MutableByteCollection collectByte(ByteFunction byteFunction) { synchronized (this.getLock()) { return this.getDelegate().collectByte(byteFunction); } } public MutableCharCollection collectChar(CharFunction charFunction) { synchronized (this.getLock()) { return this.getDelegate().collectChar(charFunction); } } public MutableDoubleCollection collectDouble(DoubleFunction doubleFunction) { synchronized (this.getLock()) { return this.getDelegate().collectDouble(doubleFunction); } } public MutableFloatCollection collectFloat(FloatFunction floatFunction) { synchronized (this.getLock()) { return this.getDelegate().collectFloat(floatFunction); } } public MutableIntCollection collectInt(IntFunction intFunction) { synchronized (this.getLock()) { return this.getDelegate().collectInt(intFunction); } } public MutableLongCollection collectLong(LongFunction longFunction) { synchronized (this.getLock()) { return this.getDelegate().collectLong(longFunction); } } public MutableShortCollection collectShort(ShortFunction shortFunction) { synchronized (this.getLock()) { return this.getDelegate().collectShort(shortFunction); } } public MutableCollection> zipWithIndex() { synchronized (this.getLock()) { return this.getDelegate().zipWithIndex(); } } public MutableCollection selectInstancesOf(Class clazz) { synchronized (this.getLock()) { return this.getDelegate().selectInstancesOf(clazz); } } public MutableCollection collect(Function function) { synchronized (this.getLock()) { return this.getDelegate().collect(function); } } public MutableCollection collectWith(Function2 function, P parameter) { synchronized (this.getLock()) { return this.getDelegate().collectWith(function, parameter); } } public MutableCollection collectIf(Predicate predicate, Function function) { synchronized (this.getLock()) { return this.getDelegate().collectIf(predicate, function); } } public MutableCollection flatCollect(Function> function) { synchronized (this.getLock()) { return this.getDelegate().flatCollect(function); } } public MutableMultimap groupBy(Function function) { synchronized (this.getLock()) { return this.getDelegate().groupBy(function); } } public MutableMultimap groupByEach(Function> function) { synchronized (this.getLock()) { return this.getDelegate().groupByEach(function); } } public MutableCollection> zip(Iterable that) { synchronized (this.getLock()) { return this.getDelegate().zip(that); } } }