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

com.gs.collections.impl.list.mutable.AbstractMutableList 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.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.RandomAccess;
import java.util.concurrent.ExecutorService;

import com.gs.collections.api.block.HashingStrategy;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.Function3;
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.FloatObjectToFloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
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.Procedure2;
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.map.MutableMap;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.stack.MutableStack;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.block.factory.Functions;
import com.gs.collections.impl.block.factory.HashingStrategies;
import com.gs.collections.impl.block.factory.Predicates2;
import com.gs.collections.impl.collection.mutable.AbstractMutableCollection;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.factory.Stacks;
import com.gs.collections.impl.lazy.ReverseIterable;
import com.gs.collections.impl.lazy.parallel.list.ListIterableParallelIterable;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.stack.mutable.ArrayStack;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.ListIterate;
import com.gs.collections.impl.utility.OrderedIterate;

public abstract class AbstractMutableList
        extends AbstractMutableCollection
        implements MutableList
{
    private static final IntObjectToIntFunction HASH_CODE_FUNCTION = new IntObjectToIntFunction()
    {
        public int intValueOf(int hashCode, Object item)
        {
            return 31 * hashCode + (item == null ? 0 : item.hashCode());
        }
    };

    @Override
    public MutableList clone()
    {
        try
        {
            return (MutableList) super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            throw new AssertionError(e);
        }
    }

    @Override
    public boolean equals(Object that)
    {
        return this == that || (that instanceof List && ListIterate.equals(this, (List) that));
    }

    @Override
    public int hashCode()
    {
        // Optimize injectInto in subclasses if necessary
        return this.injectInto(1, (IntObjectToIntFunction) HASH_CODE_FUNCTION);
    }

    public void each(Procedure procedure)
    {
        ListIterate.forEach(this, procedure);
    }

    public void reverseForEach(Procedure procedure)
    {
        if (this.notEmpty())
        {
            this.forEach(this.size() - 1, 0, procedure);
        }
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        ListIterate.forEachWithIndex(this, objectIntProcedure);
    }

    @Override
    public 

void forEachWith(Procedure2 procedure, P parameter) { ListIterate.forEachWith(this, procedure, parameter); } @Override public >> R zip(Iterable that, R target) { return ListIterate.zip(this, that, target); } @Override public >> R zipWithIndex(R target) { return ListIterate.zipWithIndex(this, target); } public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure objectIntProcedure) { ListIterate.forEachWithIndex(this, fromIndex, toIndex, objectIntProcedure); } public MutableList select(Predicate predicate) { return this.select(predicate, this.newEmpty()); } @Override public > R select(Predicate predicate, R target) { return ListIterate.select(this, predicate, target); } public

MutableList selectWith(Predicate2 predicate, P parameter) { return this.selectWith(predicate, parameter, this.newEmpty()); } @Override public > R selectWith( Predicate2 predicate, P parameter, R target) { return ListIterate.selectWith(this, predicate, parameter, target); } public MutableList reject(Predicate predicate) { return this.reject(predicate, this.newEmpty()); } @Override public > R reject(Predicate predicate, R target) { return ListIterate.reject(this, predicate, target); } public

MutableList rejectWith(Predicate2 predicate, P parameter) { return this.rejectWith(predicate, parameter, this.newEmpty()); } @Override public > R rejectWith( Predicate2 predicate, P parameter, R target) { return ListIterate.rejectWith(this, predicate, parameter, target); } @Override public

Twin> selectAndRejectWith( Predicate2 predicate, P parameter) { return ListIterate.selectAndRejectWith(this, predicate, parameter); } public PartitionMutableList partition(Predicate predicate) { return ListIterate.partition(this, predicate); } public

PartitionMutableList partitionWith(Predicate2 predicate, P parameter) { return ListIterate.partitionWith(this, predicate, parameter); } public MutableList selectInstancesOf(Class clazz) { return ListIterate.selectInstancesOf(this, clazz); } @Override public boolean removeIf(Predicate predicate) { return ListIterate.removeIf(this, predicate); } @Override public

