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

org.eclipse.collections.impl.collection.mutable.AbstractMultiReaderMutableCollection Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2020 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.mutable;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.stream.Stream;

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.MutableBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.bimap.MutableBiMap;
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.Function3;
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.MutableCollection;
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.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
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.api.tuple.Twin;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.factory.primitive.ObjectDoubleMaps;
import org.eclipse.collections.impl.factory.primitive.ObjectLongMaps;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;

/**
 * AbstractMultiReaderMutableCollection is a common abstraction that provides thread-safe collection behaviors.
 * Subclasses of this class must provide implementations of getDelegate() and getLock().
 */
@SuppressWarnings({"unused", "TransientFieldInNonSerializableClass"})
public abstract class AbstractMultiReaderMutableCollection implements MutableCollection
{
    protected transient ReadWriteLock lock;
    protected transient ReadWriteLockWrapper lockWrapper;

    protected abstract MutableCollection getDelegate();

    @Override
    public boolean contains(Object item)
    {
        try (LockWrapper wrapper = this.lockWrapper.acquireReadLock())
        {
            return this.getDelegate().contains(item);
        }
    }

    @Override
    public boolean containsAll(Collection collection)
    {
        try (LockWrapper wrapper = this.lockWrapper.acquireReadLock())
        {
            return this.getDelegate().containsAll(collection);
        }
    }

    @Override
    public boolean containsAllIterable(Iterable source)
    {
        try (LockWrapper wrapper = this.lockWrapper.acquireReadLock())
        {
            return this.getDelegate().containsAllIterable(source);
        }
    }

    @Override
    public boolean containsAllArguments(Object... elements)
    {
        try (LockWrapper wrapper = this.lockWrapper.acquireReadLock())
        {
            return this.getDelegate().containsAllArguments(elements);
        }
    }

    @Override
    public boolean noneSatisfy(Predicate predicate)
    {
        try (LockWrapper wrapper = this.lockWrapper.acquireReadLock())
        {
            return this.getDelegate().noneSatisfy(predicate);
        }
    }

    @Override
    public 

boolean noneSatisfyWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().noneSatisfyWith(predicate, parameter); } } @Override public boolean allSatisfy(Predicate predicate) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().allSatisfy(predicate); } } @Override public

boolean allSatisfyWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().allSatisfyWith(predicate, parameter); } } @Override public boolean anySatisfy(Predicate predicate) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().anySatisfy(predicate); } } @Override public

boolean anySatisfyWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().anySatisfyWith(predicate, parameter); } } @Override public > R into(R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().into(target); } } @Override public MutableList toList() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toList(); } } @Override public MutableMap toMap( Function keyFunction, Function valueFunction) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toMap(keyFunction, valueFunction); } } @Override public > R toMap( Function keyFunction, Function valueFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toMap(keyFunction, valueFunction, target); } } @Override public MutableSortedMap toSortedMap( Function keyFunction, Function valueFunction) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedMap(keyFunction, valueFunction); } } @Override public MutableSortedMap toSortedMap( Comparator comparator, Function keyFunction, Function valueFunction) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedMap(comparator, keyFunction, valueFunction); } } @Override public , NK, NV> MutableSortedMap toSortedMapBy( Function sortBy, Function keyFunction, Function valueFunction) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedMapBy(sortBy, keyFunction, valueFunction); } } @Override public MutableBiMap toBiMap( Function keyFunction, Function valueFunction) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toBiMap(keyFunction, valueFunction); } } @Override public LazyIterable asLazy() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().asLazy(); } } @Override public MutableSet toSet() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSet(); } } @Override public MutableBag toBag() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toBag(); } } @Override public MutableSortedBag toSortedBag() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedBag(); } } @Override public MutableSortedBag toSortedBag(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedBag(comparator); } } @Override public > MutableSortedBag toSortedBagBy( Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedBagBy(function); } } @Override public MutableList toSortedList() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedList(); } } @Override public MutableList toSortedList(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedList(comparator); } } @Override public > MutableList toSortedListBy( Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedListBy(function); } } @Override public MutableSortedSet toSortedSet() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedSet(); } } @Override public MutableSortedSet toSortedSet(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedSet(comparator); } } @Override public > MutableSortedSet toSortedSetBy( Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toSortedSetBy(function); } } @Override public int count(Predicate predicate) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().count(predicate); } } @Override public

int countWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().countWith(predicate, parameter); } } @Override public T detect(Predicate predicate) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().detect(predicate); } } @Override public

T detectWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().detectWith(predicate, parameter); } } @Override public Optional detectOptional(Predicate predicate) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().detectOptional(predicate); } } @Override public

Optional detectWithOptional( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().detectWithOptional(predicate, parameter); } } @Override public T detectIfNone( Predicate predicate, Function0 function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().detectIfNone(predicate, function); } } @Override public

T detectWithIfNone( Predicate2 predicate, P parameter, Function0 function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().detectWithIfNone(predicate, parameter, function); } } @Override public T min(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().min(comparator); } } @Override public T max(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().max(comparator); } } @Override public Optional minOptional(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().minOptional(comparator); } } @Override public Optional maxOptional(Comparator comparator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().maxOptional(comparator); } } @Override public T min() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().min(); } } @Override public T max() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().max(); } } @Override public Optional minOptional() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().minOptional(); } } @Override public Optional maxOptional() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().maxOptional(); } } @Override public > T minBy(Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().minBy(function); } } @Override public > T maxBy(Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().maxBy(function); } } @Override public > Optional minByOptional(Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().minByOptional(function); } } @Override public > Optional maxByOptional(Function function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().maxByOptional(function); } } @Override public T getFirst() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().getFirst(); } } @Override public T getLast() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().getLast(); } } @Override public T getOnly() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().getOnly(); } } @Override public boolean notEmpty() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().notEmpty(); } } @Override public

Twin> selectAndRejectWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().selectAndRejectWith(predicate, parameter); } } @Override public > R collect( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collect(function, target); } } @Override public R collectBoolean(BooleanFunction booleanFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectBoolean(booleanFunction, target); } } @Override public R flatCollectBoolean( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectBoolean(function, target); } } @Override public R collectByte(ByteFunction byteFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectByte(byteFunction, target); } } @Override public R flatCollectByte( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectByte(function, target); } } @Override public R collectChar(CharFunction charFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectChar(charFunction, target); } } @Override public R flatCollectChar( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectChar(function, target); } } @Override public R collectDouble(DoubleFunction doubleFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectDouble(doubleFunction, target); } } @Override public R flatCollectDouble( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectDouble(function, target); } } @Override public R collectFloat(FloatFunction floatFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectFloat(floatFunction, target); } } @Override public R flatCollectFloat( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectFloat(function, target); } } @Override public R collectInt(IntFunction intFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectInt(intFunction, target); } } @Override public R flatCollectInt( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectInt(function, target); } } @Override public R collectLong(LongFunction longFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectLong(longFunction, target); } } @Override public R flatCollectLong( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectLong(function, target); } } @Override public R collectShort(ShortFunction shortFunction, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectShort(shortFunction, target); } } @Override public R flatCollectShort( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollectShort(function, target); } } @Override public > R flatCollect( Function> function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().flatCollect(function, target); } } @Override public > R collectIf( Predicate predicate, Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectIf(predicate, function, target); } } @Override public > R collectWith( Function2 function, P parameter, R targetCollection) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().collectWith(function, parameter, targetCollection); } } @Override public > R selectWith( Predicate2 predicate, P parameter, R targetCollection) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().selectWith(predicate, parameter, targetCollection); } } @Override public > R reject( Predicate predicate, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().reject(predicate, target); } } @Override public > R rejectWith( Predicate2 predicate, P parameter, R targetCollection) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().rejectWith(predicate, parameter, targetCollection); } } @Override public > R select(Predicate predicate, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().select(predicate, target); } } @Override public IV injectInto( IV injectedValue, Function2 function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().injectInto(injectedValue, function); } } @Override public int injectInto(int injectedValue, IntObjectToIntFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().injectInto(injectedValue, function); } } @Override public long injectInto(long injectedValue, LongObjectToLongFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().injectInto(injectedValue, function); } } @Override public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().injectInto(injectedValue, function); } } @Override public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().injectInto(injectedValue, function); } } @Override public long sumOfInt(IntFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().sumOfInt(function); } } @Override public double sumOfFloat(FloatFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().sumOfFloat(function); } } @Override public long sumOfLong(LongFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().sumOfLong(function); } } @Override public double sumOfDouble(DoubleFunction function) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().sumOfDouble(function); } } @Override public MutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { MutableObjectLongMap result = ObjectLongMaps.mutable.empty(); return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function)); } @Override public MutableObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { MutableObjectDoubleMap result = ObjectDoubleMaps.mutable.empty(); return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function)); } @Override public MutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { MutableObjectLongMap result = ObjectLongMaps.mutable.empty(); return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function)); } @Override public MutableObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { MutableObjectDoubleMap result = ObjectDoubleMaps.mutable.empty(); return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function)); } @Override public IV injectIntoWith( IV injectValue, Function3 function, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().injectIntoWith(injectValue, function, parameter); } } @Override public boolean removeIf(Predicate predicate) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().removeIf(predicate); } } @Override public

