com.gs.collections.impl.stack.immutable.primitive.ImmutableIntArrayStack Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
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.
/*
* 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.stack.immutable.primitive;
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.LazyIntIterable;
import com.gs.collections.api.IntIterable;
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.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.primitive.IntProcedure;
import com.gs.collections.api.iterator.IntIterator;
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.api.stack.ImmutableStack;
import com.gs.collections.api.stack.primitive.IntStack;
import com.gs.collections.api.stack.primitive.ImmutableIntStack;
import com.gs.collections.impl.bag.mutable.primitive.IntHashBag;
import com.gs.collections.impl.block.procedure.checked.primitive.CheckedIntProcedure;
import com.gs.collections.impl.factory.Stacks;
import com.gs.collections.impl.factory.primitive.IntStacks;
import com.gs.collections.impl.iterator.UnmodifiableIntIterator;
import com.gs.collections.impl.lazy.primitive.LazyIntIterableAdapter;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.set.mutable.primitive.IntHashSet;
import com.gs.collections.impl.stack.mutable.primitive.IntArrayStack;
import net.jcip.annotations.Immutable;
/**
* ImmutableIntArrayStack is the non-modifiable equivalent of {@link IntArrayStack}.
* This file was automatically generated from template file immutablePrimitiveArrayStack.stg.
*
* @since 4.0.
*/
@Immutable
final class ImmutableIntArrayStack
implements ImmutableIntStack, Serializable
{
private static final long serialVersionUID = 1L;
private final IntArrayList delegate;
private ImmutableIntArrayStack(int[] newElements)
{
this.checkOptimizedSize(newElements.length);
this.delegate = new IntArrayList(newElements);
}
private ImmutableIntArrayStack(IntArrayList newElements)
{
this.checkOptimizedSize(newElements.size());
this.delegate = newElements;
}
private void checkOptimizedSize(int length)
{
if (length <= 1)
{
throw new IllegalArgumentException("Use IntStacks.immutable.with() to instantiate an optimized collection");
}
}
public static ImmutableIntArrayStack newStack(IntIterable iterable)
{
return new ImmutableIntArrayStack(iterable.toArray());
}
public static ImmutableIntArrayStack newStackWith(int... elements)
{
int[] newArray = new int[elements.length];
System.arraycopy(elements, 0, newArray, 0, elements.length);
return new ImmutableIntArrayStack(newArray);
}
public static ImmutableIntArrayStack newStackFromTopToBottom(int... items)
{
return new ImmutableIntArrayStack(IntArrayList.newListWith(items).reverseThis());
}
public static ImmutableIntArrayStack newStackFromTopToBottom(IntIterable items)
{
return new ImmutableIntArrayStack(IntArrayList.newList(items).reverseThis());
}
public ImmutableIntStack push(int item)
{
IntArrayList newDelegate = IntArrayList.newList(this.delegate);
newDelegate.add(item);
return new ImmutableIntArrayStack(newDelegate);
}
public ImmutableIntStack pop()
{
IntArrayList newDelegate = IntArrayList.newList(this.delegate);
newDelegate.removeAtIndex(this.delegate.size() - 1);
return IntStacks.immutable.with(newDelegate.toArray());
}
public ImmutableIntStack pop(int count)
{
this.checkNegativeCount(count);
if (count == 0)
{
return this;
}
this.checkSizeLessThanCount(count);
IntArrayList newDelegate = IntArrayList.newList(this.delegate);
while (count > 0)
{
newDelegate.removeAtIndex(newDelegate.size() - 1);
count--;
}
return IntStacks.immutable.with(newDelegate.toArray());
}
private void checkNegativeCount(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be positive but was " + count);
}
}
public int peek()
{
return this.delegate.getLast();
}
public IntList peek(int count)
{
this.checkNegativeCount(count);
this.checkSizeLessThanCount(count);
if (count == 0)
{
return new IntArrayList();
}
MutableIntList subList = new IntArrayList(count);
int index = this.delegate.size() - 1;
for (int i = 0; i < count; i++)
{
subList.add(this.delegate.get(index - i));
}
return subList;
}
public int peekAt(int index)
{
this.rangeCheck(index);
return this.delegate.get(this.delegate.size() - 1 - index);
}
private void rangeCheck(int index)
{
if (index < 0 || index > this.delegate.size() - 1)
{
throw new IllegalArgumentException("Index " + index + " out of range.Should be between 0 and " + (this.delegate.size() - 1));
}
}
public IntIterator intIterator()
{
return new UnmodifiableIntIterator(this.delegate.asReversed().intIterator());
}
public void forEach(IntProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
public void each(IntProcedure procedure)
{
this.delegate.asReversed().forEach(procedure);
}
public int count(IntPredicate predicate)
{
return this.delegate.asReversed().count(predicate);
}
public boolean anySatisfy(IntPredicate predicate)
{
return this.delegate.asReversed().anySatisfy(predicate);
}
public boolean allSatisfy(IntPredicate predicate)
{
return this.delegate.asReversed().allSatisfy(predicate);
}
public boolean noneSatisfy(IntPredicate predicate)
{
return this.delegate.asReversed().noneSatisfy(predicate);
}
public ImmutableIntStack select(IntPredicate predicate)
{
return IntStacks.immutable.withAllReversed(this.delegate.asReversed().select(predicate));
}
public ImmutableIntStack reject(IntPredicate predicate)
{
return IntStacks.immutable.withAllReversed(this.delegate.asReversed().reject(predicate));
}
public int detectIfNone(IntPredicate predicate, int ifNone)
{
return this.delegate.asReversed().detectIfNone(predicate, ifNone);
}
public ImmutableStack collect(IntToObjectFunction extends V> function)
{
return Stacks.immutable.withAllReversed(this.delegate.asReversed().collect(function));
}
public long sum()
{
return this.delegate.sum();
}
public int max()
{
return this.delegate.max();
}
public int maxIfEmpty(int defaultValue)
{
return this.max();
}
public int min()
{
return this.delegate.min();
}
public int minIfEmpty(int defaultValue)
{
return this.min();
}
public double average()
{
return this.delegate.average();
}
public double median()
{
return this.delegate.median();
}
public int[] toSortedArray()
{
return this.delegate.toSortedArray();
}
public MutableIntList toSortedList()
{
return IntArrayList.newList(this).sortThis();
}
public int[] toArray()
{
return this.delegate.asReversed().toArray();
}
public boolean contains(int value)
{
return this.delegate.asReversed().contains(value);
}
public boolean containsAll(int... source)
{
return this.delegate.asReversed().containsAll(source);
}
public boolean containsAll(IntIterable source)
{
return this.delegate.asReversed().containsAll(source);
}
public MutableIntList toList()
{
return IntArrayList.newList(this);
}
public MutableIntSet toSet()
{
return IntHashSet.newSet(this);
}
public MutableIntBag toBag()
{
return IntHashBag.newBag(this);
}
public V injectInto(V injectedValue, ObjectIntToObjectFunction super V, ? extends V> function)
{
return this.delegate.toReversed().injectInto(injectedValue, function);
}
public LazyIntIterable asLazy()
{
return new LazyIntIterableAdapter(this);
}
public ImmutableIntStack toImmutable()
{
return this;
}
public int size()
{
return this.delegate.size();
}
public boolean isEmpty()
{
return false;
}
public boolean notEmpty()
{
return true;
}
@Override
public boolean equals(Object otherStack)
{
if (otherStack == this)
{
return true;
}
if (!(otherStack instanceof IntStack))
{
return false;
}
IntStack stack = (IntStack) otherStack;
if (this.size() != stack.size())
{
return false;
}
for (int i = 0; i < this.size(); i++)
{
if (this.peekAt(i) != stack.peekAt(i))
{
return false;
}
}
return true;
}
@Override
public int hashCode()
{
int hashCode = 1;
IntIterable iterable = this.delegate.asReversed();
IntIterator iterator = iterable.intIterator();
while (iterator.hasNext())
{
int item = iterator.next();
hashCode = 31 * hashCode + item;
}
return hashCode;
}
@Override
public String toString()
{
return this.delegate.asReversed().toString();
}
public String makeString()
{
return this.delegate.asReversed().makeString();
}
public String makeString(String separator)
{
return this.delegate.asReversed().makeString(separator);
}
public String makeString(String start, String separator, String end)
{
return this.delegate.asReversed().makeString(start, separator, end);
}
public void appendString(Appendable appendable)
{
this.delegate.asReversed().appendString(appendable);
}
public void appendString(Appendable appendable, String separator)
{
this.delegate.asReversed().appendString(appendable, separator);
}
public void appendString(Appendable appendable, String start, String separator, String end)
{
this.delegate.asReversed().appendString(appendable, start, separator, end);
}
private void checkSizeLessThanCount(int count)
{
if (this.delegate.size() < count)
{
throw new IllegalArgumentException("Count must be less than size: Count = " + count + " Size = " + this.delegate.size());
}
}
private Object writeReplace()
{
return new ImmutableIntStackSerializationProxy(this);
}
private static class ImmutableIntStackSerializationProxy implements Externalizable
{
private static final long serialVersionUID = 1L;
private IntStack stack;
@SuppressWarnings("UnusedDeclaration")
public ImmutableIntStackSerializationProxy()
{
// Empty constructor for Externalizable class
}
protected ImmutableIntStackSerializationProxy(IntStack stack)
{
this.stack = stack;
}
public void writeExternal(final ObjectOutput out) throws IOException
{
out.writeInt(this.stack.size());
try
{
this.stack.forEach(new CheckedIntProcedure()
{
@Override
public void safeValue(int item) throws IOException
{
out.writeInt(item);
}
});
}
catch (RuntimeException e)
{
if (e.getCause() instanceof IOException)
{
throw (IOException) e.getCause();
}
throw e;
}
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
int size = in.readInt();
IntArrayList deserializedDelegate = new IntArrayList(size);
for (int i = 0; i < size; i++)
{
deserializedDelegate.add(in.readInt());
}
this.stack = ImmutableIntArrayStack.newStackFromTopToBottom(deserializedDelegate);
}
protected Object readResolve()
{
return this.stack;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy