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

com.gs.collections.impl.list.fixed.AbstractArrayAdapter 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.fixed;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;

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.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.MutableList;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.impl.block.factory.Predicates2;
import com.gs.collections.impl.list.mutable.AbstractMutableList;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.ListAdapter;
import com.gs.collections.impl.utility.ArrayIterate;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.ListIterate;
import com.gs.collections.impl.utility.internal.InternalArrayIterate;
import com.gs.collections.impl.utility.internal.RandomAccessListIterate;

public abstract class AbstractArrayAdapter
        extends AbstractMutableList
        implements RandomAccess
{
    private static final Object[] OBJECTS = {};
    protected T[] items;

    /**
     * This method must be here so subclasses can be serializable.
     */
    protected AbstractArrayAdapter()
    {
        this.items = (T[]) OBJECTS;
    }

    protected AbstractArrayAdapter(T[] newElements)
    {
        if (newElements == null)
        {
            throw new IllegalArgumentException("items cannot be null");
        }
        this.items = newElements;
    }

    @Override
    public boolean notEmpty()
    {
        return this.items.length > 0;
    }

    @Override
    public T getFirst()
    {
        return this.isEmpty() ? null : this.items[0];
    }

    @Override
    public T getLast()
    {
        return this.isEmpty() ? null : this.items[this.items.length - 1];
    }

    @Override
    public void each(Procedure procedure)
    {
        int size = this.size();
        for (int i = 0; i < size; i++)
        {
            procedure.value(this.items[i]);
        }
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        InternalArrayIterate.forEachWithIndex(this.items, this.items.length, objectIntProcedure);
    }

    @Override
    public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure objectIntProcedure)
    {
        ListIterate.rangeCheck(fromIndex, toIndex, this.items.length);
        InternalArrayIterate.forEachWithIndexWithoutChecks(this.items, fromIndex, toIndex, objectIntProcedure);
    }

    @Override
    public boolean removeIf(Predicate predicate)
    {
        throw new UnsupportedOperationException("Cannot call removeIf() on " + this.getClass().getSimpleName());
    }

    @Override
    public 

boolean removeIfWith(Predicate2 predicate, P parameter) { throw new UnsupportedOperationException("Cannot call removeIfWith() on " + this.getClass().getSimpleName()); } @Override public T detect(Predicate predicate) { return InternalArrayIterate.detect(this.items, this.items.length, predicate); } @Override public T detectIfNone(Predicate predicate, Function0 function) { T result = this.detect(predicate); return result == null ? function.value() : result; } @Override public int detectIndex(Predicate predicate) { return InternalArrayIterate.detectIndex(this.items, this.items.length, predicate); } @Override public int detectLastIndex(Predicate predicate) { return InternalArrayIterate.detectLastIndex(this.items, this.items.length, predicate); } @Override public int count(Predicate predicate) { return InternalArrayIterate.count(this.items, this.items.length, predicate); } @Override public boolean corresponds(OrderedIterable other, Predicate2 predicate) { return InternalArrayIterate.corresponds(this.items, this.items.length, other, predicate); } @Override public boolean anySatisfy(Predicate predicate) { return InternalArrayIterate.anySatisfy(this.items, this.items.length, predicate); } @Override public boolean allSatisfy(Predicate predicate) { return InternalArrayIterate.allSatisfy(this.items, this.items.length, predicate); } @Override public boolean noneSatisfy(Predicate predicate) { return InternalArrayIterate.noneSatisfy(this.items, this.items.length, predicate); } @Override public IV injectInto(IV injectedValue, Function2 function) { return ArrayIterate.injectInto(injectedValue, this.items, function); } @Override public MutableList select(Predicate predicate) { return this.select(predicate, FastList.newList()); } @Override public > R select(Predicate predicate, R target) { return InternalArrayIterate.select(this.items, this.items.length, predicate, target); } @Override public MutableList reject(Predicate predicate) { return this.reject(predicate, FastList.newList()); } @Override public > R reject(Predicate predicate, R target) { return InternalArrayIterate.reject(this.items, this.items.length, predicate, target); } @Override public MutableList collect(Function function) { return this.collect(function, FastList.newList(this.size())); } @Override public > R collect(Function function, R target) { return InternalArrayIterate.collect(this.items, this.items.length, function, target); } @Override 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 InternalArrayIterate.collectIf(this.items, this.items.length, predicate, function, target); } @Override public MutableList flatCollect(Function> function) { return InternalArrayIterate.flatCollect(this.items, this.items.length, function, FastList.newList(this.items.length)); } @Override public > R flatCollect(Function> function, R target) { return InternalArrayIterate.flatCollect(this.items, this.items.length, function, target); } @Override public

