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

org.eclipse.collections.impl.bag.mutable.MultiReaderHashBag Maven / Gradle / Ivy

There is a newer version: 9.2.0.M1
Show newest version
/*
 * Copyright (c) 2016 Goldman Sachs.
 * 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.bag.mutable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Iterator;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.Function;
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.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
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.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
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.MutableMap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.collection.mutable.AbstractMultiReaderMutableCollection;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Iterables;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
 * MultiReaderHashBag provides a thread-safe wrapper around a HashBag, using a ReentrantReadWriteLock. In order to
 * provide true thread-safety, MultiReaderHashBag does not implement iterator() as this method requires an external lock
 * to be taken to provide thread-safe iteration. All of these methods are available however, if you use the
 * withReadLockAndDelegate() or withWriteLockAndDelegate() methods. Both of these methods take a parameter of type
 * Procedure, and a wrapped version of the underlying HashBag is returned. This wrapper guarantees that
 * no external pointer can ever reference the underlying HashBag outside of a locked procedure. In the case of the
 * read lock method, an Unmodifiable version of the collection is offered, which will throw UnsupportedOperationExceptions
 * on any write methods like add or remove.
 */
public final class MultiReaderHashBag
        extends AbstractMultiReaderMutableCollection
        implements Externalizable, MutableBag
{
    private static final long serialVersionUID = 1L;

    private transient ReadWriteLock lock;
    private MutableBag delegate;

    /**
     * @deprecated Empty default constructor used for serialization.
     */
    @SuppressWarnings("UnusedDeclaration")
    @Deprecated
    public MultiReaderHashBag()
    {
        // For Externalizable use only
    }

    private MultiReaderHashBag(MutableBag newDelegate)
    {
        this(newDelegate, new ReentrantReadWriteLock());
    }

    private MultiReaderHashBag(MutableBag newDelegate, ReadWriteLock newLock)
    {
        this.lock = newLock;
        this.delegate = newDelegate;
    }

    public static  MultiReaderHashBag newBag()
    {
        return new MultiReaderHashBag<>(HashBag.newBag());
    }

    public static  MultiReaderHashBag newBag(int capacity)
    {
        return new MultiReaderHashBag<>(HashBag.newBag(capacity));
    }

    public static  MultiReaderHashBag newBag(Iterable iterable)
    {
        return new MultiReaderHashBag<>(HashBag.newBag(iterable));
    }

    public static  MultiReaderHashBag newBagWith(T... elements)
    {
        return new MultiReaderHashBag<>(HashBag.newBagWith(elements));
    }

    @Override
    protected MutableBag getDelegate()
    {
        return this.delegate;
    }

    @Override
    protected ReadWriteLock getLock()
    {
        return this.lock;
    }

    UntouchableMutableBag asReadUntouchable()
    {
        return new UntouchableMutableBag<>(this.delegate.asUnmodifiable());
    }

    UntouchableMutableBag asWriteUntouchable()
    {
        return new UntouchableMutableBag<>(this.delegate);
    }

    public void withReadLockAndDelegate(Procedure> procedure)
    {
        this.acquireReadLock();
        try
        {
            UntouchableMutableBag bag = this.asReadUntouchable();
            procedure.value(bag);
            bag.becomeUseless();
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    public void withWriteLockAndDelegate(Procedure> procedure)
    {
        this.acquireWriteLock();
        try
        {
            UntouchableMutableBag bag = this.asWriteUntouchable();
            procedure.value(bag);
            bag.becomeUseless();
        }
        finally
        {
            this.unlockWriteLock();
        }
    }

    @Override
    public MutableBag asSynchronized()
    {
        this.acquireReadLock();
        try
        {
            return SynchronizedBag.of(this);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableBag asUnmodifiable()
    {
        this.acquireReadLock();
        try
        {
            return UnmodifiableBag.of(this);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public ImmutableBag toImmutable()
    {
        this.acquireReadLock();
        try
        {
            return Bags.immutable.withAll(this.delegate);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public int addOccurrences(T item, int occurrences)
    {
        this.acquireWriteLock();
        try
        {
            return this.delegate.addOccurrences(item, occurrences);
        }
        finally
        {
            this.unlockWriteLock();
        }
    }

    @Override
    public boolean removeOccurrences(Object item, int occurrences)
    {
        this.acquireWriteLock();
        try
        {
            return this.delegate.removeOccurrences(item, occurrences);
        }
        finally
        {
            this.unlockWriteLock();
        }
    }

    @Override
    public boolean setOccurrences(T item, int occurrences)
    {
        this.acquireWriteLock();
        try
        {
            return this.delegate.setOccurrences(item, occurrences);
        }
        finally
        {
            this.unlockWriteLock();
        }
    }

    @Override
    public int occurrencesOf(Object item)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.occurrencesOf(item);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public int sizeDistinct()
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.sizeDistinct();
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public  MutableBag collect(Function function)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collect(function);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableBooleanBag collectBoolean(BooleanFunction booleanFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectBoolean(booleanFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableByteBag collectByte(ByteFunction byteFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectByte(byteFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableCharBag collectChar(CharFunction charFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectChar(charFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableDoubleBag collectDouble(DoubleFunction doubleFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectDouble(doubleFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableFloatBag collectFloat(FloatFunction floatFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectFloat(floatFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableIntBag collectInt(IntFunction intFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectInt(intFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableLongBag collectLong(LongFunction longFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectLong(longFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableShortBag collectShort(ShortFunction shortFunction)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectShort(shortFunction);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public  MutableBag flatCollect(
            Function> function)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.flatCollect(function);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableList> topOccurrences(int count)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.topOccurrences(count);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableList> bottomOccurrences(int count)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.bottomOccurrences(count);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public  MutableBag collectIf(
            Predicate predicate,
            Function function)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectIf(predicate, function);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public  MutableBag collectWith(
            Function2 function,
            P parameter)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.collectWith(function, parameter);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public MutableBag newEmpty()
    {
        return MultiReaderHashBag.newBag();
    }

    @Override
    public MutableBag reject(Predicate predicate)
    {
        this.acquireReadLock();
        try
        {
            return this.delegate.reject(predicate);
        }
        finally
        {
            this.unlockReadLock();
        }
    }

    @Override
    public 

MutableBag rejectWith( Predicate2 predicate, P parameter) { this.acquireReadLock(); try { return this.delegate.rejectWith(predicate, parameter); } finally { this.unlockReadLock(); } } @Override public MutableBag tap(Procedure procedure) { this.acquireReadLock(); try { this.forEach(procedure); return this; } finally { this.unlockReadLock(); } } @Override public MutableBag select(Predicate predicate) { this.acquireReadLock(); try { return this.delegate.select(predicate); } finally { this.unlockReadLock(); } } @Override public

MutableBag selectWith( Predicate2 predicate, P parameter) { this.acquireReadLock(); try { return this.delegate.selectWith(predicate, parameter); } finally { this.unlockReadLock(); } } @Override public MutableBag selectByOccurrences(IntPredicate predicate) { this.acquireReadLock(); try { return this.delegate.selectByOccurrences(predicate); } finally { this.unlockReadLock(); } } @Override public MutableBag selectInstancesOf(Class clazz) { this.acquireReadLock(); try { return this.delegate.selectInstancesOf(clazz); } finally { this.unlockReadLock(); } } @Override public PartitionMutableBag partition(Predicate predicate) { this.acquireReadLock(); try { return this.delegate.partition(predicate); } finally { this.unlockReadLock(); } } @Override public

PartitionMutableBag partitionWith(Predicate2 predicate, P parameter) { this.acquireReadLock(); try { return this.delegate.partitionWith(predicate, parameter); } finally { this.unlockReadLock(); } } @Override public MutableBag with(T element) { this.add(element); return this; } @Override public MutableBag without(T element) { this.remove(element); return this; } @Override public MutableBag withAll(Iterable elements) { this.addAllIterable(elements); return this; } @Override public MutableBag withoutAll(Iterable elements) { this.removeAllIterable(elements); return this; } @Override public MutableMap toMapOfItemToCount() { this.acquireReadLock(); try { return this.delegate.toMapOfItemToCount(); } finally { this.unlockReadLock(); } } @Override public String toStringOfItemToCount() { this.acquireReadLock(); try { return this.delegate.toStringOfItemToCount(); } finally { this.unlockReadLock(); } } @Override public MutableBagMultimap groupBy(Function function) { this.acquireReadLock(); try { return this.delegate.groupBy(function); } finally { this.unlockReadLock(); } } @Override public MutableBagMultimap groupByEach(Function> function) { this.acquireReadLock(); try { return this.delegate.groupByEach(function); } finally { this.unlockReadLock(); } } @Override public MutableMap groupByUniqueKey(Function function) { this.acquireReadLock(); try { return this.delegate.groupByUniqueKey(function); } finally { this.unlockReadLock(); } } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public MutableBag> zip(Iterable that) { this.acquireReadLock(); try { return this.delegate.zip(that); } finally { this.unlockReadLock(); } } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public MutableSet> zipWithIndex() { this.acquireReadLock(); try { return this.delegate.zipWithIndex(); } finally { this.unlockReadLock(); } } @Override public RichIterable> chunk(int size) { this.acquireReadLock(); try { return this.delegate.chunk(size); } finally { this.unlockReadLock(); } } @Override public void forEachWithOccurrences(ObjectIntProcedure procedure) { this.acquireReadLock(); try { this.delegate.forEachWithOccurrences(procedure); } finally { this.unlockReadLock(); } } @Override public boolean equals(Object o) { this.acquireReadLock(); try { return this.delegate.equals(o); } finally { this.unlockReadLock(); } } @Override public int hashCode() { this.acquireReadLock(); try { return this.delegate.hashCode(); } finally { this.unlockReadLock(); } } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(this.delegate); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.delegate = (MutableBag) in.readObject(); this.lock = new ReentrantReadWriteLock(); } //Exposed for testing static final class UntouchableMutableBag extends UntouchableMutableCollection implements MutableBag { private final MutableList> requestedIterators = Iterables.mList(); private UntouchableMutableBag(MutableBag newDelegate) { this.delegate = newDelegate; } public void becomeUseless() { this.delegate = null; this.requestedIterators.each(UntouchableIterator::becomeUseless); } @Override public MutableBag with(T element) { this.add(element); return this; } @Override public MutableBag without(T element) { this.remove(element); return this; } @Override public MutableBag withAll(Iterable elements) { this.addAllIterable(elements); return this; } @Override public MutableBag withoutAll(Iterable elements) { this.removeAllIterable(elements); return this; } @Override public MutableBag asSynchronized() { throw new UnsupportedOperationException("cannot wrap an UntouchableMutableBag"); } @Override public MutableBag asUnmodifiable() { throw new UnsupportedOperationException("cannot wrap an UntouchableMutableBag"); } @Override public ImmutableBag toImmutable() { return Bags.immutable.withAll(this.getDelegate()); } @Override public LazyIterable asLazy() { return LazyIterate.adapt(this); } @Override public Iterator iterator() { UntouchableIterator iterator = new UntouchableIterator<>(this.delegate.iterator()); this.requestedIterators.add(iterator); return iterator; } @Override public int addOccurrences(T item, int occurrences) { return this.getDelegate().addOccurrences(item, occurrences); } @Override public boolean removeOccurrences(Object item, int occurrences) { return this.getDelegate().removeOccurrences(item, occurrences); } @Override public boolean setOccurrences(T item, int occurrences) { return this.getDelegate().setOccurrences(item, occurrences); } @Override public int occurrencesOf(Object item) { return this.getDelegate().occurrencesOf(item); } @Override public int sizeDistinct() { return this.getDelegate().sizeDistinct(); } @Override public MutableBag collect(Function function) { return this.getDelegate().collect(function); } @Override public MutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return this.getDelegate().collectBoolean(booleanFunction); } @Override public R collectBoolean(BooleanFunction booleanFunction, R target) { return this.getDelegate().collectBoolean(booleanFunction, target); } @Override public MutableByteBag collectByte(ByteFunction byteFunction) { return this.getDelegate().collectByte(byteFunction); } @Override public R collectByte(ByteFunction byteFunction, R target) { return this.getDelegate().collectByte(byteFunction, target); } @Override public MutableCharBag collectChar(CharFunction charFunction) { return this.getDelegate().collectChar(charFunction); } @Override public R collectChar(CharFunction charFunction, R target) { return this.getDelegate().collectChar(charFunction, target); } @Override public MutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return this.getDelegate().collectDouble(doubleFunction); } @Override public R collectDouble(DoubleFunction doubleFunction, R target) { return this.getDelegate().collectDouble(doubleFunction, target); } @Override public MutableFloatBag collectFloat(FloatFunction floatFunction) { return this.getDelegate().collectFloat(floatFunction); } @Override public R collectFloat(FloatFunction floatFunction, R target) { return this.getDelegate().collectFloat(floatFunction, target); } @Override public MutableIntBag collectInt(IntFunction intFunction) { return this.getDelegate().collectInt(intFunction); } @Override public R collectInt(IntFunction intFunction, R target) { return this.getDelegate().collectInt(intFunction, target); } @Override public MutableLongBag collectLong(LongFunction longFunction) { return this.getDelegate().collectLong(longFunction); } @Override public R collectLong(LongFunction longFunction, R target) { return this.getDelegate().collectLong(longFunction, target); } @Override public MutableShortBag collectShort(ShortFunction shortFunction) { return this.getDelegate().collectShort(shortFunction); } @Override public R collectShort(ShortFunction shortFunction, R target) { return this.getDelegate().collectShort(shortFunction, target); } @Override public MutableBag flatCollect(Function> function) { return this.getDelegate().flatCollect(function); } @Override public MutableList> topOccurrences(int count) { return this.getDelegate().topOccurrences(count); } @Override public MutableList> bottomOccurrences(int count) { return this.getDelegate().bottomOccurrences(count); } @Override public MutableBag collectIf( Predicate predicate, Function function) { return this.getDelegate().collectIf(predicate, function); } @Override public MutableBag collectWith( Function2 function, P parameter) { return this.getDelegate().collectWith(function, parameter); } @Override public MutableBagMultimap groupBy(Function function) { return this.getDelegate().groupBy(function); } @Override public MutableBagMultimap groupByEach(Function> function) { return this.getDelegate().groupByEach(function); } @Override public MutableMap groupByUniqueKey(Function function) { return this.getDelegate().groupByUniqueKey(function); } @Override public MutableBag newEmpty() { return this.getDelegate().newEmpty(); } @Override public MutableBag reject(Predicate predicate) { return this.getDelegate().reject(predicate); } @Override public

MutableBag rejectWith( Predicate2 predicate, P parameter) { return this.getDelegate().rejectWith(predicate, parameter); } @Override public MutableBag tap(Procedure procedure) { this.forEach(procedure); return this; } @Override public MutableBag select(Predicate predicate) { return this.getDelegate().select(predicate); } @Override public

MutableBag selectWith( Predicate2 predicate, P parameter) { return this.getDelegate().selectWith(predicate, parameter); } @Override public MutableBag selectByOccurrences(IntPredicate predicate) { return this.getDelegate().selectByOccurrences(predicate); } @Override public MutableBag selectInstancesOf(Class clazz) { return this.getDelegate().selectInstancesOf(clazz); } @Override public void forEachWithOccurrences(ObjectIntProcedure procedure) { this.getDelegate().forEachWithOccurrences(procedure); } @Override public PartitionMutableBag partition(Predicate predicate) { return this.getDelegate().partition(predicate); } @Override public

PartitionMutableBag partitionWith(Predicate2 predicate, P parameter) { return this.getDelegate().partitionWith(predicate, parameter); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public MutableBag> zip(Iterable that) { return this.getDelegate().zip(that); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public MutableSet> zipWithIndex() { return this.getDelegate().zipWithIndex(); } @Override public MutableMap toMapOfItemToCount() { return this.getDelegate().toMapOfItemToCount(); } @Override public String toStringOfItemToCount() { return this.getDelegate().toStringOfItemToCount(); } private MutableBag getDelegate() { return (MutableBag) this.delegate; } } private static final class UntouchableIterator implements Iterator { private Iterator delegate; private UntouchableIterator(Iterator newDelegate) { this.delegate = newDelegate; } @Override public boolean hasNext() { return this.delegate.hasNext(); } @Override public T next() { return this.delegate.next(); } @Override public void remove() { this.delegate.remove(); } public void becomeUseless() { this.delegate = null; } } }