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

com.gs.collections.impl.list.mutable.UnmodifiableMutableList 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.list.mutable;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.util.RandomAccess;
import java.util.concurrent.ExecutorService;

import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.block.HashingStrategy;
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.procedure.Procedure;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.list.ImmutableList;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.list.ParallelListIterable;
import com.gs.collections.api.list.primitive.MutableBooleanList;
import com.gs.collections.api.list.primitive.MutableByteList;
import com.gs.collections.api.list.primitive.MutableCharList;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.list.primitive.MutableLongList;
import com.gs.collections.api.list.primitive.MutableShortList;
import com.gs.collections.api.multimap.list.MutableListMultimap;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.stack.MutableStack;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.factory.HashingStrategies;
import com.gs.collections.impl.collection.mutable.AbstractUnmodifiableMutableCollection;
import com.gs.collections.impl.collection.mutable.UnmodifiableCollectionSerializationProxy;
import com.gs.collections.impl.lazy.ReverseIterable;
import com.gs.collections.impl.stack.mutable.ArrayStack;

/**
 * An unmodifiable view of a list.
 *
 * @see MutableList#asUnmodifiable()
 */
public class UnmodifiableMutableList
        extends AbstractUnmodifiableMutableCollection
        implements MutableList, Serializable
{
    UnmodifiableMutableList(MutableList mutableList)
    {
        super(mutableList);
    }

    /**
     * This method will take a MutableList and wrap it directly in a UnmodifiableMutableList.  It will
     * take any other non-GS-list and first adapt it will a ListAdapter, and then return a
     * UnmodifiableMutableList that wraps the adapter.
     */
    public static > UnmodifiableMutableList of(L list)
    {
        if (list == null)
        {
            throw new IllegalArgumentException("cannot create an UnmodifiableMutableList for null");
        }
        if (list instanceof RandomAccess)
        {
            return new RandomAccessUnmodifiableMutableList(RandomAccessListAdapter.adapt(list));
        }
        return new UnmodifiableMutableList(ListAdapter.adapt(list));
    }

    private MutableList getMutableList()
    {
        return (MutableList) this.getMutableCollection();
    }

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

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

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

    @Override
    public ImmutableList toImmutable()
    {
        return this.getMutableList().toImmutable();
    }

    @Override
    public MutableList asSynchronized()
    {
        return SynchronizedMutableList.of(this);
    }

    @Override
    public UnmodifiableMutableList clone()
    {
        return this;
    }

    @Override
    public MutableList newEmpty()
    {
        return this.getMutableList().newEmpty();
    }

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

    public  boolean corresponds(OrderedIterable other, Predicate2 predicate)
    {
        return this.getMutableList().corresponds(other, predicate);
    }

    public void forEach(int fromIndex, int toIndex, Procedure procedure)
    {
        this.getMutableList().forEach(fromIndex, toIndex, procedure);
    }

    public void reverseForEach(Procedure procedure)
    {
        this.getMutableList().reverseForEach(procedure);
    }

    public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure objectIntProcedure)
    {
        this.getMutableList().forEachWithIndex(fromIndex, toIndex, objectIntProcedure);
    }

    public UnmodifiableMutableList sortThis(Comparator comparator)
    {
        throw new UnsupportedOperationException("Cannot call sortThis() on " + this.getClass().getSimpleName());
    }

    public UnmodifiableMutableList sortThis()
    {
        throw new UnsupportedOperationException("Cannot call sortThis() on " + this.getClass().getSimpleName());
    }

    public MutableList toReversed()
    {
        return this.getMutableList().toReversed();
    }

    public MutableList reverseThis()
    {
        throw new UnsupportedOperationException("Cannot call reverseThis() on " + this.getClass().getSimpleName());
    }

    public MutableList shuffleThis()
    {
        throw new UnsupportedOperationException("Cannot call shuffleThis() on " + this.getClass().getSimpleName());
    }

    public MutableList shuffleThis(Random rnd)
    {
        throw new UnsupportedOperationException("Cannot call shuffleThis() on " + this.getClass().getSimpleName());
    }

    public MutableStack toStack()
    {
        return ArrayStack.newStack(this.getMutableList());
    }

    public > MutableList sortThisBy(Function function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisBy() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByInt(IntFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByInt() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByBoolean(BooleanFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByBoolean() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByChar(CharFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByChar() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByByte(ByteFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByByte() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByShort(ShortFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByShort() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByFloat(FloatFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByFloat() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByLong(LongFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByLong() on " + this.getClass().getSimpleName());
    }

    public MutableList sortThisByDouble(DoubleFunction function)
    {
        throw new UnsupportedOperationException("Cannot call sortThisByDouble() on " + this.getClass().getSimpleName());
    }

    public boolean addAll(int index, Collection collection)
    {
        throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
    }

    public T get(int index)
    {
        return this.getMutableList().get(index);
    }

    public T set(int index, T element)
    {
        throw new UnsupportedOperationException("Cannot call set() on " + this.getClass().getSimpleName());
    }

    public void add(int index, T element)
    {
        throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
    }

    public T remove(int index)
    {
        throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
    }

    public int indexOf(Object o)
    {
        return this.getMutableList().indexOf(o);
    }

    public int lastIndexOf(Object o)
    {
        return this.getMutableList().lastIndexOf(o);
    }

    public ListIterator listIterator()
    {
        return this.listIterator(0);
    }

    public ListIterator listIterator(int index)
    {
        return new UnmodifiableListIteratorAdapter(this.getMutableList().listIterator(index));
    }

    public UnmodifiableMutableList subList(int fromIndex, int toIndex)
    {
        MutableList subList = this.getMutableList().subList(fromIndex, toIndex);
        return UnmodifiableMutableList.of(subList);
    }

    @Override
    public  MutableList collectWith(Function2 function, P parameter)
    {
        return this.getMutableList().collectWith(function, parameter);
    }

    @Override
    public  MutableList collect(Function function)
    {
        return this.getMutableList().collect(function);
    }

    @Override
    public MutableBooleanList collectBoolean(BooleanFunction booleanFunction)
    {
        return this.getMutableList().collectBoolean(booleanFunction);
    }

    @Override
    public MutableByteList collectByte(ByteFunction byteFunction)
    {
        return this.getMutableList().collectByte(byteFunction);
    }

    @Override
    public MutableCharList collectChar(CharFunction charFunction)
    {
        return this.getMutableList().collectChar(charFunction);
    }

    @Override
    public MutableDoubleList collectDouble(DoubleFunction doubleFunction)
    {
        return this.getMutableList().collectDouble(doubleFunction);
    }

    @Override
    public MutableFloatList collectFloat(FloatFunction floatFunction)
    {
        return this.getMutableList().collectFloat(floatFunction);
    }

    @Override
    public MutableIntList collectInt(IntFunction intFunction)
    {
        return this.getMutableList().collectInt(intFunction);
    }

    @Override
    public MutableLongList collectLong(LongFunction longFunction)
    {
        return this.getMutableList().collectLong(longFunction);
    }

    @Override
    public MutableShortList collectShort(ShortFunction shortFunction)
    {
        return this.getMutableList().collectShort(shortFunction);
    }

    @Override
    public  MutableList flatCollect(Function> function)
    {
        return this.getMutableList().flatCollect(function);
    }

    @Override
    public  MutableList collectIf(
            Predicate predicate,
            Function function)
    {
        return this.getMutableList().collectIf(predicate, function);
    }

    public int detectIndex(Predicate predicate)
    {
        return this.getMutableList().detectIndex(predicate);
    }

    public int detectLastIndex(Predicate predicate)
    {
        return this.getMutableList().detectLastIndex(predicate);
    }

    @Override
    public  MutableListMultimap groupBy(Function function)
    {
        return this.getMutableList().groupBy(function);
    }

    @Override
    public  MutableListMultimap groupByEach(Function> function)
    {
        return this.getMutableList().groupByEach(function);
    }

    @Override
    public MutableList reject(Predicate predicate)
    {
        return this.getMutableList().reject(predicate);
    }

    @Override
    public 

MutableList rejectWith(Predicate2 predicate, P parameter) { return this.getMutableList().rejectWith(predicate, parameter); } @Override public MutableList select(Predicate predicate) { return this.getMutableList().select(predicate); } @Override public

MutableList selectWith(Predicate2 predicate, P parameter) { return this.getMutableList().selectWith(predicate, parameter); } @Override public PartitionMutableList partition(Predicate predicate) { return this.getMutableList().partition(predicate); } @Override public

PartitionMutableList partitionWith(Predicate2 predicate, P parameter) { return this.getMutableList().partitionWith(predicate, parameter); } @Override public MutableList selectInstancesOf(Class clazz) { return this.getMutableList().selectInstancesOf(clazz); } public MutableList distinct() { return this.getMutableList().distinct(); } public MutableList distinct(HashingStrategy hashingStrategy) { return this.getMutableList().distinct(hashingStrategy); } @Override public MutableList> zip(Iterable that) { return this.getMutableList().zip(that); } @Override public MutableList> zipWithIndex() { return this.getMutableList().zipWithIndex(); } public MutableList take(int count) { return this.getMutableList().take(count); } public MutableList takeWhile(Predicate predicate) { return this.getMutableList().takeWhile(predicate); } public MutableList drop(int count) { return this.getMutableList().drop(count); } public MutableList dropWhile(Predicate predicate) { return this.getMutableList().dropWhile(predicate); } public PartitionMutableList partitionWhile(Predicate predicate) { return this.getMutableList().partitionWhile(predicate); } public LazyIterable asReversed() { return ReverseIterable.adapt(this); } public ParallelListIterable asParallel(ExecutorService executorService, int batchSize) { return this.getMutableList().asParallel(executorService, batchSize); } public int binarySearch(T key, Comparator comparator) { return Collections.binarySearch(this, key, comparator); } public int binarySearch(T key) { return Collections.binarySearch((List>) this, key); } @Override public MutableList with(T element) { throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName()); } @Override public MutableList without(T element) { throw new UnsupportedOperationException("Cannot call without() on " + this.getClass().getSimpleName()); } @Override public MutableList withAll(Iterable elements) { throw new UnsupportedOperationException("Cannot call withAll() on " + this.getClass().getSimpleName()); } @Override public MutableList withoutAll(Iterable elements) { throw new UnsupportedOperationException("Cannot call withoutAll() on " + this.getClass().getSimpleName()); } private static final class RandomAccessUnmodifiableMutableList extends UnmodifiableMutableList implements RandomAccess { private static final long serialVersionUID = 1L; RandomAccessUnmodifiableMutableList(MutableList mutableList) { super(mutableList); } @Override public RandomAccessUnmodifiableMutableList clone() { return this; } } protected Object writeReplace() { return new UnmodifiableCollectionSerializationProxy(this.getMutableList()); } }