Twin> selectAndRejectWith( Predicate2 predicate, P parameter) { return InternalArrayIterate.selectAndRejectWith(this.items, this.items.length, predicate, parameter); } public int size() { return this.items.length; } @Override public boolean isEmpty() { return this.items.length == 0; } @Override public boolean contains(Object o) { return InternalArrayIterate.anySatisfyWith(this.items, this.items.length, Predicates2.equal(), o); } @Override public Iterator iterator() { return Arrays.asList(this.items).iterator(); } @Override public Object[] toArray() { return this.items.clone(); } @Override public E[] toArray(E[] array) { int size = this.size(); E[] result = array.length < size ? (E[]) Array.newInstance(array.getClass().getComponentType(), size) : array; System.arraycopy(this.items, 0, result, 0, size); if (result.length > size) { result[size] = null; } return result; } @Override public boolean remove(Object o) { throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName()); } @Override public boolean containsAll(Collection collection) { return Iterate.allSatisfyWith(collection, Predicates2.in(), this); } @Override public boolean addAll(Collection collection) { throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName()); } @Override public boolean addAllIterable(Iterable iterable) { throw new UnsupportedOperationException("Cannot call addAllIterable() on " + this.getClass().getSimpleName()); } @Override public boolean removeAll(Collection collection) { throw new UnsupportedOperationException("Cannot call removeAll() on " + this.getClass().getSimpleName()); } @Override public boolean removeAllIterable(Iterable iterable) { throw new UnsupportedOperationException("Cannot call removeAllIterable() on " + this.getClass().getSimpleName()); } @Override public boolean retainAll(Collection collection) { throw new UnsupportedOperationException("Cannot call retainAll() on " + this.getClass().getSimpleName()); } @Override public boolean retainAllIterable(Iterable iterable) { throw new UnsupportedOperationException("Cannot call retainAllIterable() on " + this.getClass().getSimpleName()); } public void clear() { throw new UnsupportedOperationException("Cannot call clear() 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) { if (index >= this.size()) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size()); } return this.items[index]; } 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()); } @Override public int indexOf(Object item) { return InternalArrayIterate.indexOf(this.items, this.items.length, item); } @Override public int lastIndexOf(Object item) { return InternalArrayIterate.lastIndexOf(this.items, this.items.length, item); } @Override public ListIterator listIterator(int index) { return Arrays.asList(this.items).listIterator(index); } @Override public MutableList subList(int fromIndex, int toIndex) { return ListAdapter.adapt(Arrays.asList(this.items).subList(fromIndex, toIndex)); } @Override public boolean equals(Object that) { if (that == this) { return true; } if (!(that instanceof List)) { return false; } if (that instanceof AbstractArrayAdapter) { return this.abstractArrayAdapterEquals((AbstractArrayAdapter) that); } return InternalArrayIterate.arrayEqualsList(this.items, this.items.length, (List) that); } public boolean abstractArrayAdapterEquals(AbstractArrayAdapter list) { return Arrays.equals(this.items, list.items); } @Override public int hashCode() { return Arrays.hashCode(this.items); } @Override public

void forEachWith(Procedure2 procedure, P parameter) { for (T each : this.items) { procedure.value(each, parameter); } } @Override public

MutableList selectWith(Predicate2 predicate, P parameter) { return this.selectWith(predicate, parameter, FastList.newList()); } @Override public > R selectWith( Predicate2 predicate, P parameter, R target) { return InternalArrayIterate.selectWith(this.items, this.items.length, predicate, parameter, target); } @Override public

MutableList rejectWith(Predicate2 predicate, P parameter) { return this.rejectWith(predicate, parameter, FastList.newList()); } @Override public > R rejectWith( Predicate2 predicate, P parameter, R target) { return InternalArrayIterate.rejectWith(this.items, this.items.length, predicate, parameter, target); } @Override 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 InternalArrayIterate.collectWith(this.items, this.items.length, function, parameter, target); } @Override public IV injectIntoWith( IV injectValue, Function3 function, P parameter) { return ArrayIterate.injectIntoWith(injectValue, this.items, function, parameter); } @Override public void forEach(int fromIndex, int toIndex, Procedure procedure) { ListIterate.rangeCheck(fromIndex, toIndex, this.items.length); InternalArrayIterate.forEachWithoutChecks(this.items, fromIndex, toIndex, procedure); } @Override public

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

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

int countWith(Predicate2 predicate, P parameter) { return InternalArrayIterate.countWith(this.items, this.items.length, predicate, parameter); } @Override public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return InternalArrayIterate.anySatisfyWith(this.items, this.items.length, predicate, parameter); } @Override public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { return InternalArrayIterate.allSatisfyWith(this.items, this.items.length, predicate, parameter); } @Override public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return InternalArrayIterate.noneSatisfyWith(this.items, this.items.length, predicate, parameter); } @Override public MutableList distinct() { return InternalArrayIterate.distinct(this.items, this.items.length); } @Override public MutableList distinct(HashingStrategy hashingStrategy) { return InternalArrayIterate.distinct(this.items, this.items.length, hashingStrategy); } @Override public void appendString(Appendable appendable, String start, String separator, String end) { InternalArrayIterate.appendString(this, this.items, this.items.length, appendable, start, separator, end); } @Override public MutableList take(int count) { return RandomAccessListIterate.take(this, count); } @Override public MutableList drop(int count) { return RandomAccessListIterate.drop(this, count); } }