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

com.gs.collections.impl.set.sorted.mutable.SynchronizedSortedSet Maven / Gradle / Ivy

/*
 * Copyright 2011 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.set.sorted.mutable;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.SortedSet;

import com.gs.collections.api.LazyIterable;
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.list.MutableList;
import com.gs.collections.api.list.primitive.MutableBooleanList;
import com.gs.collections.api.list.primitive.MutableByteList;
import com.gs.collections.api.list.primitive.MutableCharList;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.list.primitive.MutableLongList;
import com.gs.collections.api.list.primitive.MutableShortList;
import com.gs.collections.api.multimap.sortedset.MutableSortedSetMultimap;
import com.gs.collections.api.partition.set.sorted.PartitionMutableSortedSet;
import com.gs.collections.api.set.SetIterable;
import com.gs.collections.api.set.sorted.ImmutableSortedSet;
import com.gs.collections.api.set.sorted.MutableSortedSet;
import com.gs.collections.api.set.sorted.SortedSetIterable;
import com.gs.collections.api.stack.MutableStack;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.collection.mutable.AbstractSynchronizedMutableCollection;
import com.gs.collections.impl.collection.mutable.SynchronizedCollectionSerializationProxy;
import com.gs.collections.impl.factory.SortedSets;
import com.gs.collections.impl.stack.mutable.ArrayStack;
import net.jcip.annotations.GuardedBy;
import net.jcip.annotations.ThreadSafe;

/**
 * A synchronized view of a {@link MutableSortedSet}. 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 MutableSortedSet#asSynchronized()
 */
@ThreadSafe
public class SynchronizedSortedSet
        extends AbstractSynchronizedMutableCollection
        implements MutableSortedSet, Serializable
{
    SynchronizedSortedSet(MutableSortedSet set)
    {
        super(set);
    }

    SynchronizedSortedSet(MutableSortedSet set, Object newLock)
    {
        super(set, newLock);
    }

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

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

    @GuardedBy("getLock()")
    private MutableSortedSet getSortedSet()
    {
        return (MutableSortedSet) this.getCollection();
    }

    @Override
    public MutableSortedSet asUnmodifiable()
    {
        synchronized (this.getLock())
        {
            return UnmodifiableSortedSet.of(this);
        }
    }

    @Override
    public ImmutableSortedSet toImmutable()
    {
        synchronized (this.getLock())
        {
            return SortedSets.immutable.ofSortedSet(this.getSortedSet());
        }
    }

    public MutableStack toStack()
    {
        synchronized (this.getLock())
        {
            return ArrayStack.newStack(this);
        }
    }

    @Override
    public MutableSortedSet asSynchronized()
    {
        return this;
    }

    @Override
    public MutableSortedSet clone()
    {
        synchronized (this.getLock())
        {
            return of(this.getSortedSet().clone());
        }
    }

    @Override
    public  MutableList collect(Function function)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collect(function);
        }
    }

    @Override
    public MutableBooleanList collectBoolean(BooleanFunction booleanFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectBoolean(booleanFunction);
        }
    }

    @Override
    public MutableByteList collectByte(ByteFunction byteFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectByte(byteFunction);
        }
    }

    @Override
    public MutableCharList collectChar(CharFunction charFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectChar(charFunction);
        }
    }

    @Override
    public MutableDoubleList collectDouble(DoubleFunction doubleFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectDouble(doubleFunction);
        }
    }

    @Override
    public MutableFloatList collectFloat(FloatFunction floatFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectFloat(floatFunction);
        }
    }

    @Override
    public MutableIntList collectInt(IntFunction intFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectInt(intFunction);
        }
    }

    @Override
    public MutableLongList collectLong(LongFunction longFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectLong(longFunction);
        }
    }

    @Override
    public MutableShortList collectShort(ShortFunction shortFunction)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectShort(shortFunction);
        }
    }

    @Override
    public  MutableList flatCollect(Function> function)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().flatCollect(function);
        }
    }

    @Override
    public  MutableList collectIf(
            Predicate predicate,
            Function function)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectIf(predicate, function);
        }
    }

    @Override
    public  MutableList collectWith(Function2 function, P parameter)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().collectWith(function, parameter);
        }
    }

    @Override
    public  MutableSortedSetMultimap groupBy(Function function)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().groupBy(function);
        }
    }

    @Override
    public  MutableSortedSetMultimap groupByEach(Function> function)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().groupByEach(function);
        }
    }

    @Override
    public MutableSortedSet newEmpty()
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().newEmpty();
        }
    }

    @Override
    public MutableSortedSet reject(Predicate predicate)
    {
        synchronized (this.getLock())
        {
            return this.getSortedSet().reject(predicate);
        }
    }

    @Override
    public 

MutableSortedSet rejectWith(Predicate2 predicate, P parameter) { synchronized (this.getLock()) { return this.getSortedSet().rejectWith(predicate, parameter); } } @Override public MutableSortedSet select(Predicate predicate) { synchronized (this.getLock()) { return this.getSortedSet().select(predicate); } } @Override public

