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

com.gs.collections.impl.bag.mutable.UnmodifiableBag Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2015 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.impl.bag.mutable;

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

import com.gs.collections.api.bag.ImmutableBag;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.bag.primitive.MutableBooleanBag;
import com.gs.collections.api.bag.primitive.MutableByteBag;
import com.gs.collections.api.bag.primitive.MutableCharBag;
import com.gs.collections.api.bag.primitive.MutableDoubleBag;
import com.gs.collections.api.bag.primitive.MutableFloatBag;
import com.gs.collections.api.bag.primitive.MutableIntBag;
import com.gs.collections.api.bag.primitive.MutableLongBag;
import com.gs.collections.api.bag.primitive.MutableShortBag;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.bag.MutableBagMultimap;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.partition.bag.PartitionMutableBag;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.primitive.ObjectIntPair;
import com.gs.collections.impl.collection.mutable.AbstractUnmodifiableMutableCollection;
import com.gs.collections.impl.factory.Bags;

/**
 * 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();
    }

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

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

    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); } public MutableList> topOccurrences(int count) { return this.getMutableBag().topOccurrences(count); } public MutableList> bottomOccurrences(int count) { return this.getMutableBag().bottomOccurrences(count); } @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); } public void addOccurrences(T item, int occurrences) { throw new UnsupportedOperationException("Cannot call addOccurrences() on " + this.getClass().getSimpleName()); } public boolean removeOccurrences(Object item, int occurrences) { throw new UnsupportedOperationException("Cannot call removeOccurrences() on " + this.getClass().getSimpleName()); } public boolean setOccurrences(T item, int occurrences) { throw new UnsupportedOperationException("Cannot call setOccurrences() on " + this.getClass().getSimpleName()); } public int sizeDistinct() { return this.getMutableBag().sizeDistinct(); } public int occurrencesOf(Object item) { return this.getMutableBag().occurrencesOf(item); } public void forEachWithOccurrences(ObjectIntProcedure objectIntProcedure) { this.getMutableBag().forEachWithOccurrences(objectIntProcedure); } 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; @SuppressWarnings("UnusedDeclaration") public UnmodifiableBagSerializationProxy() { // Empty constructor for Externalizable class } private UnmodifiableBagSerializationProxy(MutableBag bag) { this.mutableBag = bag; } 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; } } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.mutableBag = (MutableBag) in.readObject(); } protected Object readResolve() { return this.mutableBag.asUnmodifiable(); } } }