/*
* Copyright 2014 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.immutable.primitive;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.NoSuchElementException;
import com.gs.collections.api.IntIterable;
import com.gs.collections.api.LazyIntIterable;
import com.gs.collections.api.bag.primitive.MutableIntBag;
import com.gs.collections.api.block.function.primitive.IntToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectIntIntToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.primitive.IntProcedure;
import com.gs.collections.api.block.procedure.primitive.IntIntProcedure;
import com.gs.collections.api.iterator.IntIterator;
import com.gs.collections.api.list.ImmutableList;
import com.gs.collections.api.list.primitive.ImmutableIntList;
import com.gs.collections.api.list.primitive.IntList;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.set.primitive.MutableIntSet;
import com.gs.collections.impl.bag.mutable.primitive.IntHashBag;
import com.gs.collections.impl.factory.primitive.IntLists;
import com.gs.collections.impl.lazy.primitive.LazyIntIterableAdapter;
import com.gs.collections.impl.lazy.primitive.ReverseIntIterable;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.set.mutable.primitive.IntHashSet;
import net.jcip.annotations.Immutable;
/**
* ImmutableIntArrayList is the non-modifiable equivalent of {@link IntArrayList}.
* It wraps a Java int array.
* This file was automatically generated from template file immutablePrimitiveArrayList.stg.
*
* @since 3.2.
*/
@Immutable
final class ImmutableIntArrayList
implements ImmutableIntList, Serializable
{
private static final long serialVersionUID = 1L;
private final int[] items;
private ImmutableIntArrayList(int[] newElements)
{
if (newElements.length <= 1)
{
throw new IllegalArgumentException("Use IntLists.immutable.with() to instantiate an optimized collection");
}
this.items = newElements;
}
public static ImmutableIntArrayList newList(IntIterable iterable)
{
return new ImmutableIntArrayList(iterable.toArray());
}
public static ImmutableIntArrayList newListWith(int... elements)
{
int[] newArray = new int[elements.length];
System.arraycopy(elements, 0, newArray, 0, elements.length);
return new ImmutableIntArrayList(newArray);
}
public int get(int index)
{
return this.items[index];
}
public int getFirst()
{
return this.items[0];
}
public int getLast()
{
return this.items[this.items.length - 1];
}
public int indexOf(int value)
{
for (int i = 0; i < this.items.length; i++)
{
if (this.items[i] == value)
{
return i;
}
}
return -1;
}
public int lastIndexOf(int value)
{
for (int i = this.items.length - 1; i >= 0; i--)
{
if (this.items[i] == value)
{
return i;
}
}
return -1;
}
public IntIterator intIterator()
{
return new InternalIntIterator();
}
public void forEach(IntProcedure procedure)
{
for (int item : this.items)
{
procedure.value(item);
}
}
public void forEachWithIndex(IntIntProcedure procedure)
{
for (int i = 0; i < this.items.length; i++)
{
procedure.value(this.items[i], i);
}
}
public int count(IntPredicate predicate)
{
int count = 0;
for (int item : this.items)
{
if (predicate.accept(item))
{
count++;
}
}
return count;
}
public boolean anySatisfy(IntPredicate predicate)
{
for (int item : this.items)
{
if (predicate.accept(item))
{
return true;
}
}
return false;
}
public boolean allSatisfy(IntPredicate predicate)
{
for (int item : this.items)
{
if (!predicate.accept(item))
{
return false;
}
}
return true;
}
public boolean noneSatisfy(IntPredicate predicate)
{
for (int item : this.items)
{
if (predicate.accept(item))
{
return false;
}
}
return true;
}
public ImmutableIntList select(IntPredicate predicate)
{
IntArrayList result = new IntArrayList();
for (int item : this.items)
{
if (predicate.accept(item))
{
result.add(item);
}
}
return result.toImmutable();
}
public ImmutableIntList reject(IntPredicate predicate)
{
IntArrayList result = new IntArrayList();
for (int item : this.items)
{
if (!predicate.accept(item))
{
result.add(item);
}
}
return result.toImmutable();
}
public int detectIfNone(IntPredicate predicate, int ifNone)
{
for (int item : this.items)
{
if (predicate.accept(item))
{
return item;
}
}
return ifNone;
}
public <V> ImmutableList<V> collect(IntToObjectFunction<? extends V> function)
{
FastList<V> target = FastList.newList(this.items.length);
for (int item : this.items)
{
target.add(function.valueOf(item));
}
return target.toImmutable();
}
public long sum()
{
long result = 0L;
for (int item : this.items)
{
result += item;
}
return result;
}
public int max()
{
int max = this.items[0];
for (int i = 1; i < this.items.length; i++)
{
int value = this.items[i];
if (max < value)
{
max = value;
}
}
return max;
}
public int maxIfEmpty(int defaultValue)
{
return this.max();
}
public int min()
{
int min = this.items[0];
for (int i = 1; i < this.items.length; i++)
{
int value = this.items[i];
if (value < min)
{
min = value;
}
}
return min;
}
public int minIfEmpty(int defaultValue)
{
return this.min();
}
public double average()
{
return (double) this.sum() / (double) this.size();
}
public double median()
{
int[] sortedArray = this.toSortedArray();
int middleIndex = sortedArray.length >> 1;
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
{
int first = sortedArray[middleIndex];
int second = sortedArray[middleIndex - 1];
return ((double) first + (double) second) / 2.0;
}
return (double) sortedArray[middleIndex];
}
public int[] toSortedArray()
{
int[] array = this.toArray();
Arrays.sort(array);
return array;
}
public long dotProduct(IntList list)
{
if (this.size() != list.size())
{
throw new IllegalArgumentException("Lists used in dotProduct must be the same size");
}
long sum = 0L;
for (int i = 0; i < this.size(); i++)
{
sum += (long) this.items[i] * list.get(i);
}
return sum;
}
public LazyIntIterable asReversed()
{
return ReverseIntIterable.adapt(this);
}
public MutableIntList toSortedList()
{
return IntArrayList.newList(this).sortThis();
}
public int[] toArray()
{
int[] newItems = new int[this.items.length];
System.arraycopy(this.items, 0, newItems, 0, this.items.length);
return newItems;
}
public boolean contains(int value)
{
for (int item : this.items)
{
if (item == value)
{
return true;
}
}
return false;
}
public boolean containsAll(int... source)
{
for (int value : source)
{
if (!this.contains(value))
{
return false;
}
}
return true;
}
public boolean containsAll(IntIterable source)
{
for (IntIterator iterator = source.intIterator(); iterator.hasNext(); )
{
if (!this.contains(iterator.next()))
{
return false;
}
}
return true;
}
public MutableIntList toList()
{
return IntArrayList.newList(this);
}
public MutableIntSet toSet()
{
return IntHashSet.newSet(this);
}
public MutableIntBag toBag()
{
return IntHashBag.newBag(this);
}
public LazyIntIterable asLazy()
{
return new LazyIntIterableAdapter(this);
}
public ImmutableIntList toImmutable()
{
return this;
}
public ImmutableIntArrayList toReversed()
{
return ImmutableIntArrayList.newList(this.asReversed());
}
public ImmutableIntList newWith(int element)
{
int[] newItems = new int[this.items.length + 1];
System.arraycopy(this.items, 0, newItems, 0, this.items.length);
newItems[this.items.length] = element;
return new ImmutableIntArrayList(newItems);
}
public ImmutableIntList newWithout(int element)
{
int index = this.indexOf(element);
if (index != -1)
{
int[] newItems = new int[this.items.length - 1];
System.arraycopy(this.items, 0, newItems, 0, index);
System.arraycopy(this.items, index + 1, newItems, index, this.items.length - index - 1);
return IntLists.immutable.with(newItems);
}
return this;
}
public ImmutableIntList newWithAll(IntIterable elements)
{
int[] newItems = new int[this.items.length + elements.size()];
System.arraycopy(this.items, 0, newItems, 0, this.items.length);
int index = 0;
for (IntIterator iterator = elements.intIterator(); iterator.hasNext(); index++)
{
newItems[this.items.length + index] = iterator.next();
}
return new ImmutableIntArrayList(newItems);
}
public ImmutableIntList newWithoutAll(IntIterable elements)
{
MutableIntList mutableIntList = this.toList();
mutableIntList.removeAll(elements);
return mutableIntList.toImmutable();
}
public int size()
{
return this.items.length;
}
public boolean isEmpty()
{
return false;
}
public boolean notEmpty()
{
return true;
}
public <T> T injectInto(T injectedValue, ObjectIntToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
for (int i = 0; i < this.items.length; i++)
{
result = function.valueOf(result, this.items[i]);
}
return result;
}
public <T> T injectIntoWithIndex(T injectedValue, ObjectIntIntToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
for (int i = 0; i < this.items.length; i++)
{
result = function.valueOf(result, this.items[i], i);
}
return result;
}
@Override
public boolean equals(Object otherList)
{
if (otherList == this)
{
return true;
}
if (!(otherList instanceof IntList))
{
return false;
}
IntList list = (IntList) otherList;
if (this.items.length != list.size())
{
return false;
}
for (int i = 0; i < this.items.length; i++)
{
if (this.items[i] != list.get(i))
{
return false;
}
}
return true;
}
@Override
public int hashCode()
{
int hashCode = 1;
for (int item : this.items)
{
hashCode = 31 * hashCode + item;
}
return hashCode;
}
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
public String makeString()
{
return this.makeString(", ");
}
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
public void appendString(Appendable appendable, String separator)
{
this.appendString(appendable, "", separator, "");
}
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
for (int i = 0; i < this.items.length; i++)
{
if (i > 0)
{
appendable.append(separator);
}
int value = this.items[i];
appendable.append(String.valueOf(value));
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public ImmutableIntList subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException("subList not yet implemented!");
}
private class InternalIntIterator implements IntIterator
{
/**
* Index of element to be returned by subsequent call to next.
*/
private int currentIndex;
public boolean hasNext()
{
return this.currentIndex != ImmutableIntArrayList.this.items.length;
}
public int next()
{
if (!this.hasNext())
{
throw new NoSuchElementException();
}
int next = ImmutableIntArrayList.this.items[this.currentIndex];
this.currentIndex++;
return next;
}
}
}