boolean removeIfWith( Predicate2 predicate, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().removeIfWith(predicate, parameter); } } @Override public boolean add(T item) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().add(item); } } @Override public boolean addAll(Collection collection) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().addAll(collection); } } @Override public boolean addAllIterable(Iterable iterable) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().addAllIterable(iterable); } } @Override public void clear() { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { this.getDelegate().clear(); } } @Override public boolean isEmpty() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().isEmpty(); } } /** * This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like * to use an iterator with a MultiReader collection, then you must do the following: * *

     * multiReaderList.withReadLockAndDelegate(MutableList<Person> ->
     *     {
     *         Iterator it = people.iterator();
     *         ....
     *     });
     * 
* *
     * final Collection jdkSet = new HashSet();
     * final boolean containsAll = new boolean[1];
     * multiReaderList.withReadLockAndDelegate(MutableList<Person> people ->
     *     {
     *         set.addAll(people); // addAll uses iterator() in AbstractCollection
     *         containsAll[0] = set.containsAll(people); // containsAll uses iterator() in AbstractCollection
     *     });
     * 
*/ @Override public Iterator iterator() { throw new UnsupportedOperationException( "Iterator is not supported directly on MultiReader collections. " + "If you would like to use an iterator, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate()."); } /** * This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like * to use a spliterator with a MultiReader collection, then you must protect the calls by calling either * withReadLockAndDelegate or withWriteLockAndDelegate. */ @Override public Spliterator spliterator() { throw new UnsupportedOperationException( "Spliterator is not supported directly on MultiReader collections. " + "If you would like to use an spliterator, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate()."); } /** * This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like * to use stream with a MultiReader collection, then you must protect the calls by calling either * withReadLockAndDelegate or withWriteLockAndDelegate. */ @Override public Stream stream() { throw new UnsupportedOperationException( "Stream is not supported directly on MultiReader collections. " + "If you would like to use stream, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate()."); } /** * This method is not supported directly on MultiReader collections because it is not thread-safe. If you would like * to use parallelStream with a MultiReader collection, then you must protect the calls by calling either * withReadLockAndDelegate or withWriteLockAndDelegate. */ @Override public Stream parallelStream() { throw new UnsupportedOperationException( "parallelStream is not supported directly on MultiReader collections. " + "If you would like to use parallelStream, you must either use withReadLockAndDelegate() or withWriteLockAndDelegate()."); } @Override public boolean remove(Object item) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().remove(item); } } @Override public boolean removeAll(Collection collection) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().removeAll(collection); } } @Override public boolean removeAllIterable(Iterable iterable) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().removeAllIterable(iterable); } } @Override public boolean retainAll(Collection collection) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().retainAll(collection); } } @Override public boolean retainAllIterable(Iterable iterable) { try (LockWrapper wrapper = this.lockWrapper.acquireWriteLock()) { return this.getDelegate().retainAllIterable(iterable); } } @Override public int size() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().size(); } } @Override public Object[] toArray() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toArray(); } } @Override public E[] toArray(E[] a) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toArray(a); } } @Override public void each(Procedure procedure) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { this.getDelegate().forEach(procedure); } } @Override public