boolean removeIfWith(Predicate2 predicate, P parameter) { return ListIterate.removeIfWith(this, predicate, parameter); } public MutableList collect(Function function) { return this.collect(function, FastList.newList()); } public MutableBooleanList collectBoolean(BooleanFunction booleanFunction) { return ListIterate.collectBoolean(this, booleanFunction); } public MutableByteList collectByte(ByteFunction byteFunction) { return ListIterate.collectByte(this, byteFunction); } public MutableCharList collectChar(CharFunction charFunction) { return ListIterate.collectChar(this, charFunction); } public MutableDoubleList collectDouble(DoubleFunction doubleFunction) { return ListIterate.collectDouble(this, doubleFunction); } public MutableFloatList collectFloat(FloatFunction floatFunction) { return ListIterate.collectFloat(this, floatFunction); } public MutableIntList collectInt(IntFunction intFunction) { return ListIterate.collectInt(this, intFunction); } public MutableLongList collectLong(LongFunction longFunction) { return ListIterate.collectLong(this, longFunction); } public MutableShortList collectShort(ShortFunction shortFunction) { return ListIterate.collectShort(this, shortFunction); } @Override public > R collect(Function function, R target) { return ListIterate.collect(this, function, target); } public MutableList flatCollect(Function> function) { return this.flatCollect(function, FastList.newList()); } @Override public > R flatCollect( Function> function, R target) { return ListIterate.flatCollect(this, function, target); } public MutableList collectWith(Function2 function, P parameter) { return this.collectWith(function, parameter, FastList.newList()); } @Override public > R collectWith( Function2 function, P parameter, R target) { return ListIterate.collectWith(this, function, parameter, target); } public MutableList collectIf( Predicate predicate, Function function) { return this.collectIf(predicate, function, FastList.newList()); } @Override public > R collectIf( Predicate predicate, Function function, R target) { return ListIterate.collectIf(this, predicate, function, target); } @Override public T detect(Predicate predicate) { return ListIterate.detect(this, predicate); } @Override public T detectIfNone(Predicate predicate, Function0 function) { T result = this.detect(predicate); return result == null ? function.value() : result; } public int detectIndex(Predicate predicate) { return ListIterate.detectIndex(this, predicate); } public int detectLastIndex(Predicate predicate) { return ListIterate.detectLastIndex(this, predicate); } @Override public T min(Comparator comparator) { return ListIterate.min(this, comparator); } @Override public T max(Comparator comparator) { return ListIterate.max(this, comparator); } @Override public T min() { return ListIterate.min(this); } @Override public T max() { return ListIterate.max(this); } @Override public > T minBy(Function function) { return ListIterate.minBy(this, function); } @Override public > T maxBy(Function function) { return ListIterate.maxBy(this, function); } @Override public

T detectWith(Predicate2 predicate, P parameter) { return ListIterate.detectWith(this, predicate, parameter); } @Override public

T detectWithIfNone( Predicate2 predicate, P parameter, Function0 function) { T result = ListIterate.detectWith(this, predicate, parameter); return result == null ? function.value() : result; } @Override public int count(Predicate predicate) { return ListIterate.count(this, predicate); } @Override public

