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

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

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2022 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.bag.mutable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Collection;

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.ObjectIntToObjectFunction;
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.predicate.primitive.ObjectIntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.factory.primitive.ObjectLongMaps;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
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.AbstractUnmodifiableMutableCollection;

/**
 * An unmodifiable view of a bag.
 *
 * @see MutableBag#asUnmodifiable()
 * @since 1.0
 */
public class UnmodifiableBag
        extends AbstractUnmodifiableMutableCollection
        implements MutableBag, Serializable
{
    UnmodifiableBag(MutableBag mutableBag)
    {
        super(mutableBag);
    }

    /**
     * This method will take a MutableBag and wrap it directly in a UnmodifiableBag.
     */
    public static > UnmodifiableBag of(B bag)
    {
        if (bag == null)
        {
            throw new IllegalArgumentException("cannot create an UnmodifiableBag for null");
        }
        return new UnmodifiableBag<>(bag);
    }

    protected MutableBag getMutableBag()
    {
        return (MutableBag) this.getMutableCollection();
    }

    @Override
    public MutableBag asUnmodifiable()
    {
        return this;
    }

    @Override
    public MutableBag asSynchronized()
    {
        return SynchronizedBag.of(this);
    }

    @Override
    public ImmutableBag toImmutable()
    {
        return Bags.immutable.withAll(this);
    }

    @Override
    public boolean equals(Object obj)
    {
        return this.getMutableBag().equals(obj);
    }

    @Override
    public int hashCode()
    {
        return this.getMutableBag().hashCode();
    }

    @Override
    public String toStringOfItemToCount()
    {
        return this.getMutableBag().toStringOfItemToCount();
    }

    @Override
    public MutableBag newEmpty()
    {
        return this.getMutableBag().newEmpty();
    }

    @Override
    public MutableBag selectByOccurrences(IntPredicate predicate)
    {
        return this.getMutableBag().selectByOccurrences(predicate);
    }

    @Override
    public MutableBag tap(Procedure procedure)
    {
        this.forEach(procedure);
        return this;
    }

    @Override
    public MutableBag select(Predicate predicate)
    {
        return this.getMutableBag().select(predicate);
    }

    @Override
    public 

MutableBag selectWith(Predicate2 predicate, P parameter) { return this.getMutableBag().selectWith(predicate, parameter); } @Override public MutableBag reject(Predicate predicate) { return this.getMutableBag().reject(predicate); } @Override public

MutableBag rejectWith(Predicate2 predicate, P parameter) { return this.getMutableBag().rejectWith(predicate, parameter); } @Override public PartitionMutableBag partition(Predicate predicate) { return this.getMutableBag().partition(predicate); } @Override public

PartitionMutableBag partitionWith(Predicate2 predicate, P parameter) { return this.getMutableBag().partitionWith(predicate, parameter); } @Override public MutableBag selectInstancesOf(Class clazz) { return this.getMutableBag().selectInstancesOf(clazz); } @Override public MutableBag collect(Function function) { return this.getMutableBag().collect(function); } @Override public MutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return this.getMutableBag().collectBoolean(booleanFunction); } @Override public MutableByteBag collectByte(ByteFunction byteFunction) { return this.getMutableBag().collectByte(byteFunction); } @Override public MutableCharBag collectChar(CharFunction charFunction) { return this.getMutableBag().collectChar(charFunction); } @Override public MutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return this.getMutableBag().collectDouble(doubleFunction); } @Override public MutableFloatBag collectFloat(FloatFunction floatFunction) { return this.getMutableBag().collectFloat(floatFunction); } @Override public MutableIntBag collectInt(IntFunction intFunction) { return this.getMutableBag().collectInt(intFunction); } @Override public MutableLongBag collectLong(LongFunction longFunction) { return this.getMutableBag().collectLong(longFunction); } @Override public MutableShortBag collectShort(ShortFunction shortFunction) { return this.getMutableBag().collectShort(shortFunction); } @Override public MutableBag flatCollect(Function> function) { return this.getMutableBag().flatCollect(function); } @Override public MutableList> topOccurrences(int count) { return this.getMutableBag().topOccurrences(count); } @Override public MutableList> bottomOccurrences(int count) { return this.getMutableBag().bottomOccurrences(count); } @Override public MutableBag collectWithOccurrences(ObjectIntToObjectFunction function) { return this.getMutableBag().collectWithOccurrences(function, Bags.mutable.empty()); } @Override public MutableBag collectWith(Function2 function, P parameter) { return this.getMutableBag().collectWith(function, parameter); } @Override public MutableBag collectIf( Predicate predicate, Function function) { return this.getMutableBag().collectIf(predicate, function); } @Override public MutableBagMultimap groupBy(Function function) { return this.getMutableBag().groupBy(function); } @Override public MutableBagMultimap groupByEach(Function> function) { return this.getMutableBag().groupByEach(function); } @Override public int addOccurrences(T item, int occurrences) { throw new UnsupportedOperationException("Cannot call addOccurrences() on " + this.getClass().getSimpleName()); } @Override public boolean removeOccurrences(Object item, int occurrences) { throw new UnsupportedOperationException("Cannot call removeOccurrences() on " + this.getClass().getSimpleName()); } @Override public boolean setOccurrences(T item, int occurrences) { throw new UnsupportedOperationException("Cannot call setOccurrences() on " + this.getClass().getSimpleName()); } @Override public int sizeDistinct() { return this.getMutableBag().sizeDistinct(); } @Override public int occurrencesOf(Object item) { return this.getMutableBag().occurrencesOf(item); } @Override public void forEachWithOccurrences(ObjectIntProcedure objectIntProcedure) { this.getMutableBag().forEachWithOccurrences(objectIntProcedure); } @Override public boolean anySatisfyWithOccurrences(ObjectIntPredicate predicate) { return this.getMutableBag().anySatisfyWithOccurrences(predicate); } @Override public boolean allSatisfyWithOccurrences(ObjectIntPredicate predicate) { return this.getMutableBag().allSatisfyWithOccurrences(predicate); } @Override public boolean noneSatisfyWithOccurrences(ObjectIntPredicate predicate) { return this.getMutableBag().noneSatisfyWithOccurrences(predicate); } @Override public T detectWithOccurrences(ObjectIntPredicate predicate) { return this.getMutableBag().detectWithOccurrences(predicate); } /** * @since 9.1. */ @Override public > R collectWithOccurrences(ObjectIntToObjectFunction function, R target) { return this.getMutableBag().collectWithOccurrences(function, target); } @Override public MutableMap toMapOfItemToCount() { return this.getMutableBag().toMapOfItemToCount(); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Deprecated @Override public MutableBag> zip(Iterable that) { return this.getMutableBag().zip(that); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Deprecated @Override public MutableSet> zipWithIndex() { return this.getMutableBag().zipWithIndex(); } @Override public MutableBag with(T element) { throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName()); } @Override public MutableBag without(T element) { throw new UnsupportedOperationException("Cannot call without() on " + this.getClass().getSimpleName()); } @Override public MutableBag withAll(Iterable elements) { throw new UnsupportedOperationException("Cannot call withAll() on " + this.getClass().getSimpleName()); } @Override public MutableBag withoutAll(Iterable elements) { throw new UnsupportedOperationException("Cannot call withoutAll() on " + this.getClass().getSimpleName()); } protected Object writeReplace() { return new UnmodifiableBagSerializationProxy<>(this.getMutableBag()); } private static class UnmodifiableBagSerializationProxy implements Externalizable { private static final long serialVersionUID = 1L; private MutableBag mutableBag; public UnmodifiableBagSerializationProxy() { // Empty constructor for Externalizable class } private UnmodifiableBagSerializationProxy(MutableBag bag) { this.mutableBag = bag; } @Override public void writeExternal(ObjectOutput out) throws IOException { try { out.writeObject(this.mutableBag); } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } throw e; } } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.mutableBag = (MutableBag) in.readObject(); } protected Object readResolve() { return this.mutableBag.asUnmodifiable(); } } @Override public MutableSet selectUnique() { return this.getMutableBag().selectUnique(); } @Override public MutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { MutableObjectLongMap result = ObjectLongMaps.mutable.empty(); this.forEachWithOccurrences((each, occurrences) -> result.addToValue( groupBy.valueOf(each), function.intValueOf(each) * (long) occurrences)); return result; } @Override public MutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { MutableObjectLongMap result = ObjectLongMaps.mutable.empty(); this.forEachWithOccurrences((each, occurrences) -> result.addToValue( groupBy.valueOf(each), function.longValueOf(each) * (long) occurrences)); return result; } @Override public RichIterable distinctView() { return this.getMutableBag().distinctView(); } }