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

org.eclipse.collections.impl.stack.immutable.ImmutableArrayStack Maven / Gradle / Ivy

/*
 * Copyright (c) 2017 Goldman Sachs and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.stack.immutable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.Optional;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectLongMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.multimap.list.ImmutableListMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.stack.PartitionImmutableStack;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.stack.ImmutableStack;
import org.eclipse.collections.api.stack.MutableStack;
import org.eclipse.collections.api.stack.StackIterable;
import org.eclipse.collections.api.stack.primitive.ImmutableBooleanStack;
import org.eclipse.collections.api.stack.primitive.ImmutableByteStack;
import org.eclipse.collections.api.stack.primitive.ImmutableCharStack;
import org.eclipse.collections.api.stack.primitive.ImmutableDoubleStack;
import org.eclipse.collections.api.stack.primitive.ImmutableFloatStack;
import org.eclipse.collections.api.stack.primitive.ImmutableIntStack;
import org.eclipse.collections.api.stack.primitive.ImmutableLongStack;
import org.eclipse.collections.api.stack.primitive.ImmutableShortStack;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.checked.CheckedProcedure;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.partition.stack.PartitionArrayStack;
import org.eclipse.collections.impl.stack.mutable.ArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.BooleanArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.ByteArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.CharArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.DoubleArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.FloatArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.IntArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.LongArrayStack;
import org.eclipse.collections.impl.stack.mutable.primitive.ShortArrayStack;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
 * The immutable equivalent of ArrayStack. Wraps a FastList.
 */
final class ImmutableArrayStack implements ImmutableStack, Serializable
{
    private static final long serialVersionUID = 1L;
    private final FastList delegate;

    private ImmutableArrayStack(T[] newElements)
    {
        this.delegate = FastList.newListWith(newElements);
    }

    private ImmutableArrayStack(FastList newElements)
    {
        this.delegate = newElements;
    }

    public static  ImmutableStack newStack()
    {
        return new ImmutableArrayStack<>(FastList.newList());
    }

    public static  ImmutableArrayStack newStack(Iterable iterable)
    {
        return new ImmutableArrayStack<>((E[]) Iterate.toArray(iterable));
    }

    public static  ImmutableArrayStack newStackWith(E... elements)
    {
        return new ImmutableArrayStack<>(elements.clone());
    }

    public static  ImmutableArrayStack newStackFromTopToBottom(Iterable items)
    {
        return new ImmutableArrayStack<>(FastList.newList(items).reverseThis());
    }

    public static  ImmutableArrayStack newStackFromTopToBottom(T... items)
    {
        return new ImmutableArrayStack<>(FastList.newListWith(items).reverseThis());
    }

    @Override
    public ImmutableStack push(T item)
    {
        FastList newDelegate = FastList.newList(this.delegate);
        newDelegate.add(item);
        return new ImmutableArrayStack<>(newDelegate);
    }

    @Override
    public ImmutableStack pop()
    {
        this.checkEmptyStack();
        FastList newDelegate = FastList.newList(this.delegate);
        newDelegate.remove(this.delegate.size() - 1);
        return new ImmutableArrayStack<>(newDelegate);
    }

    @Override
    public ImmutableStack pop(int count)
    {
        this.checkNegativeCount(count);
        if (this.checkZeroCount(count))
        {
            return this;
        }
        this.checkEmptyStack();
        this.checkSizeLessThanCount(count);
        FastList newDelegate = this.delegate.clone();
        while (count > 0)
        {
            newDelegate.remove(this.delegate.size() - 1);
            count--;
        }
        return new ImmutableArrayStack<>(newDelegate);
    }

    @Override
    public T peek()
    {
        this.checkEmptyStack();
        return this.delegate.getLast();
    }

    private void checkEmptyStack()
    {
        if (this.delegate.isEmpty())
        {
            throw new EmptyStackException();
        }
    }

    @Override
    public ListIterable peek(int count)
    {
        this.checkNegativeCount(count);
        if (this.checkZeroCount(count))
        {
            return FastList.newList();
        }
        this.checkEmptyStack();
        this.checkSizeLessThanCount(count);
        return FastList.newList(this.asLazy().take(count));
    }

    private boolean checkZeroCount(int count)
    {
        return count == 0;
    }

    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 void checkSizeLessThanOrEqualToIndex(int index)
    {
        if (this.delegate.size() <= index)
        {
            throw new IllegalArgumentException("Count must be less than size: Count = " + index + " Size = " + this.delegate.size());
        }
    }

    private void checkNegativeCount(int count)
    {
        if (count < 0)
        {
            throw new IllegalArgumentException("Count must be positive but was " + count);
        }
    }

    @Override
    public T peekAt(int index)
    {
        this.checkNegativeCount(index);
        this.checkEmptyStack();
        this.checkSizeLessThanOrEqualToIndex(index);
        return this.delegate.get(this.delegate.size() - 1 - index);
    }

    @Override
    public T getFirst()
    {
        return this.peek();
    }

    @Override
    public T getLast()
    {
        throw new UnsupportedOperationException("Cannot call getLast() on " + this.getClass().getSimpleName());
    }

    @Override
    public T getOnly()
    {
        return this.delegate.getOnly();
    }

    @Override
    public boolean contains(Object object)
    {
        return this.delegate.asReversed().contains(object);
    }

    @Override
    public boolean containsAllIterable(Iterable source)
    {
        return this.delegate.asReversed().containsAllIterable(source);
    }

    @Override
    public boolean containsAll(Collection source)
    {
        return this.delegate.asReversed().containsAll(source);
    }

    @Override
    public boolean containsAllArguments(Object... elements)
    {
        return this.delegate.asReversed().containsAllArguments(elements);
    }

    @Override
    public MutableStack toStack()
    {
        return ArrayStack.newStackFromTopToBottom(this);
    }

    @Override
    public ImmutableStack select(Predicate predicate)
    {
        return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().select(predicate).toList());
    }

    @Override
    public 

ImmutableStack selectWith(Predicate2 predicate, P parameter) { return this.select(Predicates.bind(predicate, parameter)); } @Override public > R select(Predicate predicate, R target) { return this.delegate.asReversed().select(predicate, target); } @Override public > R selectWith(Predicate2 predicate, P parameter, R targetCollection) { return this.delegate.asReversed().selectWith(predicate, parameter, targetCollection); } @Override public ImmutableStack reject(Predicate predicate) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().reject(predicate).toList()); } @Override public > R reject(Predicate predicate, R target) { return this.delegate.asReversed().reject(predicate, target); } @Override public

ImmutableStack rejectWith(Predicate2 predicate, P parameter) { return this.reject(Predicates.bind(predicate, parameter)); } @Override public > R rejectWith(Predicate2 predicate, P parameter, R targetCollection) { return this.delegate.asReversed().rejectWith(predicate, parameter, targetCollection); } @Override public PartitionImmutableStack partition(Predicate predicate) { PartitionArrayStack partitionMutableStack = new PartitionArrayStack<>(); this.delegate.asReversed().forEach(new PartitionArrayStack.PartitionProcedure<>(predicate, partitionMutableStack)); return partitionMutableStack.toImmutable(); } @Override public

