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

org.eclipse.collections.impl.collection.AbstractSynchronizedRichIterable Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2017 Goldman Sachs and others.
 * 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.collection;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;

import org.eclipse.collections.api.BooleanIterable;
import org.eclipse.collections.api.ByteIterable;
import org.eclipse.collections.api.CharIterable;
import org.eclipse.collections.api.DoubleIterable;
import org.eclipse.collections.api.FloatIterable;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.LongIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.MutableBagIterable;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
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.DoubleObjectToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction;
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.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.partition.PartitionIterable;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.utility.LazyIterate;

public abstract class AbstractSynchronizedRichIterable implements RichIterable
{
    protected final Object lock;
    protected final RichIterable delegate;

    protected AbstractSynchronizedRichIterable(RichIterable delegate, Object lock)
    {
        if (delegate == null)
        {
            throw new IllegalArgumentException("Cannot create a AbstractSynchronizedRichIterable on a null collection");
        }

        this.delegate = delegate;
        this.lock = lock == null ? this : lock;
    }

    protected RichIterable getDelegate()
    {
        return this.delegate;
    }

    protected Object getLock()
    {
        return this.lock;
    }

    @Override
    public boolean equals(Object obj)
    {
        synchronized (this.lock)
        {
            return this.delegate.equals(obj);
        }
    }

    @Override
    public int hashCode()
    {
        synchronized (this.lock)
        {
            return this.delegate.hashCode();
        }
    }

    @Override
    public String toString()
    {
        synchronized (this.lock)
        {
            return this.delegate.toString();
        }
    }

    /**
     * Must be called in a synchronized block.
     */
    @Override
    public Iterator iterator()
    {
        return this.delegate.iterator();
    }

    @Override
    public void forEach(Procedure procedure)
    {
        this.each(procedure);
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        synchronized (this.lock)
        {
            this.delegate.forEachWithIndex(objectIntProcedure);
        }
    }

    @Override
    public 

void forEachWith(Procedure2 procedure, P parameter) { synchronized (this.lock) { this.delegate.forEachWith(procedure, parameter); } } @Override public int size() { synchronized (this.lock) { return this.delegate.size(); } } @Override public boolean isEmpty() { synchronized (this.lock) { return this.delegate.isEmpty(); } } @Override public boolean notEmpty() { synchronized (this.lock) { return this.delegate.notEmpty(); } } @Override public T getFirst() { synchronized (this.lock) { return this.delegate.getFirst(); } } @Override public T getLast() { synchronized (this.lock) { return this.delegate.getLast(); } } @Override public boolean contains(Object o) { synchronized (this.lock) { return this.delegate.contains(o); } } @Override public boolean containsAllIterable(Iterable source) { synchronized (this.lock) { return this.delegate.containsAllIterable(source); } } @Override public boolean containsAll(Collection coll) { synchronized (this.lock) { return this.delegate.containsAll(coll); } } @Override public boolean containsAllArguments(Object... elements) { synchronized (this.lock) { return this.delegate.containsAllArguments(elements); } } @Override public void each(Procedure procedure) { synchronized (this.lock) { this.delegate.forEach(procedure); } } @Override public RichIterable select(Predicate predicate) { synchronized (this.lock) { return this.delegate.select(predicate); } } @Override public > R select(Predicate predicate, R target) { synchronized (this.lock) { return this.delegate.select(predicate, target); } } @Override public

RichIterable selectWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.selectWith(predicate, parameter); } } @Override public > R selectWith( Predicate2 predicate, P parameter, R targetCollection) { synchronized (this.lock) { return this.delegate.selectWith(predicate, parameter, targetCollection); } } @Override public RichIterable reject(Predicate predicate) { synchronized (this.lock) { return this.delegate.reject(predicate); } } @Override public

RichIterable rejectWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.rejectWith(predicate, parameter); } } @Override public > R reject(Predicate predicate, R target) { synchronized (this.lock) { return this.delegate.reject(predicate, target); } } @Override public > R rejectWith( Predicate2 predicate, P parameter, R targetCollection) { synchronized (this.lock) { return this.delegate.rejectWith(predicate, parameter, targetCollection); } } @Override public PartitionIterable partition(Predicate predicate) { synchronized (this.lock) { return this.delegate.partition(predicate); } } @Override public

