package org.eclipse.collections.impl.stack.primitive;
import java.util.EmptyStackException;
import org.eclipse.collections.api.LazyShortIterable;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.primitive.ObjectShortIntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectShortToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.ShortPredicate;
import org.eclipse.collections.api.block.procedure.primitive.ShortProcedure;
import org.eclipse.collections.api.block.procedure.primitive.ShortIntProcedure;
import org.eclipse.collections.api.iterator.ShortIterator;
import org.eclipse.collections.api.list.primitive.ShortList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.set.primitive.MutableShortSet;
import org.eclipse.collections.api.stack.primitive.ShortStack;
import org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag;
import org.eclipse.collections.impl.iterator.UnmodifiableShortIterator;
import org.eclipse.collections.impl.lazy.primitive.LazyShortIterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.ShortArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.ShortHashSet;
public abstract class AbstractShortStack implements ShortStack
{
protected abstract ShortArrayList getDelegate();
protected void checkEmptyStack()
{
if (this.getDelegate().isEmpty())
{
throw new EmptyStackException();
}
}
@Override
public short peek()
{
this.checkEmptyStack();
return this.getDelegate().getLast();
}
@Override
public ShortList peek(int count)
{
this.checkPositiveValueForCount(count);
this.checkSizeLessThanCount(count);
if (count == 0)
{
return new ShortArrayList(0);
}
MutableShortList subList = new ShortArrayList(count);
int index = this.getDelegate().size() - 1;
for (int i = 0; i < count; i++)
{
subList.add(this.getDelegate().get(index - i));
}
return subList;
}
@Override
public short peekAt(int index)
{
this.rangeCheck(index);
return this.getDelegate().get(this.getDelegate().size() - 1 - index);
}
protected void rangeCheck(int index)
{
if (index < 0 || index > this.getDelegate().size() - 1)
{
throw new IllegalArgumentException("Index " + index + " out of range.Should be between 0 and " + (this.getDelegate().size() - 1));
}
}
protected void checkPositiveValueForCount(int count)
{
if (count < 0)
{
throw new IllegalArgumentException("Count must be positive but was " + count);
}
}
protected void checkSizeLessThanCount(int count)
{
if (this.getDelegate().size() < count)
{
throw new IllegalArgumentException("Count must be less than size: Count = " + count + " Size = " + this.getDelegate().size());
}
}
@Override
public void forEach(ShortProcedure procedure)
{
this.each(procedure);
}
@Override
public ShortIterator shortIterator()
{
return new UnmodifiableShortIterator(this.getDelegate().asReversed().shortIterator());
}
@Override
public void each(ShortProcedure procedure)
{
this.getDelegate().asReversed().forEach(procedure);
}
@Override
public int count(ShortPredicate predicate)
{
return this.getDelegate().asReversed().count(predicate);
}
@Override
public boolean anySatisfy(ShortPredicate predicate)
{
return this.getDelegate().asReversed().anySatisfy(predicate);
}
@Override
public boolean allSatisfy(ShortPredicate predicate)
{
return this.getDelegate().asReversed().allSatisfy(predicate);
}
@Override
public boolean noneSatisfy(ShortPredicate predicate)
{
return this.getDelegate().asReversed().noneSatisfy(predicate);
}
@Override
public short detectIfNone(ShortPredicate predicate, short ifNone)
{
return this.getDelegate().asReversed().detectIfNone(predicate, ifNone);
}
@Override
public short[] toArray()
{
return this.getDelegate().asReversed().toArray();
}
@Override
public boolean contains(short value)
{
return this.getDelegate().asReversed().contains(value);
}
@Override
public boolean containsAll(short... source)
{
return this.getDelegate().asReversed().containsAll(source);
}
@Override
public boolean containsAll(ShortIterable source)
{
return this.getDelegate().asReversed().containsAll(source);
}
@Override
public MutableShortList toList()
{
return ShortArrayList.newList(this);
}
@Override
public MutableShortSet toSet()
{
return ShortHashSet.newSet(this);
}
@Override
public MutableShortBag toBag()
{
return ShortHashBag.newBag(this);
}
@Override
public <V> V injectInto(V injectedValue, ObjectShortToObjectFunction<? super V, ? extends V> function)
{
return this.getDelegate().toReversed().injectInto(injectedValue, function);
}
@Override
public LazyShortIterable asLazy()
{
return new LazyShortIterableAdapter(this);
}
@Override
public int size()
{
return this.getDelegate().size();
}
@Override
public boolean equals(Object otherStack)
{
if (otherStack == this)
{
return true;
}
if (!(otherStack instanceof ShortStack))
{
return false;
}
ShortStack stack = (ShortStack) 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;
ShortIterable iterable = this.getDelegate().asReversed();
ShortIterator iterator = iterable.shortIterator();
while (iterator.hasNext())
{
short item = iterator.next();
hashCode = 31 * hashCode + (int) item;
}
return hashCode;
}
@Override
public String toString()
{
return this.getDelegate().asReversed().toString();
}
@Override
public String makeString()
{
return this.getDelegate().asReversed().makeString();
}
@Override
public String makeString(String separator)
{
return this.getDelegate().asReversed().makeString(separator);
}
@Override
public String makeString(String start, String separator, String end)
{
return this.getDelegate().asReversed().makeString(start, separator, end);
}
@Override
public void appendString(Appendable appendable)
{
this.getDelegate().asReversed().appendString(appendable);
}
@Override
public void appendString(Appendable appendable, String separator)
{
this.getDelegate().asReversed().appendString(appendable, separator);
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
this.getDelegate().asReversed().appendString(appendable, start, separator, end);
}
@Override
public short getFirst()
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".getFirst() not implemented yet");
}
@Override
public int indexOf(short value)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet");
}
@Override
public <T> T injectIntoWithIndex(T injectedValue, ObjectShortIntToObjectFunction<? super T, ? extends T> function)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".injectIntoWithIndex() not implemented yet");
}
@Override
public void forEachWithIndex(ShortIntProcedure procedure)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".forEachWithIndex() not implemented yet");
}
@Override
public RichIterable<ShortIterable> chunk(int size)
{
return this.getDelegate().asReversed().chunk(size);
}
@Override
public long sum()
{
return this.getDelegate().sum();
}
@Override
public short max()
{
return this.getDelegate().max();
}
@Override
public short min()
{
return this.getDelegate().min();
}
@Override
public short minIfEmpty(short defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.min();
}
@Override
public short maxIfEmpty(short defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.max();
}
@Override
public double average()
{
return this.getDelegate().average();
}
@Override
public double median()
{
return this.getDelegate().median();
}
@Override
public short[] toSortedArray()
{
return this.getDelegate().toSortedArray();
}
}