PartitionImmutableStack partitionWith(Predicate2 predicate, P parameter) { PartitionArrayStack partitionMutableStack = new PartitionArrayStack<>(); this.delegate.asReversed().forEach(new PartitionArrayStack.PartitionPredicate2Procedure<>(predicate, parameter, partitionMutableStack)); return partitionMutableStack.toImmutable(); } @Override public ImmutableStack selectInstancesOf(Class clazz) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().selectInstancesOf(clazz).toList()); } @Override public ImmutableStack collect(Function function) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collect(function)); } @Override public ImmutableBooleanStack collectBoolean(BooleanFunction booleanFunction) { return BooleanArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectBoolean(booleanFunction)).toImmutable(); } @Override public R collectBoolean(BooleanFunction booleanFunction, R target) { return this.delegate.asReversed().collectBoolean(booleanFunction, target); } @Override public ImmutableByteStack collectByte(ByteFunction byteFunction) { return ByteArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectByte(byteFunction)).toImmutable(); } @Override public R collectByte(ByteFunction byteFunction, R target) { return this.delegate.asReversed().collectByte(byteFunction, target); } @Override public ImmutableCharStack collectChar(CharFunction charFunction) { return CharArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectChar(charFunction)).toImmutable(); } @Override public R collectChar(CharFunction charFunction, R target) { return this.delegate.asReversed().collectChar(charFunction, target); } @Override public ImmutableDoubleStack collectDouble(DoubleFunction doubleFunction) { return DoubleArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectDouble(doubleFunction)).toImmutable(); } @Override public R collectDouble(DoubleFunction doubleFunction, R target) { return this.delegate.asReversed().collectDouble(doubleFunction, target); } @Override public ImmutableFloatStack collectFloat(FloatFunction floatFunction) { return FloatArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectFloat(floatFunction)).toImmutable(); } @Override public R collectFloat(FloatFunction floatFunction, R target) { return this.delegate.asReversed().collectFloat(floatFunction, target); } @Override public ImmutableIntStack collectInt(IntFunction intFunction) { return IntArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectInt(intFunction)).toImmutable(); } @Override public R collectInt(IntFunction intFunction, R target) { return this.delegate.asReversed().collectInt(intFunction, target); } @Override public ImmutableLongStack collectLong(LongFunction longFunction) { return LongArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectLong(longFunction)).toImmutable(); } @Override public R collectLong(LongFunction longFunction, R target) { return this.delegate.asReversed().collectLong(longFunction, target); } @Override public ImmutableShortStack collectShort(ShortFunction shortFunction) { return ShortArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectShort(shortFunction)).toImmutable(); } @Override public R collectShort(ShortFunction shortFunction, R target) { return this.delegate.asReversed().collectShort(shortFunction, target); } @Override public > R collect(Function function, R target) { return this.delegate.asReversed().collect(function, target); } @Override public ImmutableStack collectWith(Function2 function, P parameter) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectWith(function, parameter).toList()); } @Override public > R collectWith(Function2 function, P parameter, R targetCollection) { return this.delegate.asReversed().collectWith(function, parameter, targetCollection); } @Override public ImmutableStack collectIf(Predicate predicate, Function function) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectIf(predicate, function).toList()); } @Override public > R collectIf(Predicate predicate, Function function, R target) { return this.delegate.asReversed().collectIf(predicate, function, target); } @Override public ImmutableStack flatCollect(Function> function) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().flatCollect(function).toList()); } @Override public > R flatCollect(Function> function, R target) { return this.delegate.asReversed().flatCollect(function, target); } @Override public T detect(Predicate predicate) { return this.delegate.asReversed().detect(predicate); } @Override public

T detectWith(Predicate2 predicate, P parameter) { return this.delegate.asReversed().detectWith(predicate, parameter); } @Override public Optional detectOptional(Predicate predicate) { return this.delegate.asReversed().detectOptional(predicate); } @Override public

Optional detectWithOptional(Predicate2 predicate, P parameter) { return this.delegate.asReversed().detectWithOptional(predicate, parameter); } @Override public T detectIfNone(Predicate predicate, Function0 function) { return this.delegate.asReversed().detectIfNone(predicate, function); } @Override public

T detectWithIfNone(Predicate2 predicate, P parameter, Function0 function) { return this.delegate.asReversed().detectWithIfNone(predicate, parameter, function); } @Override public int count(Predicate predicate) { return this.delegate.asReversed().count(predicate); } @Override public