void forEachWith(Procedure2 procedure, P parameter) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { this.getDelegate().forEachWith(procedure, parameter); } } @Override public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { this.getDelegate().forEachWithIndex(objectIntProcedure); } } @Override public String toString() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().toString(); } } @Override public String makeString() { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().makeString(); } } @Override public String makeString(String separator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().makeString(separator); } } @Override public String makeString(String start, String separator, String end) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().makeString(start, separator, end); } } @Override public void appendString(Appendable appendable) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { this.getDelegate().appendString(appendable); } } @Override public void appendString(Appendable appendable, String separator) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { this.getDelegate().appendString(appendable, separator); } } @Override public void appendString(Appendable appendable, String start, String separator, String end) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { this.getDelegate().appendString(appendable, start, separator, end); } } @Override public > R groupBy( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().groupBy(function, target); } } @Override public > R groupByEach( Function> function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().groupByEach(function, target); } } @Override public > R groupByUniqueKey( Function function, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().groupByUniqueKey(function, target); } } @Override public >> R zip(Iterable that, R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().zip(that, target); } } @Override public >> R zipWithIndex(R target) { try (LockWrapper wrapper = this.lockWrapper.acquireReadLock()) { return this.getDelegate().zipWithIndex(target); } } @Override public MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } protected abstract static class UntouchableMutableCollection implements MutableCollection { protected MutableCollection delegate; @Override public boolean allSatisfy(Predicate predicate) { return this.delegate.allSatisfy(predicate); } @Override public

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

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

boolean anySatisfyWith( Predicate2 predicate, P parameter) { return this.delegate.anySatisfyWith(predicate, parameter); } @Override public > R into(R target) { return this.delegate.into(target); } @Override public MutableList toList() { return this.delegate.toList(); } @Override public MutableMap toMap( Function keyFunction, Function valueFunction) { return this.delegate.toMap(keyFunction, valueFunction); } @Override public > R toMap( Function keyFunction, Function valueFunction, R target) { return this.delegate.toMap(keyFunction, valueFunction, target); } @Override public MutableSortedMap toSortedMap( Function keyFunction, Function valueFunction) { return this.delegate.toSortedMap(keyFunction, valueFunction); } @Override public MutableSortedMap toSortedMap( Comparator comparator, Function keyFunction, Function valueFunction) { return this.delegate.toSortedMap(comparator, keyFunction, valueFunction); } @Override public , NK, NV> MutableSortedMap toSortedMapBy( Function sortBy, Function keyFunction, Function valueFunction) { return this.delegate.toSortedMapBy(sortBy, keyFunction, valueFunction); } @Override public MutableBiMap toBiMap( Function keyFunction, Function valueFunction) { return this.delegate.toBiMap(keyFunction, valueFunction); } @Override public MutableSet toSet() { return this.delegate.toSet(); } @Override public MutableBag toBag() { return this.delegate.toBag(); } @Override public MutableSortedBag toSortedBag() { return this.delegate.toSortedBag(); } @Override public MutableSortedBag toSortedBag(Comparator comparator) { return this.delegate.toSortedBag(comparator); } @Override public > MutableSortedBag toSortedBagBy(Function function) { return this.delegate.toSortedBagBy(function); } @Override public MutableList toSortedList() { return this.delegate.toSortedList(); } @Override public MutableList toSortedList(Comparator comparator) { return this.delegate.toSortedList(comparator); } @Override public > MutableList toSortedListBy(Function function) { return this.delegate.toSortedListBy(function); } @Override public MutableSortedSet toSortedSet() { return this.delegate.toSortedSet(); } @Override public MutableSortedSet toSortedSet(Comparator comparator) { return this.delegate.toSortedSet(comparator); } @Override public > MutableSortedSet toSortedSetBy(Function function) { return this.delegate.toSortedSetBy(function); } @Override public > R collect( Function function, R target) { return this.delegate.collect(function, target); } @Override public > R flatCollect( Function> function, R target) { return this.delegate.flatCollect(function, target); } @Override public > R collectIf( Predicate predicate, Function function, R target) { return this.delegate.collectIf(predicate, function, target); } @Override public > R collectWith( Function2 function, P parameter, R targetCollection) { return this.delegate.collectWith(function, parameter, targetCollection); } @Override public > R groupBy( Function function, R target) { return this.delegate.groupBy(function, target); } @Override public > R groupByEach( Function> function, R target) { return this.delegate.groupByEach(function, target); } @Override public > R groupByUniqueKey( Function function, R target) { return this.delegate.groupByUniqueKey(function, target); } @Override public int count(Predicate predicate) { return this.delegate.count(predicate); } @Override public

int countWith(Predicate2 predicate, P parameter) { return this.delegate.countWith(predicate, parameter); } @Override public T detect(Predicate predicate) { return this.delegate.detect(predicate); } @Override public

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

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

T detectWithIfNone( Predicate2 predicate, P parameter, Function0 function) { return this.delegate.detectWithIfNone(predicate, parameter, function); } @Override public T min(Comparator comparator) { return this.delegate.min(comparator); } @Override public T max(Comparator comparator) { return this.delegate.max(comparator); } @Override public T min() { return this.delegate.min(); } @Override public T max() { return this.delegate.max(); } @Override public > T minBy(Function function) { return this.delegate.minBy(function); } @Override public > T maxBy(Function function) { return this.delegate.maxBy(function); } @Override public T getFirst() { return this.delegate.getFirst(); } @Override public T getLast() { return this.delegate.getLast(); } @Override public T getOnly() { return this.delegate.getOnly(); } @Override public IV injectInto( IV injectedValue, Function2 function) { return this.delegate.injectInto(injectedValue, function); } @Override public int injectInto(int injectedValue, IntObjectToIntFunction function) { return this.delegate.injectInto(injectedValue, function); } @Override public long injectInto(long injectedValue, LongObjectToLongFunction function) { return this.delegate.injectInto(injectedValue, function); } @Override public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { return this.delegate.injectInto(injectedValue, function); } @Override public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { return this.delegate.injectInto(injectedValue, function); } @Override public long sumOfInt(IntFunction function) { return this.delegate.sumOfInt(function); } @Override public double sumOfFloat(FloatFunction function) { return this.delegate.sumOfFloat(function); } @Override public long sumOfLong(LongFunction function) { return this.delegate.sumOfLong(function); } @Override public double sumOfDouble(DoubleFunction function) { return this.delegate.sumOfDouble(function); } @Override public MutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { return this.delegate.sumByInt(groupBy, function); } @Override public MutableObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { return this.delegate.sumByFloat(groupBy, function); } @Override public MutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { return this.delegate.sumByLong(groupBy, function); } @Override public MutableObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { return this.delegate.sumByDouble(groupBy, function); } @Override public IV injectIntoWith( IV injectValue, Function3 function, P parameter) { return this.delegate.injectIntoWith(injectValue, function, parameter); } @Override public boolean notEmpty() { return this.delegate.notEmpty(); } @Override public > R reject(Predicate predicate, R target) { return this.delegate.reject(predicate, target); } @Override public > R rejectWith( Predicate2 predicate, P parameter, R targetCollection) { return this.delegate.rejectWith(predicate, parameter, targetCollection); } @Override public boolean removeIf(Predicate predicate) { return this.delegate.removeIf(predicate); } @Override public

boolean removeIfWith( Predicate2 predicate, P parameter) { return this.delegate.removeIfWith(predicate, parameter); } @Override public > R select(Predicate predicate, R target) { return this.delegate.select(predicate, target); } @Override public

Twin> selectAndRejectWith( Predicate2 predicate, P parameter) { return this.delegate.selectAndRejectWith(predicate, parameter); } @Override public > R selectWith( Predicate2 predicate, P parameter, R targetCollection) { return this.delegate.selectWith(predicate, parameter, targetCollection); } @Override public boolean add(T o) { return this.delegate.add(o); } @Override public boolean addAll(Collection collection) { return this.delegate.addAll(collection); } @Override public boolean addAllIterable(Iterable iterable) { return this.delegate.addAllIterable(iterable); } @Override public void clear() { this.delegate.clear(); } @Override public boolean contains(Object o) { return this.delegate.contains(o); } @Override public boolean containsAll(Collection collection) { return this.delegate.containsAll(collection); } @Override public boolean containsAllIterable(Iterable source) { return this.delegate.containsAllIterable(source); } @Override public boolean containsAllArguments(Object... elements) { return this.delegate.containsAllArguments(elements); } @Override public boolean equals(Object o) { return this.delegate.equals(o); } @Override public int hashCode() { return this.delegate.hashCode(); } @Override public boolean isEmpty() { return this.delegate.isEmpty(); } @Override public boolean remove(Object o) { return this.delegate.remove(o); } @Override public boolean removeAll(Collection collection) { return this.delegate.removeAll(collection); } @Override public boolean removeAllIterable(Iterable iterable) { return this.delegate.removeAllIterable(iterable); } @Override public boolean retainAll(Collection collection) { return this.delegate.retainAll(collection); } @Override public boolean retainAllIterable(Iterable iterable) { return this.delegate.retainAllIterable(iterable); } @Override public int size() { return this.delegate.size(); } @Override public Object[] toArray() { return this.delegate.toArray(); } @Override public E[] toArray(E[] a) { return this.delegate.toArray(a); } @Override public void each(Procedure procedure) { this.delegate.forEach(procedure); } @Override public

void forEachWith(Procedure2 procedure, P parameter) { this.delegate.forEachWith(procedure, parameter); } @Override public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) { this.delegate.forEachWithIndex(objectIntProcedure); } @Override public String toString() { return this.delegate.toString(); } @Override public String makeString() { return this.delegate.makeString(); } @Override public String makeString(String separator) { return this.delegate.makeString(separator); } @Override public String makeString(String start, String separator, String end) { return this.delegate.makeString(start, separator, end); } @Override public void appendString(Appendable appendable) { this.delegate.appendString(appendable); } @Override public void appendString(Appendable appendable, String separator) { this.delegate.appendString(appendable, separator); } @Override public void appendString(Appendable appendable, String start, String separator, String end) { this.delegate.appendString(appendable, start, separator, end); } @Override public >> R zip(Iterable that, R target) { return this.delegate.zip(that, target); } @Override public >> R zipWithIndex(R target) { return this.delegate.zipWithIndex(target); } @Override public RichIterable> chunk(int size) { return this.delegate.chunk(size); } @Override public MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.each(each -> { K key = groupBy.valueOf(each); V value = map.getIfAbsentPut(key, zeroValueFactory); mutatingAggregator.value(value, each); }); return map; } } public static class ReadWriteLockWrapper { private final LockWrapper readLock; private final LockWrapper writeLock; public ReadWriteLockWrapper(ReadWriteLock readWriteLock) { this.readLock = new LockWrapper(readWriteLock.readLock()); this.writeLock = new LockWrapper(readWriteLock.writeLock()); } /** * This method must be wrapped in a try block. */ public LockWrapper acquireReadLock() { this.readLock.lock.lock(); return this.readLock; } /** * This method must be wrapped in a try block. */ public LockWrapper acquireWriteLock() { this.writeLock.lock.lock(); return this.writeLock; } } public static class LockWrapper implements AutoCloseable { private final Lock lock; public LockWrapper(Lock lock) { this.lock = lock; } @Override public void close() { this.lock.unlock(); } } }