PartitionIterable partitionWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.partitionWith(predicate, parameter); } } @Override public RichIterable selectInstancesOf(Class clazz) { synchronized (this.lock) { return this.delegate.selectInstancesOf(clazz); } } @Override public RichIterable collect(Function function) { synchronized (this.lock) { return this.delegate.collect(function); } } @Override public BooleanIterable collectBoolean(BooleanFunction booleanFunction) { synchronized (this.lock) { return this.delegate.collectBoolean(booleanFunction); } } @Override public R collectBoolean(BooleanFunction booleanFunction, R target) { synchronized (this.lock) { return this.delegate.collectBoolean(booleanFunction, target); } } @Override public R collectByte(ByteFunction byteFunction, R target) { synchronized (this.lock) { return this.delegate.collectByte(byteFunction, target); } } @Override public R collectChar(CharFunction charFunction, R target) { synchronized (this.lock) { return this.delegate.collectChar(charFunction, target); } } @Override public R collectDouble(DoubleFunction doubleFunction, R target) { synchronized (this.lock) { return this.delegate.collectDouble(doubleFunction, target); } } @Override public R collectFloat(FloatFunction floatFunction, R target) { synchronized (this.lock) { return this.delegate.collectFloat(floatFunction, target); } } @Override public R collectInt(IntFunction intFunction, R target) { synchronized (this.lock) { return this.delegate.collectInt(intFunction, target); } } @Override public R collectLong(LongFunction longFunction, R target) { synchronized (this.lock) { return this.delegate.collectLong(longFunction, target); } } @Override public R collectShort(ShortFunction shortFunction, R target) { synchronized (this.lock) { return this.delegate.collectShort(shortFunction, target); } } @Override public > R collect(Function function, R target) { synchronized (this.lock) { return this.delegate.collect(function, target); } } @Override public > R collectWith( Function2 function, P parameter, R targetCollection) { synchronized (this.lock) { return this.delegate.collectWith(function, parameter, targetCollection); } } @Override public > R collectIf( Predicate predicate, Function function, R target) { synchronized (this.lock) { return this.delegate.collectIf(predicate, function, target); } } @Override public > R flatCollect(Function> function, R target) { synchronized (this.lock) { return this.delegate.flatCollect(function, target); } } @Override public T detect(Predicate predicate) { synchronized (this.lock) { return this.delegate.detect(predicate); } } @Override public

T detectWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.detectWith(predicate, parameter); } } @Override public Optional detectOptional(Predicate predicate) { synchronized (this.lock) { return this.delegate.detectOptional(predicate); } } @Override public

Optional detectWithOptional(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.detectWithOptional(predicate, parameter); } } @Override public T detectIfNone(Predicate predicate, Function0 function) { synchronized (this.lock) { return this.delegate.detectIfNone(predicate, function); } } @Override public

T detectWithIfNone( Predicate2 predicate, P parameter, Function0 function) { synchronized (this.lock) { return this.delegate.detectWithIfNone(predicate, parameter, function); } } @Override public int count(Predicate predicate) { synchronized (this.lock) { return this.delegate.count(predicate); } } @Override public

int countWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.countWith(predicate, parameter); } } @Override public boolean anySatisfy(Predicate predicate) { synchronized (this.lock) { return this.delegate.anySatisfy(predicate); } } @Override public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.anySatisfyWith(predicate, parameter); } } @Override public boolean allSatisfy(Predicate predicate) { synchronized (this.lock) { return this.delegate.allSatisfy(predicate); } } @Override public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.allSatisfyWith(predicate, parameter); } } @Override public boolean noneSatisfy(Predicate predicate) { synchronized (this.lock) { return this.delegate.noneSatisfy(predicate); } } @Override public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { synchronized (this.lock) { return this.delegate.noneSatisfyWith(predicate, parameter); } } @Override public IV injectInto(IV injectedValue, Function2 function) { synchronized (this.lock) { return this.delegate.injectInto(injectedValue, function); } } @Override public int injectInto(int injectedValue, IntObjectToIntFunction function) { synchronized (this.lock) { return this.delegate.injectInto(injectedValue, function); } } @Override public long injectInto(long injectedValue, LongObjectToLongFunction function) { synchronized (this.lock) { return this.delegate.injectInto(injectedValue, function); } } @Override public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { synchronized (this.lock) { return this.delegate.injectInto(injectedValue, function); } } @Override public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { synchronized (this.lock) { return this.delegate.injectInto(injectedValue, function); } } @Override public > R into(R target) { synchronized (this.lock) { return this.delegate.into(target); } } @Override public MutableList toList() { synchronized (this.lock) { return this.delegate.toList(); } } @Override public MutableList toSortedList() { synchronized (this.lock) { return this.delegate.toSortedList(); } } @Override public MutableList toSortedList(Comparator comparator) { synchronized (this.lock) { return this.delegate.toSortedList(comparator); } } @Override public > MutableList toSortedListBy(Function function) { synchronized (this.lock) { return this.delegate.toSortedList(Comparators.byFunction(function)); } } @Override public MutableSet toSet() { synchronized (this.lock) { return this.delegate.toSet(); } } @Override public MutableSortedSet toSortedSet() { synchronized (this.lock) { return this.delegate.toSortedSet(); } } @Override public MutableSortedSet toSortedSet(Comparator comparator) { synchronized (this.lock) { return this.delegate.toSortedSet(comparator); } } @Override public > MutableSortedSet toSortedSetBy(Function function) { synchronized (this.lock) { return this.delegate.toSortedSetBy(function); } } @Override public MutableBag toBag() { synchronized (this.lock) { return this.delegate.toBag(); } } @Override public MutableSortedBag toSortedBag() { synchronized (this.lock) { return this.delegate.toSortedBag(); } } @Override public MutableSortedBag toSortedBag(Comparator comparator) { synchronized (this.lock) { return this.delegate.toSortedBag(comparator); } } @Override public > MutableSortedBag toSortedBagBy(Function function) { synchronized (this.lock) { return this.delegate.toSortedBag(Comparators.byFunction(function)); } } @Override public MutableMap toMap( Function keyFunction, Function valueFunction) { synchronized (this.lock) { return this.delegate.toMap(keyFunction, valueFunction); } } @Override public MutableSortedMap toSortedMap( Function keyFunction, Function valueFunction) { synchronized (this.lock) { return this.delegate.toSortedMap(keyFunction, valueFunction); } } @Override public MutableSortedMap toSortedMap(Comparator comparator, Function keyFunction, Function valueFunction) { synchronized (this.lock) { return this.delegate.toSortedMap(comparator, keyFunction, valueFunction); } } @Override public LazyIterable asLazy() { return LazyIterate.adapt(this); } @Override public Object[] toArray() { synchronized (this.lock) { return this.delegate.toArray(); } } @Override public T[] toArray(T[] a) { synchronized (this.lock) { return this.delegate.toArray(a); } } @Override public T min(Comparator comparator) { synchronized (this.lock) { return this.delegate.min(comparator); } } @Override public T max(Comparator comparator) { synchronized (this.lock) { return this.delegate.max(comparator); } } @Override public Optional minOptional(Comparator comparator) { synchronized (this.lock) { return this.delegate.minOptional(comparator); } } @Override public Optional maxOptional(Comparator comparator) { synchronized (this.lock) { return this.delegate.maxOptional(comparator); } } @Override public T min() { synchronized (this.lock) { return this.delegate.min(); } } @Override public T max() { synchronized (this.lock) { return this.delegate.max(); } } @Override public Optional minOptional() { synchronized (this.lock) { return this.delegate.minOptional(); } } @Override public Optional maxOptional() { synchronized (this.lock) { return this.delegate.maxOptional(); } } @Override public > T minBy(Function function) { synchronized (this.lock) { return this.delegate.minBy(function); } } @Override public > T maxBy(Function function) { synchronized (this.lock) { return this.delegate.maxBy(function); } } @Override public > Optional minByOptional(Function function) { synchronized (this.lock) { return this.delegate.minByOptional(function); } } @Override public > Optional maxByOptional(Function function) { synchronized (this.lock) { return this.delegate.maxByOptional(function); } } @Override public long sumOfInt(IntFunction function) { synchronized (this.lock) { return this.delegate.sumOfInt(function); } } @Override public double sumOfFloat(FloatFunction function) { synchronized (this.lock) { return this.delegate.sumOfFloat(function); } } @Override public long sumOfLong(LongFunction function) { synchronized (this.lock) { return this.delegate.sumOfLong(function); } } @Override public double sumOfDouble(DoubleFunction function) { synchronized (this.lock) { return this.delegate.sumOfDouble(function); } } @Override public String makeString() { synchronized (this.lock) { return this.delegate.makeString(); } } @Override public String makeString(String separator) { synchronized (this.lock) { return this.delegate.makeString(separator); } } @Override public String makeString(String start, String separator, String end) { synchronized (this.lock) { return this.delegate.makeString(start, separator, end); } } @Override public void appendString(Appendable appendable) { synchronized (this.lock) { this.delegate.appendString(appendable); } } @Override public void appendString(Appendable appendable, String separator) { synchronized (this.lock) { this.delegate.appendString(appendable, separator); } } @Override public void appendString(Appendable appendable, String start, String separator, String end) { synchronized (this.lock) { this.delegate.appendString(appendable, start, separator, end); } } /** * @since 9.0 */ @Override public Bag countBy(Function function) { synchronized (this.lock) { return this.getDelegate().countBy(function); } } /** * @since 9.0 */ @Override public > R countBy(Function function, R target) { synchronized (this.lock) { return this.delegate.countBy(function, target); } } /** * @since 9.0 */ @Override public Bag countByWith(Function2 function, P parameter) { synchronized (this.lock) { return this.delegate.countByWith(function, parameter); } } /** * @since 9.0 */ @Override public > R countByWith(Function2 function, P parameter, R target) { synchronized (this.lock) { return this.delegate.countByWith(function, parameter, target); } } @Override public > R groupBy( Function function, R target) { synchronized (this.lock) { return this.delegate.groupBy(function, target); } } @Override public > R groupByEach( Function> function, R target) { synchronized (this.lock) { return this.delegate.groupByEach(function, target); } } @Override public MapIterable groupByUniqueKey(Function function) { synchronized (this.lock) { return this.delegate.groupByUniqueKey(function, UnifiedMap.newMap()); } } @Override public > R groupByUniqueKey( Function function, R target) { synchronized (this.lock) { return this.delegate.groupByUniqueKey(function, target); } } @Override public >> R zip(Iterable that, R target) { synchronized (this.lock) { return this.delegate.zip(that, target); } } @Override public >> R zipWithIndex(R target) { synchronized (this.lock) { return this.delegate.zipWithIndex(target); } } @Override public RichIterable> chunk(int size) { synchronized (this.lock) { return this.delegate.chunk(size); } } @Override public T getOnly() { synchronized (this.lock) { return this.delegate.getOnly(); } } @Override public ByteIterable collectByte(ByteFunction byteFunction) { synchronized (this.lock) { return this.delegate.collectByte(byteFunction); } } @Override public CharIterable collectChar(CharFunction charFunction) { synchronized (this.lock) { return this.delegate.collectChar(charFunction); } } @Override public DoubleIterable collectDouble(DoubleFunction doubleFunction) { synchronized (this.lock) { return this.delegate.collectDouble(doubleFunction); } } @Override public FloatIterable collectFloat(FloatFunction floatFunction) { synchronized (this.lock) { return this.delegate.collectFloat(floatFunction); } } @Override public IntIterable collectInt(IntFunction intFunction) { synchronized (this.lock) { return this.delegate.collectInt(intFunction); } } @Override public LongIterable collectLong(LongFunction longFunction) { synchronized (this.lock) { return this.delegate.collectLong(longFunction); } } @Override public ShortIterable collectShort(ShortFunction shortFunction) { synchronized (this.lock) { return this.delegate.collectShort(shortFunction); } } @Override public RichIterable collectWith(Function2 function, P parameter) { synchronized (this.lock) { return this.delegate.collectWith(function, parameter); } } @Override public RichIterable collectIf(Predicate predicate, Function function) { synchronized (this.lock) { return this.delegate.collectIf(predicate, function); } } @Override public RichIterable flatCollect(Function> function) { synchronized (this.lock) { return this.delegate.flatCollect(function); } } @Override public ObjectLongMap sumByInt(Function groupBy, IntFunction function) { synchronized (this.lock) { return this.delegate.sumByInt(groupBy, function); } } @Override public ObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { synchronized (this.lock) { return this.delegate.sumByFloat(groupBy, function); } } @Override public ObjectLongMap sumByLong(Function groupBy, LongFunction function) { synchronized (this.lock) { return this.delegate.sumByLong(groupBy, function); } } @Override public ObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { synchronized (this.lock) { return this.delegate.sumByDouble(groupBy, function); } } @Override public Multimap groupBy(Function function) { synchronized (this.lock) { return this.delegate.groupBy(function); } } @Override public Multimap groupByEach(Function> function) { synchronized (this.lock) { return this.delegate.groupByEach(function); } } @Override public RichIterable> zip(Iterable that) { synchronized (this.lock) { return this.delegate.zip(that); } } @Override public RichIterable> zipWithIndex() { synchronized (this.lock) { return this.delegate.zipWithIndex(); } } @Override public MapIterable aggregateInPlaceBy(Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { synchronized (this.lock) { return this.delegate.aggregateInPlaceBy(groupBy, zeroValueFactory, mutatingAggregator); } } @Override public MapIterable aggregateBy(Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { synchronized (this.lock) { return this.delegate.aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator); } } @Override public RichIterable tap(Procedure procedure) { synchronized (this.lock) { this.forEach(procedure); return this; } } }