int countWith(Predicate2 predicate, P parameter) { return this.delegate.asReversed().countWith(predicate, parameter); } @Override public boolean anySatisfy(Predicate predicate) { return this.delegate.asReversed().anySatisfy(predicate); } @Override public

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

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

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return this.delegate.asReversed().noneSatisfyWith(predicate, parameter); } @Override public IV injectInto(IV injectedValue, Function2 function) { return this.delegate.asReversed().injectInto(injectedValue, function); } @Override public int injectInto(int injectedValue, IntObjectToIntFunction intObjectToIntFunction) { return this.delegate.asReversed().injectInto(injectedValue, intObjectToIntFunction); } @Override public long injectInto(long injectedValue, LongObjectToLongFunction longObjectToLongFunction) { return this.delegate.asReversed().injectInto(injectedValue, longObjectToLongFunction); } @Override public double injectInto(double injectedValue, DoubleObjectToDoubleFunction doubleObjectToDoubleFunction) { return this.delegate.asReversed().injectInto(injectedValue, doubleObjectToDoubleFunction); } @Override public float injectInto(float injectedValue, FloatObjectToFloatFunction floatObjectToFloatFunction) { return this.delegate.asReversed().injectInto(injectedValue, floatObjectToFloatFunction); } @Override public > R into(R target) { return this.delegate.asReversed().into(target); } @Override public MutableList toList() { return this.delegate.asReversed().toList(); } @Override public MutableList toSortedList() { return this.delegate.asReversed().toSortedList(); } @Override public MutableList toSortedList(Comparator comparator) { return this.delegate.asReversed().toSortedList(comparator); } @Override public > MutableList toSortedListBy(Function function) { return this.delegate.asReversed().toSortedListBy(function); } @Override public MutableSet toSet() { return this.delegate.asReversed().toSet(); } @Override public MutableSortedSet toSortedSet() { return this.delegate.asReversed().toSortedSet(); } @Override public MutableSortedSet toSortedSet(Comparator comparator) { return this.delegate.asReversed().toSortedSet(comparator); } @Override public > MutableSortedSet toSortedSetBy(Function function) { return this.delegate.asReversed().toSortedSetBy(function); } @Override public MutableBag toBag() { return this.delegate.asReversed().toBag(); } @Override public MutableSortedBag toSortedBag() { return this.delegate.asReversed().toSortedBag(); } @Override public MutableSortedBag toSortedBag(Comparator comparator) { return this.delegate.asReversed().toSortedBag(comparator); } @Override public > MutableSortedBag toSortedBagBy(Function function) { return this.delegate.asReversed().toSortedBagBy(function); } @Override public MutableMap toMap(Function keyFunction, Function valueFunction) { return this.delegate.asReversed().toMap(keyFunction, valueFunction); } @Override public MutableSortedMap toSortedMap(Function keyFunction, Function valueFunction) { return this.delegate.asReversed().toSortedMap(keyFunction, valueFunction); } @Override public MutableSortedMap toSortedMap(Comparator comparator, Function keyFunction, Function valueFunction) { return this.delegate.asReversed().toSortedMap(comparator, keyFunction, valueFunction); } @Override public LazyIterable asLazy() { return LazyIterate.adapt(this); } @Override public Object[] toArray() { return this.delegate.asReversed().toArray(); } @Override public T[] toArray(T[] a) { return this.delegate.asReversed().toArray(a); } @Override public T min(Comparator comparator) { return this.delegate.asReversed().min(comparator); } @Override public T max(Comparator comparator) { return this.delegate.asReversed().max(comparator); } @Override public T min() { return this.delegate.asReversed().min(); } @Override public T max() { return this.delegate.asReversed().max(); } @Override public > T minBy(Function function) { return this.delegate.asReversed().toList().minBy(function); } @Override public > T maxBy(Function function) { return this.delegate.asReversed().maxBy(function); } @Override public long sumOfInt(IntFunction intFunction) { return this.delegate.asReversed().sumOfInt(intFunction); } @Override public double sumOfFloat(FloatFunction floatFunction) { return this.delegate.asReversed().sumOfFloat(floatFunction); } @Override public long sumOfLong(LongFunction longFunction) { return this.delegate.asReversed().sumOfLong(longFunction); } @Override public double sumOfDouble(DoubleFunction doubleFunction) { return this.delegate.asReversed().sumOfDouble(doubleFunction); } @Override public ImmutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { ObjectLongMap map = this.delegate.asReversed().sumByInt(groupBy, function); return map.toImmutable(); } @Override public ImmutableObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { ObjectDoubleMap map = this.delegate.asReversed().sumByFloat(groupBy, function); return map.toImmutable(); } @Override public ImmutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { ObjectLongMap map = this.delegate.asReversed().sumByLong(groupBy, function); return map.toImmutable(); } @Override public ImmutableObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { ObjectDoubleMap map = this.delegate.asReversed().sumByDouble(groupBy, function); return map.toImmutable(); } @Override public String makeString() { return this.delegate.asReversed().makeString(); } @Override public String makeString(String separator) { return this.delegate.asReversed().makeString(separator); } @Override public String makeString(String start, String separator, String end) { return this.delegate.asReversed().makeString(start, separator, end); } @Override public void appendString(Appendable appendable) { this.delegate.asReversed().appendString(appendable); } @Override public void appendString(Appendable appendable, String separator) { this.delegate.asReversed().appendString(appendable, separator); } @Override public void appendString(Appendable appendable, String start, String separator, String end) { this.delegate.asReversed().appendString(appendable, start, separator, end); } @Override public ImmutableListMultimap groupBy(Function function) { return this.groupBy(function, FastListMultimap.newMultimap()).toImmutable(); } @Override public > R groupBy(Function function, R target) { return this.delegate.asReversed().groupBy(function, target); } @Override public ImmutableListMultimap groupByEach(Function> function) { return this.groupByEach(function, FastListMultimap.newMultimap()).toImmutable(); } @Override public > R groupByEach(Function> function, R target) { return this.delegate.asReversed().groupByEach(function, target); } @Override public ImmutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap()).toImmutable(); } @Override public > R groupByUniqueKey(Function function, R target) { return this.delegate.asReversed().groupByUniqueKey(function, target); } @Override public ImmutableStack> zip(Iterable that) { return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().zip(that).toList()); } @Override public >> R zip(Iterable that, R target) { return this.delegate.asReversed().zip(that, target); } @Override public ImmutableStack> zipWithIndex() { int maxIndex = this.delegate.size() - 1; Interval indices = Interval.fromTo(0, maxIndex); return ImmutableArrayStack.newStackFromTopToBottom(this.delegate.asReversed().zip(indices).toList()); } @Override public >> R zipWithIndex(R target) { return this.delegate.asReversed().zipWithIndex(target); } @Override public ImmutableStack toImmutable() { return this; } @Override public RichIterable> chunk(int size) { return this.delegate.asReversed().chunk(size); } @Override public ImmutableMap aggregateInPlaceBy(Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, mutatingAggregator)); return map.toImmutable(); } @Override public ImmutableMap aggregateBy(Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map.toImmutable(); } @Override public int size() { return this.delegate.size(); } @Override public boolean isEmpty() { return this.delegate.isEmpty(); } @Override public boolean notEmpty() { return this.delegate.notEmpty(); } @Override public ImmutableStack tap(Procedure procedure) { this.forEach(procedure); return this; } @Override public void forEach(Procedure procedure) { this.each(procedure); } @Override public void each(Procedure procedure) { this.delegate.reverseForEach(procedure); } @Override public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) { this.delegate.asReversed().forEachWithIndex(objectIntProcedure); } @Override public