MutableSortedSet selectWith(Predicate2 predicate, P parameter) { synchronized (this.getLock()) { return this.getSortedSet().selectWith(predicate, parameter); } } @Override public PartitionMutableSortedSet partition(Predicate predicate) { synchronized (this.getLock()) { return this.getSortedSet().partition(predicate); } } @Override public

PartitionMutableSortedSet partitionWith(Predicate2 predicate, P parameter) { synchronized (this.getLock()) { return this.getSortedSet().partitionWith(predicate, parameter); } } public PartitionMutableSortedSet partitionWhile(Predicate predicate) { synchronized (this.getLock()) { return this.getSortedSet().partitionWhile(predicate); } } @Override public MutableSortedSet selectInstancesOf(Class clazz) { synchronized (this.getLock()) { return this.getSortedSet().selectInstancesOf(clazz); } } @Override public boolean equals(Object obj) { synchronized (this.getLock()) { return this.getSortedSet().equals(obj); } } @Override public int hashCode() { synchronized (this.getLock()) { return this.getSortedSet().hashCode(); } } @Override public MutableList> zip(Iterable that) { synchronized (this.getLock()) { return this.getSortedSet().zip(that); } } @Override public >> R zip(Iterable that, R target) { synchronized (this.getLock()) { return this.getSortedSet().zip(that, target); } } @Override public MutableSortedSet> zipWithIndex() { synchronized (this.getLock()) { return this.getSortedSet().zipWithIndex(); } } @Override public >> R zipWithIndex(R target) { synchronized (this.getLock()) { return this.getSortedSet().zipWithIndex(target); } } public MutableSortedSet takeWhile(Predicate predicate) { synchronized (this.getLock()) { return this.getSortedSet().takeWhile(predicate); } } public MutableSortedSet dropWhile(Predicate predicate) { synchronized (this.getLock()) { return this.getSortedSet().dropWhile(predicate); } } public MutableSortedSet distinct() { synchronized (this.getLock()) { return this.getSortedSet().distinct(); } } public Comparator comparator() { synchronized (this.getLock()) { return this.getSortedSet().comparator(); } } public MutableSortedSet union(SetIterable set) { synchronized (this.getLock()) { return this.getSortedSet().union(set); } } public > R unionInto(SetIterable set, R targetSet) { synchronized (this.getLock()) { return this.getSortedSet().unionInto(set, targetSet); } } public MutableSortedSet intersect(SetIterable set) { synchronized (this.getLock()) { return this.getSortedSet().intersect(set); } } public > R intersectInto(SetIterable set, R targetSet) { synchronized (this.getLock()) { return this.getSortedSet().intersectInto(set, targetSet); } } public MutableSortedSet difference(SetIterable subtrahendSet) { synchronized (this.getLock()) { return this.getSortedSet().difference(subtrahendSet); } } public > R differenceInto(SetIterable subtrahendSet, R targetSet) { synchronized (this.getLock()) { return this.getSortedSet().differenceInto(subtrahendSet, targetSet); } } public MutableSortedSet symmetricDifference(SetIterable setB) { synchronized (this.getLock()) { return this.getSortedSet().symmetricDifference(setB); } } public > R symmetricDifferenceInto(SetIterable set, R targetSet) { synchronized (this.getLock()) { return this.getSortedSet().symmetricDifferenceInto(set, targetSet); } } public boolean isSubsetOf(SetIterable candidateSuperset) { synchronized (this.getLock()) { return this.getSortedSet().isSubsetOf(candidateSuperset); } } public boolean isProperSubsetOf(SetIterable candidateSuperset) { synchronized (this.getLock()) { return this.getSortedSet().isProperSubsetOf(candidateSuperset); } } public MutableSortedSet> powerSet() { synchronized (this.getLock()) { return this.getSortedSet().powerSet(); } } public LazyIterable> cartesianProduct(SetIterable set) { synchronized (this.getLock()) { return this.getSortedSet().cartesianProduct(set); } } public MutableSortedSet subSet(T fromElement, T toElement) { synchronized (this.getLock()) { return of(this.getSortedSet().subSet(fromElement, toElement), this.getLock()); } } public MutableSortedSet headSet(T toElement) { synchronized (this.getLock()) { return of(this.getSortedSet().headSet(toElement), this.getLock()); } } public MutableSortedSet tailSet(T fromElement) { synchronized (this.getLock()) { return of(this.getSortedSet().tailSet(fromElement), this.getLock()); } } public T first() { synchronized (this.getLock()) { return this.getSortedSet().first(); } } public T last() { synchronized (this.getLock()) { return this.getSortedSet().last(); } } public int compareTo(SortedSetIterable o) { synchronized (this.getLock()) { return this.getSortedSet().compareTo(o); } } @Override public MutableSortedSet with(T element) { this.add(element); return this; } @Override public MutableSortedSet without(T element) { this.remove(element); return this; } @Override public MutableSortedSet withAll(Iterable elements) { this.addAllIterable(elements); return this; } @Override public MutableSortedSet withoutAll(Iterable elements) { this.removeAllIterable(elements); return this; } protected Object writeReplace() { return new SynchronizedCollectionSerializationProxy(this.getSortedSet()); } }