int countWith(Predicate2 predicate, P parameter) { return ListIterate.countWith(this, predicate, parameter); } public boolean corresponds(OrderedIterable other, Predicate2 predicate) { return OrderedIterate.corresponds(this, other, predicate); } @Override public boolean anySatisfy(Predicate predicate) { return ListIterate.anySatisfy(this, predicate); } @Override public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return ListIterate.anySatisfyWith(this, predicate, parameter); } @Override public boolean allSatisfy(Predicate predicate) { return ListIterate.allSatisfy(this, predicate); } @Override public

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

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return ListIterate.noneSatisfyWith(this, predicate, parameter); } @Override public IV injectInto(IV injectedValue, Function2 function) { return ListIterate.injectInto(injectedValue, this, function); } @Override public int injectInto(int injectedValue, IntObjectToIntFunction function) { return ListIterate.injectInto(injectedValue, this, function); } @Override public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { return ListIterate.injectInto(injectedValue, this, function); } public MutableList distinct() { return ListIterate.distinct(this); } public MutableList distinct(HashingStrategy hashingStrategy) { return ListIterate.distinct(this, hashingStrategy); } @Override public long sumOfInt(IntFunction function) { return ListIterate.sumOfInt(this, function); } @Override public long sumOfLong(LongFunction function) { return ListIterate.sumOfLong(this, function); } @Override public double sumOfFloat(FloatFunction function) { return ListIterate.sumOfFloat(this, function); } @Override public double sumOfDouble(DoubleFunction function) { return ListIterate.sumOfDouble(this, function); } @Override public long injectInto(long injectedValue, LongObjectToLongFunction function) { return ListIterate.injectInto(injectedValue, this, function); } @Override public IV injectIntoWith( IV injectValue, Function3 function, P parameter) { return ListIterate.injectIntoWith(injectValue, this, function, parameter); } @Override public MutableList toList() { return FastList.newList(this); } @Override public MutableList toSortedList() { return this.toSortedList(Comparators.naturalOrder()); } @Override public MutableList toSortedList(Comparator comparator) { return this.toList().sortThis(comparator); } @Override public MutableSet toSet() { return UnifiedSet.newSet(this); } public MutableStack toStack() { return Stacks.mutable.withAll(this); } public MutableList asUnmodifiable() { return UnmodifiableMutableList.of(this); } public ImmutableList toImmutable() { return Lists.immutable.withAll(this); } public MutableList asSynchronized() { return SynchronizedMutableList.of(this); } public MutableList sortThis(Comparator comparator) { if (this.size() < 10) { if (comparator == null) { this.insertionSort(); } else { this.insertionSort(comparator); } } else { this.defaultSort(comparator); } return this; } /** * Override in subclasses where it can be optimized. */ protected void defaultSort(Comparator comparator) { Collections.sort(this, comparator); } private void insertionSort(Comparator comparator) { for (int i = 0; i < this.size(); i++) { for (int j = i; j > 0 && comparator.compare(this.get(j - 1), this.get(j)) > 0; j--) { Collections.swap(this, j, j - 1); } } } private void insertionSort() { for (int i = 0; i < this.size(); i++) { for (int j = i; j > 0 && ((Comparable) this.get(j - 1)).compareTo(this.get(j)) > 0; j--) { Collections.swap(this, j, j - 1); } } } public MutableList sortThis() { return this.sortThis(Comparators.naturalOrder()); } public > MutableList sortThisBy(Function function) { return this.sortThis(Comparators.byFunction(function)); } public MutableList sortThisByInt(IntFunction function) { return this.sortThis(Functions.toIntComparator(function)); } public MutableList sortThisByBoolean(BooleanFunction function) { return this.sortThis(Functions.toBooleanComparator(function)); } public MutableList sortThisByChar(CharFunction function) { return this.sortThis(Functions.toCharComparator(function)); } public MutableList sortThisByByte(ByteFunction function) { return this.sortThis(Functions.toByteComparator(function)); } public MutableList sortThisByShort(ShortFunction function) { return this.sortThis(Functions.toShortComparator(function)); } public MutableList sortThisByFloat(FloatFunction function) { return this.sortThis(Functions.toFloatComparator(function)); } public MutableList sortThisByLong(LongFunction function) { return this.sortThis(Functions.toLongComparator(function)); } public MutableList sortThisByDouble(DoubleFunction function) { return this.sortThis(Functions.toDoubleComparator(function)); } public MutableList newEmpty() { return Lists.mutable.empty(); } public MutableList tap(Procedure procedure) { this.forEach(procedure); return this; } public void forEach(int from, int to, Procedure procedure) { ListIterate.forEach(this, from, to, procedure); } public int indexOf(Object object) { for (int i = 0; i < this.size(); i++) { if (Comparators.nullSafeEquals(this.get(i), object)) { return i; } } return -1; } public int lastIndexOf(Object object) { for (int i = this.size() - 1; i >= 0; i--) { if (Comparators.nullSafeEquals(this.get(i), object)) { return i; } } return -1; } public Iterator iterator() { return new MutableIterator(this); } public ListIterator listIterator() { return this.listIterator(0); } public ListIterator listIterator(int index) { if (index < 0 || index > this.size()) { throw new IndexOutOfBoundsException("Index: " + index); } return new MutableListIterator(this, index); } public MutableList toReversed() { return FastList.newList(this).reverseThis(); } public MutableList reverseThis() { Collections.reverse(this); return this; } public MutableList shuffleThis() { Collections.shuffle(this); return this; } public MutableList shuffleThis(Random rnd) { Collections.shuffle(this, rnd); return this; } public MutableList subList(int fromIndex, int toIndex) { return new SubList(this, fromIndex, toIndex); } protected static class SubList extends AbstractMutableList implements Serializable, RandomAccess { // Not important since it uses writeReplace() private static final long serialVersionUID = 1L; private final MutableList original; private final int offset; private int size; protected SubList(AbstractMutableList list, int fromIndex, int toIndex) { if (fromIndex < 0) { throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); } if (toIndex > list.size()) { throw new IndexOutOfBoundsException("toIndex = " + toIndex); } if (fromIndex > toIndex) { throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ')'); } this.original = list; this.offset = fromIndex; this.size = toIndex - fromIndex; } @Override public MutableList toReversed() { return FastList.newList(this).reverseThis(); } protected Object writeReplace() { return FastList.newList(this); } @Override public boolean add(T o) { this.original.add(this.offset + this.size, o); this.size++; return true; } public T set(int index, T element) { this.checkIfOutOfBounds(index); return this.original.set(index + this.offset, element); } public T get(int index) { this.checkIfOutOfBounds(index); return this.original.get(index + this.offset); } public int size() { return this.size; } public void add(int index, T element) { this.checkIfOutOfBounds(index); this.original.add(index + this.offset, element); this.size++; } public T remove(int index) { this.checkIfOutOfBounds(index); T result = this.original.remove(index + this.offset); this.size--; return result; } public void clear() { for (Iterator iterator = this.iterator(); iterator.hasNext(); ) { iterator.next(); iterator.remove(); } } @Override public boolean addAll(Collection collection) { return this.addAll(this.size, collection); } public boolean addAll(int index, Collection collection) { if (index < 0 || index > this.size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size); } int cSize = collection.size(); if (cSize == 0) { return false; } this.original.addAll(this.offset + index, collection); this.size += cSize; return true; } @Override public Iterator iterator() { return this.listIterator(); } @Override public ListIterator listIterator(final int index) { if (index < 0 || index > this.size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size); } return new ListIterator() { private final ListIterator listIterator = SubList.this.original.listIterator(index + SubList.this.offset); public boolean hasNext() { return this.nextIndex() < SubList.this.size; } public T next() { if (this.hasNext()) { return this.listIterator.next(); } throw new NoSuchElementException(); } public boolean hasPrevious() { return this.previousIndex() >= 0; } public T previous() { if (this.hasPrevious()) { return this.listIterator.previous(); } throw new NoSuchElementException(); } public int nextIndex() { return this.listIterator.nextIndex() - SubList.this.offset; } public int previousIndex() { return this.listIterator.previousIndex() - SubList.this.offset; } public void remove() { this.listIterator.remove(); SubList.this.size--; } public void set(T o) { this.listIterator.set(o); } public void add(T o) { this.listIterator.add(o); SubList.this.size++; } }; } @Override public MutableList subList(int fromIndex, int toIndex) { return new SubList(this, fromIndex, toIndex); } private void checkIfOutOfBounds(int index) { if (index >= this.size || index < 0) { throw new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size); } } // Weird implementation of clone() is ok on final classes @Override public MutableList clone() { return new FastList(this); } @Override public T getFirst() { return this.isEmpty() ? null : this.original.get(this.offset); } @Override public T getLast() { return this.isEmpty() ? null : this.original.get(this.offset + this.size - 1); } @Override public MutableStack toStack() { return ArrayStack.newStack(this); } @Override public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) { ListIterate.forEachWithIndex(this, objectIntProcedure); } @Override public

void forEachWith(Procedure2 procedure, P parameter) { ListIterate.forEachWith(this, procedure, parameter); } } @Override public boolean contains(Object object) { return this.indexOf(object) > -1; } @Override public boolean containsAll(Collection source) { return Iterate.allSatisfyWith(source, Predicates2.in(), this); } @Override public boolean removeAll(Collection collection) { int currentSize = this.size(); this.removeIfWith(Predicates2.in(), collection); return currentSize != this.size(); } @Override public boolean retainAll(Collection collection) { int currentSize = this.size(); this.removeIfWith(Predicates2.notIn(), collection); return currentSize != this.size(); } public T getFirst() { return ListIterate.getFirst(this); } public T getLast() { return ListIterate.getLast(this); } @Override public void appendString(Appendable appendable, String separator) { this.appendString(appendable, "", separator, ""); } @Override public void appendString(Appendable appendable, String start, String separator, String end) { ListIterate.appendString(this, appendable, start, separator, end); } public FastListMultimap groupBy(Function function) { return ListIterate.groupBy(this, function); } public FastListMultimap groupByEach(Function> function) { return ListIterate.groupByEach(this, function); } @Override public MutableMap groupByUniqueKey(Function function) { return ListIterate.groupByUniqueKey(this, function); } public MutableList> zip(Iterable that) { return ListIterate.zip(this, that); } public MutableList> zipWithIndex() { return ListIterate.zipWithIndex(this); } public MutableList with(T element) { this.add(element); return this; } public MutableList without(T element) { this.remove(element); return this; } public MutableList withAll(Iterable elements) { this.addAllIterable(elements); return this; } public MutableList withoutAll(Iterable elements) { this.removeAllIterable(elements); return this; } public ReverseIterable asReversed() { return ReverseIterable.adapt(this); } public ParallelListIterable asParallel(ExecutorService executorService, int batchSize) { return new ListIterableParallelIterable(this, 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); } public MutableList take(int count) { return ListIterate.take(this, count); } public MutableList takeWhile(Predicate predicate) { return ListIterate.takeWhile(this, predicate); } public MutableList drop(int count) { return ListIterate.drop(this, count); } public MutableList dropWhile(Predicate predicate) { return ListIterate.dropWhile(this, predicate); } public PartitionMutableList partitionWhile(Predicate predicate) { return ListIterate.partitionWhile(this, predicate); } }