2
* Copyright 2014 Goldman Sachs.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.gs.collections.impl.stack.mutable;
19
import java.io.Externalizable;
20
import java.io.IOException;
21
import java.io.ObjectInput;
22
import java.io.ObjectOutput;
23
import java.util.Collection;
24
import java.util.Comparator;
25
import java.util.EmptyStackException;
26
import java.util.Iterator;
28
import com.gs.collections.api.LazyIterable;
29
import com.gs.collections.api.RichIterable;
30
import com.gs.collections.api.bag.MutableBag;
31
import com.gs.collections.api.block.function.Function;
32
import com.gs.collections.api.block.function.Function0;
33
import com.gs.collections.api.block.function.Function2;
34
import com.gs.collections.api.block.function.primitive.BooleanFunction;
35
import com.gs.collections.api.block.function.primitive.ByteFunction;
36
import com.gs.collections.api.block.function.primitive.CharFunction;
37
import com.gs.collections.api.block.function.primitive.DoubleFunction;
38
import com.gs.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
39
import com.gs.collections.api.block.function.primitive.FloatFunction;
40
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
41
import com.gs.collections.api.block.function.primitive.IntFunction;
42
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
43
import com.gs.collections.api.block.function.primitive.LongFunction;
44
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
45
import com.gs.collections.api.block.function.primitive.ShortFunction;
46
import com.gs.collections.api.block.predicate.Predicate;
47
import com.gs.collections.api.block.predicate.Predicate2;
48
import com.gs.collections.api.block.procedure.Procedure;
49
import com.gs.collections.api.block.procedure.Procedure2;
50
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
51
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
52
import com.gs.collections.api.collection.primitive.MutableByteCollection;
53
import com.gs.collections.api.collection.primitive.MutableCharCollection;
54
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
55
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
56
import com.gs.collections.api.collection.primitive.MutableIntCollection;
57
import com.gs.collections.api.collection.primitive.MutableLongCollection;
58
import com.gs.collections.api.collection.primitive.MutableShortCollection;
59
import com.gs.collections.api.list.ListIterable;
60
import com.gs.collections.api.list.MutableList;
61
import com.gs.collections.api.map.MutableMap;
62
import com.gs.collections.api.map.sorted.MutableSortedMap;
63
import com.gs.collections.api.multimap.MutableMultimap;
64
import com.gs.collections.api.multimap.list.MutableListMultimap;
65
import com.gs.collections.api.partition.stack.PartitionMutableStack;
66
import com.gs.collections.api.set.MutableSet;
67
import com.gs.collections.api.set.sorted.MutableSortedSet;
68
import com.gs.collections.api.stack.ImmutableStack;
69
import com.gs.collections.api.stack.MutableStack;
70
import com.gs.collections.api.stack.StackIterable;
71
import com.gs.collections.api.stack.primitive.MutableBooleanStack;
72
import com.gs.collections.api.stack.primitive.MutableByteStack;
73
import com.gs.collections.api.stack.primitive.MutableCharStack;
74
import com.gs.collections.api.stack.primitive.MutableDoubleStack;
75
import com.gs.collections.api.stack.primitive.MutableFloatStack;
76
import com.gs.collections.api.stack.primitive.MutableIntStack;
77
import com.gs.collections.api.stack.primitive.MutableLongStack;
78
import com.gs.collections.api.stack.primitive.MutableShortStack;
79
import com.gs.collections.api.tuple.Pair;
80
import com.gs.collections.impl.block.factory.Comparators;
81
import com.gs.collections.impl.block.factory.Predicates;
82
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
83
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
84
import com.gs.collections.impl.factory.Stacks;
85
import com.gs.collections.impl.list.Interval;
86
import com.gs.collections.impl.list.mutable.FastList;
87
import com.gs.collections.impl.map.mutable.UnifiedMap;
88
import com.gs.collections.impl.multimap.list.FastListMultimap;
89
import com.gs.collections.impl.partition.stack.PartitionArrayStack;
90
import com.gs.collections.impl.stack.mutable.primitive.BooleanArrayStack;
91
import com.gs.collections.impl.stack.mutable.primitive.ByteArrayStack;
92
import com.gs.collections.impl.stack.mutable.primitive.CharArrayStack;
93
import com.gs.collections.impl.stack.mutable.primitive.DoubleArrayStack;
94
import com.gs.collections.impl.stack.mutable.primitive.FloatArrayStack;
95
import com.gs.collections.impl.stack.mutable.primitive.IntArrayStack;
96
import com.gs.collections.impl.stack.mutable.primitive.LongArrayStack;
97
import com.gs.collections.impl.stack.mutable.primitive.ShortArrayStack;
98
import com.gs.collections.impl.utility.LazyIterate;
101
* ArrayStack is a MutableStack which contains a FastList of data. ArrayStack iterates from top to bottom (LIFO order).
102
* It behaves like FastList in terms of runtime complexity. The method push() is amortized constant time like
103
* FastList.add(). The backing data structure grows and shrinks by 50% at a time, and size is constant. ArrayStack does
104
* not extend Vector, as does the Java Stack, which was one of the reasons to create this data structure.
106
public class ArrayStack<T> implements MutableStack<T>, Externalizable
108
private static final long serialVersionUID = 1L;
109
private FastList<T> delegate;
113
this.delegate = FastList.newList();
116
public ArrayStack(int initialCapacity)
118
this.delegate = FastList.newList(initialCapacity);
121
public ArrayStack(Iterable<T> items)
123
this.delegate = FastList.newList(items);
126
public ArrayStack(T... items)
128
this.delegate = FastList.wrapCopy(items);
131
public static <T> ArrayStack<T> newStack()
133
return new ArrayStack<T>();
136
public static <T> ArrayStack<T> newStack(Iterable<? extends T> items)
138
return new ArrayStack<T>((Iterable<T>) items);
141
public static <T> ArrayStack<T> newStackWith(T... items)
143
return new ArrayStack<T>(items);
146
public static <T> ArrayStack<T> newStackFromTopToBottom(T... items)
148
ArrayStack<T> stack = new ArrayStack<T>(items.length);
149
for (int i = items.length - 1; i >= 0; i--)
151
stack.push(items[i]);
156
public static <T> ArrayStack<T> newStackFromTopToBottom(Iterable<? extends T> items)
158
ArrayStack<T> stack = newStack();
159
stack.delegate = FastList.newList(items).reverseThis();
163
public void push(T item)
165
this.delegate.add(item);
170
this.checkEmptyStack();
171
return this.delegate.remove(this.delegate.size() - 1);
174
private void checkEmptyStack()
176
if (this.delegate.isEmpty())
178
throw new EmptyStackException();
182
public ListIterable<T> pop(int count)
184
this.checkNegativeCount(count);
185
MutableList<T> result = FastList.newList(count);
186
if (this.checkZeroCount(count))
190
this.checkEmptyStack();
191
this.checkSizeLessThanCount(count);
194
result.add(this.pop());
200
public <R extends Collection<T>> R pop(int count, R targetCollection)
202
this.checkNegativeCount(count);
203
if (this.checkZeroCount(count))
205
return targetCollection;
207
this.checkEmptyStack();
208
this.checkSizeLessThanCount(count);
211
targetCollection.add(this.pop());
214
return targetCollection;
217
public <R extends MutableStack<T>> R pop(int count, R targetStack)
219
this.checkNegativeCount(count);
220
if (this.checkZeroCount(count))
224
this.checkEmptyStack();
225
this.checkSizeLessThanCount(count);
228
targetStack.push(this.pop());
236
this.delegate.clear();
239
private boolean checkZeroCount(int count)
246
this.checkEmptyStack();
247
return this.delegate.getLast();
250
public ListIterable<T> peek(int count)
252
this.checkNegativeCount(count);
253
if (this.checkZeroCount(count))
255
return FastList.newList();
257
this.checkEmptyStack();
258
this.checkSizeLessThanCount(count);
259
return FastList.newList(this.asLazy().take(count));
262
public T peekAt(int index)
264
this.checkNegativeCount(index);
265
this.checkEmptyStack();
266
this.checkSizeLessThanOrEqualToIndex(index);
267
return this.delegate.get(this.delegate.size() - 1 - index);
272
return this.delegate.size();
275
public boolean isEmpty()
277
return this.delegate.isEmpty();
280
public boolean notEmpty()
282
return this.delegate.notEmpty();
292
throw new UnsupportedOperationException("Cannot call getLast() on " + this.getClass().getSimpleName());
295
public boolean contains(Object object)
297
return this.delegate.asReversed().contains(object);
300
public boolean containsAllIterable(Iterable<?> source)
302
return this.delegate.asReversed().containsAllIterable(source);
305
public boolean containsAll(Collection<?> source)
307
return this.delegate.asReversed().containsAll(source);
310
public boolean containsAllArguments(Object... elements)
312
return this.delegate.asReversed().containsAllArguments(elements);
315
public <V> ArrayStack<V> collect(Function<? super T, ? extends V> function)
317
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collect(function));
320
public MutableBooleanStack collectBoolean(final BooleanFunction<? super T> booleanFunction)
322
final BooleanArrayStack result = new BooleanArrayStack();
323
this.delegate.forEach(new Procedure<T>()
325
public void value(T each)
327
result.push(booleanFunction.booleanValueOf(each));
333
public <R extends MutableBooleanCollection> R collectBoolean(BooleanFunction<? super T> booleanFunction, R target)
335
return this.delegate.collectBoolean(booleanFunction, target);
338
public MutableByteStack collectByte(final ByteFunction<? super T> byteFunction)
340
final ByteArrayStack result = new ByteArrayStack();
341
this.delegate.forEach(new Procedure<T>()
343
public void value(T each)
345
result.push(byteFunction.byteValueOf(each));
351
public <R extends MutableByteCollection> R collectByte(ByteFunction<? super T> byteFunction, R target)
353
return this.delegate.collectByte(byteFunction, target);
356
public MutableCharStack collectChar(final CharFunction<? super T> charFunction)
358
final CharArrayStack result = new CharArrayStack();
359
this.delegate.forEach(new Procedure<T>()
361
public void value(T each)
363
result.push(charFunction.charValueOf(each));
369
public <R extends MutableCharCollection> R collectChar(CharFunction<? super T> charFunction, R target)
371
return this.delegate.collectChar(charFunction, target);
374
public MutableDoubleStack collectDouble(final DoubleFunction<? super T> doubleFunction)
376
final DoubleArrayStack result = new DoubleArrayStack();
377
this.delegate.forEach(new Procedure<T>()
379
public void value(T each)
381
result.push(doubleFunction.doubleValueOf(each));
387
public <R extends MutableDoubleCollection> R collectDouble(DoubleFunction<? super T> doubleFunction, R target)
389
return this.delegate.collectDouble(doubleFunction, target);
392
public MutableFloatStack collectFloat(final FloatFunction<? super T> floatFunction)
394
final FloatArrayStack result = new FloatArrayStack();
395
this.delegate.forEach(new Procedure<T>()
397
public void value(T each)
399
result.push(floatFunction.floatValueOf(each));
405
public <R extends MutableFloatCollection> R collectFloat(FloatFunction<? super T> floatFunction, R target)
407
return this.delegate.collectFloat(floatFunction, target);
410
public MutableIntStack collectInt(final IntFunction<? super T> intFunction)
412
final IntArrayStack result = new IntArrayStack();
413
this.delegate.forEach(new Procedure<T>()
415
public void value(T each)
417
result.push(intFunction.intValueOf(each));
423
public <R extends MutableIntCollection> R collectInt(IntFunction<? super T> intFunction, R target)
425
return this.delegate.collectInt(intFunction, target);
428
public MutableLongStack collectLong(final LongFunction<? super T> longFunction)
430
final LongArrayStack result = new LongArrayStack();
431
this.delegate.forEach(new Procedure<T>()
433
public void value(T each)
435
result.push(longFunction.longValueOf(each));
441
public <R extends MutableLongCollection> R collectLong(LongFunction<? super T> longFunction, R target)
443
return this.delegate.collectLong(longFunction, target);
446
public MutableShortStack collectShort(final ShortFunction<? super T> shortFunction)
448
final ShortArrayStack result = new ShortArrayStack();
449
this.delegate.forEach(new Procedure<T>()
451
public void value(T each)
453
result.push(shortFunction.shortValueOf(each));
459
public <R extends MutableShortCollection> R collectShort(ShortFunction<? super T> shortFunction, R target)
461
return this.delegate.collectShort(shortFunction, target);
464
public <V, R extends Collection<V>> R collect(Function<? super T, ? extends V> function, R target)
466
return this.delegate.asReversed().collect(function, target);
469
public <P, V> ArrayStack<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter)
471
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectWith(function, parameter).toList());
474
public <V> ArrayStack<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function)
476
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectIf(predicate, function).toList());
479
public <V, R extends Collection<V>> R collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function, R target)
481
return this.delegate.asReversed().collectIf(predicate, function, target);
484
public <P, V, R extends Collection<V>> R collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter, R targetCollection)
486
return this.delegate.asReversed().collectWith(function, parameter, targetCollection);
489
public <V> ArrayStack<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
491
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().flatCollect(function).toList());
494
public <V, R extends Collection<V>> R flatCollect(Function<? super T, ? extends Iterable<V>> function, R target)
496
return this.delegate.asReversed().flatCollect(function, target);
499
public ArrayStack<T> select(Predicate<? super T> predicate)
501
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().select(predicate).toList());
504
public <P> ArrayStack<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
506
return this.select(Predicates.bind(predicate, parameter));
509
public <R extends Collection<T>> R select(Predicate<? super T> predicate, R target)
511
return this.delegate.asReversed().select(predicate, target);
514
public <S> ArrayStack<S> selectInstancesOf(Class<S> clazz)
516
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().selectInstancesOf(clazz).toList());
519
public <P, R extends Collection<T>> R selectWith(Predicate2<? super T, ? super P> predicate, P parameter, R targetCollection)
521
return this.delegate.asReversed().selectWith(predicate, parameter, targetCollection);
524
public ArrayStack<T> reject(Predicate<? super T> predicate)
526
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().reject(predicate).toList());
529
public <R extends Collection<T>> R reject(Predicate<? super T> predicate, R target)
531
return this.delegate.asReversed().reject(predicate, target);
534
public <P> ArrayStack<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
536
return this.reject(Predicates.bind(predicate, parameter));
539
public <P, R extends Collection<T>> R rejectWith(Predicate2<? super T, ? super P> predicate, P parameter, R targetCollection)
541
return this.delegate.asReversed().rejectWith(predicate, parameter, targetCollection);
544
public T detect(Predicate<? super T> predicate)
546
return this.delegate.asReversed().detect(predicate);
549
public <P> T detectWith(Predicate2<? super T, ? super P> predicate, P parameter)
551
return this.delegate.asReversed().detectWith(predicate, parameter);
554
public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
556
return this.delegate.asReversed().detectIfNone(predicate, function);
559
public <P> T detectWithIfNone(Predicate2<? super T, ? super P> predicate, P parameter, Function0<? extends T> function)
561
return this.delegate.asReversed().detectWithIfNone(predicate, parameter, function);
564
public PartitionMutableStack<T> partition(Predicate<? super T> predicate)
566
PartitionArrayStack<T> partitionMutableStack = new PartitionArrayStack<T>();
567
this.delegate.asReversed().forEach(new PartitionArrayStack.PartitionProcedure<T>(predicate, partitionMutableStack));
568
return partitionMutableStack;
571
public <P> PartitionMutableStack<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
573
PartitionArrayStack<T> partitionMutableStack = new PartitionArrayStack<T>();
574
this.delegate.asReversed().forEach(new PartitionArrayStack.PartitionPredicate2Procedure<T, P>(predicate, parameter, partitionMutableStack));
575
return partitionMutableStack;
578
public <S> ArrayStack<Pair<T, S>> zip(Iterable<S> that)
580
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().zip(that).toList());
583
public <S, R extends Collection<Pair<T, S>>> R zip(Iterable<S> that, R target)
585
return this.delegate.asReversed().zip(that, target);
588
public ArrayStack<Pair<T, Integer>> zipWithIndex()
590
int maxIndex = this.delegate.size() - 1;
591
Interval indicies = Interval.fromTo(0, maxIndex);
593
return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().zip(indicies).toList());
596
public <R extends Collection<Pair<T, Integer>>> R zipWithIndex(R target)
598
return this.delegate.asReversed().zipWithIndex(target);
601
public int count(Predicate<? super T> predicate)
603
return this.delegate.asReversed().count(predicate);
606
public <P> int countWith(Predicate2<? super T, ? super P> predicate, P parameter)
608
return this.delegate.asReversed().countWith(predicate, parameter);
611
public boolean anySatisfy(Predicate<? super T> predicate)
613
return this.delegate.asReversed().anySatisfy(predicate);
616
public <P> boolean anySatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
618
return this.delegate.asReversed().anySatisfyWith(predicate, parameter);
621
public boolean allSatisfy(Predicate<? super T> predicate)
623
return this.delegate.asReversed().allSatisfy(predicate);
626
public <P> boolean allSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
628
return this.delegate.asReversed().allSatisfyWith(predicate, parameter);
631
public boolean noneSatisfy(Predicate<? super T> predicate)
633
return this.delegate.asReversed().noneSatisfy(predicate);
636
public <P> boolean noneSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
638
return this.delegate.asReversed().noneSatisfyWith(predicate, parameter);
641
public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
643
return this.delegate.asReversed().injectInto(injectedValue, function);
646
public int injectInto(int injectedValue, IntObjectToIntFunction<? super T> intObjectToIntFunction)
648
return this.delegate.asReversed().injectInto(injectedValue, intObjectToIntFunction);
651
public long injectInto(long injectedValue, LongObjectToLongFunction<? super T> longObjectToLongFunction)
653
return this.delegate.asReversed().injectInto(injectedValue, longObjectToLongFunction);
656
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super T> doubleObjectToDoubleFunction)
658
return this.delegate.asReversed().injectInto(injectedValue, doubleObjectToDoubleFunction);
661
public float injectInto(float injectedValue, FloatObjectToFloatFunction<? super T> floatObjectToFloatFunction)
663
return this.delegate.asReversed().injectInto(injectedValue, floatObjectToFloatFunction);
666
public long sumOfInt(IntFunction<? super T> intFunction)
668
return this.delegate.asReversed().sumOfInt(intFunction);
671
public double sumOfFloat(FloatFunction<? super T> floatFunction)
673
return this.delegate.asReversed().sumOfFloat(floatFunction);
676
public long sumOfLong(LongFunction<? super T> longFunction)
678
return this.delegate.asReversed().sumOfLong(longFunction);
681
public double sumOfDouble(DoubleFunction<? super T> doubleFunction)
683
return this.delegate.asReversed().sumOfDouble(doubleFunction);
688
return this.delegate.asReversed().max();
691
public T max(Comparator<? super T> comparator)
693
return this.delegate.asReversed().max(comparator);
696
public <V extends Comparable<? super V>> T maxBy(Function<? super T, ? extends V> function)
698
return this.delegate.asReversed().maxBy(function);
703
return this.delegate.asReversed().min();
706
public T min(Comparator<? super T> comparator)
708
return this.delegate.asReversed().min(comparator);
711
public <V extends Comparable<? super V>> T minBy(Function<? super T, ? extends V> function)
713
return this.delegate.asReversed().toList().minBy(function);
716
public String makeString()
718
return this.delegate.asReversed().makeString();
721
public String makeString(String separator)
723
return this.delegate.asReversed().makeString(separator);
726
public String makeString(String start, String separator, String end)
728
return this.delegate.asReversed().makeString(start, separator, end);
731
public void appendString(Appendable appendable)
733
this.delegate.asReversed().appendString(appendable);
736
public void appendString(Appendable appendable, String separator)
738
this.delegate.asReversed().appendString(appendable, separator);
741
public void appendString(Appendable appendable, String start, String separator, String end)
743
this.delegate.asReversed().appendString(appendable, start, separator, end);
746
public <V> MutableListMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
748
return this.groupBy(function, FastListMultimap.<V, T>newMultimap());
751
public <V, R extends MutableMultimap<V, T>> R groupBy(Function<? super T, ? extends V> function, R target)
753
return this.delegate.asReversed().groupBy(function, target);
756
public <V> MutableListMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
758
return this.groupByEach(function, FastListMultimap.<V, T>newMultimap());
761
public <V, R extends MutableMultimap<V, T>> R groupByEach(Function<? super T, ? extends Iterable<V>> function, R target)
763
return this.delegate.asReversed().groupByEach(function, target);
766
public <V> MutableMap<V, T> groupByUniqueKey(Function<? super T, ? extends V> function)
768
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet");
771
public RichIterable<RichIterable<T>> chunk(int size)
773
return this.delegate.asReversed().chunk(size);
776
public void forEach(Procedure<? super T> procedure)
778
this.delegate.reverseForEach(procedure);
781
public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parameter)
783
this.delegate.asReversed().forEachWith(procedure, parameter);
786
public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
788
this.delegate.asReversed().forEachWithIndex(objectIntProcedure);
791
public MutableList<T> toList()
793
return this.delegate.asReversed().toList();
796
public MutableList<T> toSortedList()
798
return this.delegate.asReversed().toSortedList();
801
public MutableList<T> toSortedList(Comparator<? super T> comparator)
803
return this.delegate.asReversed().toSortedList(comparator);
806
public <V extends Comparable<? super V>> MutableList<T> toSortedListBy(Function<? super T, ? extends V> function)
808
return this.delegate.asReversed().toSortedListBy(function);
811
public MutableSet<T> toSet()
813
return this.delegate.asReversed().toSet();
816
public MutableSortedSet<T> toSortedSet()
818
return this.delegate.asReversed().toSortedSet();
821
public MutableSortedSet<T> toSortedSet(Comparator<? super T> comparator)
823
return this.delegate.asReversed().toSortedSet(comparator);
826
public MutableStack<T> toStack()
828
return ArrayStack.newStackFromTopToBottom(this);
831
public ImmutableStack<T> toImmutable()
833
return Stacks.immutable.withAll(this.delegate);
836
public <V extends Comparable<? super V>> MutableSortedSet<T> toSortedSetBy(Function<? super T, ? extends V> function)
838
return this.delegate.asReversed().toSortedSetBy(function);
841
public MutableBag<T> toBag()
843
return this.delegate.asReversed().toBag();
846
public <NK, NV> MutableMap<NK, NV> toMap(Function<? super T, ? extends NK> keyFunction, Function<? super T, ? extends NV> valueFunction)
848
return this.delegate.asReversed().toMap(keyFunction, valueFunction);
851
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(Function<? super T, ? extends NK> keyFunction, Function<? super T, ? extends NV> valueFunction)
853
return this.delegate.asReversed().toSortedMap(keyFunction, valueFunction);
856
public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(Comparator<? super NK> comparator, Function<? super T, ? extends NK> keyFunction, Function<? super T, ? extends NV> valueFunction)
858
return this.delegate.asReversed().toSortedMap(comparator, keyFunction, valueFunction);
861
public LazyIterable<T> asLazy()
863
return LazyIterate.adapt(this);
866
public MutableStack<T> asUnmodifiable()
868
return UnmodifiableStack.of(this);
871
public MutableStack<T> asSynchronized()
873
return SynchronizedStack.of(this);
876
public Object[] toArray()
878
return this.delegate.asReversed().toArray();
881
public <T> T[] toArray(T[] a)
883
return this.delegate.asReversed().toArray(a);
886
public Iterator<T> iterator()
888
return this.delegate.asReversed().iterator();
892
public boolean equals(Object o)
898
if (!(o instanceof StackIterable<?>))
903
StackIterable<?> that = (StackIterable<?>) o;
905
if (that instanceof ArrayStack<?>)
907
return this.delegate.equals(((ArrayStack<?>) that).delegate);
909
Iterator<T> thisIterator = this.iterator();
910
Iterator<?> thatIterator = that.iterator();
911
while (thisIterator.hasNext() && thatIterator.hasNext())
913
if (!Comparators.nullSafeEquals(thisIterator.next(), thatIterator.next()))
918
return !thisIterator.hasNext() && !thatIterator.hasNext();
922
public String toString()
924
return this.delegate.asReversed().makeString("[", ", ", "]");
928
public int hashCode()
933
hashCode = 31 * hashCode + (each == null ? 0 : each.hashCode());
938
public void writeExternal(ObjectOutput out) throws IOException
940
out.writeInt(this.size());
941
for (T each : this.delegate.asReversed())
943
out.writeObject(each);
947
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
949
int size = in.readInt();
950
T[] array = (T[]) new Object[size];
951
for (int i = size - 1; i >= 0; i--)
953
array[i] = (T) in.readObject();
955
this.delegate = FastList.newListWith(array);
958
private void checkSizeLessThanCount(int count)
960
if (this.delegate.size() < count)
962
throw new IllegalArgumentException("Count must be less than size: Count = " + count + " Size = " + this.delegate.size());
966
private void checkSizeLessThanOrEqualToIndex(int index)
968
if (this.delegate.size() <= index)
970
throw new IllegalArgumentException("Count must be less than size: Count = " + index + " Size = " + this.delegate.size());
974
private void checkNegativeCount(int count)
978
throw new IllegalArgumentException("Count must be positive but was " + count);
982
public <K, V> MutableMap<K, V> aggregateInPlaceBy(Function<? super T, ? extends K> groupBy, Function0<? extends V> zeroValueFactory, Procedure2<? super V, ? super T> mutatingAggregator)
984
MutableMap<K, V> map = UnifiedMap.newMap();
985
this.forEach(new MutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, mutatingAggregator));
989
public <K, V> MutableMap<K, V> aggregateBy(Function<? super T, ? extends K> groupBy, Function0<? extends V> zeroValueFactory, Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
991
MutableMap<K, V> map = UnifiedMap.newMap();
992
this.forEach(new NonMutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, nonMutatingAggregator));