void forEachWith(Procedure2 procedure, P parameter) { this.delegate.asReversed().forEachWith(procedure, parameter); } @Override public ImmutableStack takeWhile(Predicate predicate) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".takeWhile() not implemented yet"); } @Override public ImmutableStack dropWhile(Predicate predicate) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".dropWhile() not implemented yet"); } @Override public PartitionImmutableStack partitionWhile(Predicate predicate) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".partitionWhile() not implemented yet"); } @Override public ImmutableStack distinct() { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".distinct() not implemented yet"); } @Override public int indexOf(Object object) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".indexOf() not implemented yet"); } @Override public boolean corresponds(OrderedIterable other, Predicate2 predicate) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".corresponds() not implemented yet"); } public boolean hasSameElements(OrderedIterable other) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".hasSameElements() not implemented yet"); } @Override public void forEach(int startIndex, int endIndex, Procedure procedure) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".forEach() not implemented yet"); } @Override public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure objectIntProcedure) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".forEachWithIndex() not implemented yet"); } @Override public int detectIndex(Predicate predicate) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".detectIndex() not implemented yet"); } @Override public Iterator iterator() { return this.delegate.asReversed().iterator(); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof StackIterable)) { return false; } StackIterable that = (StackIterable) o; if (that instanceof ImmutableArrayStack) { return this.delegate.equals(((ImmutableArrayStack) that).delegate); } Iterator thisIterator = this.iterator(); Iterator thatIterator = that.iterator(); while (thisIterator.hasNext() && thatIterator.hasNext()) { if (!Comparators.nullSafeEquals(thisIterator.next(), thatIterator.next())) { return false; } } return !thisIterator.hasNext() && !thatIterator.hasNext(); } @Override public String toString() { return this.delegate.asReversed().makeString("[", ", ", "]"); } @Override public int hashCode() { int hashCode = 1; for (T each : this) { hashCode = 31 * hashCode + (each == null ? 0 : each.hashCode()); } return hashCode; } private Object writeReplace() { return new ImmutableStackSerializationProxy<>(this); } private static class ImmutableStackSerializationProxy implements Externalizable { private static final long serialVersionUID = 1L; private StackIterable stack; @SuppressWarnings("UnusedDeclaration") public ImmutableStackSerializationProxy() { // Empty constructor for Externalizable class } protected ImmutableStackSerializationProxy(StackIterable stack) { this.stack = stack; } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.stack.size()); try { this.stack.forEach(new CheckedProcedure() { public void safeValue(T object) throws IOException { out.writeObject(object); } }); } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } throw e; } } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { int size = in.readInt(); FastList deserializedDelegate = new FastList<>(size); for (int i = 0; i < size; i++) { deserializedDelegate.add((T) in.readObject()); } this.stack = ImmutableArrayStack.newStackFromTopToBottom(deserializedDelegate); } protected Object readResolve() { return this.stack; } } }