~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to collections/src/main/java/com/gs/collections/impl/stack/mutable/ArrayStack.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 Goldman Sachs.
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 
 
17
package com.gs.collections.impl.stack.mutable;
 
18
 
 
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;
 
27
 
 
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;
 
99
 
 
100
/**
 
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.
 
105
 */
 
106
public class ArrayStack<T> implements MutableStack<T>, Externalizable
 
107
{
 
108
    private static final long serialVersionUID = 1L;
 
109
    private FastList<T> delegate;
 
110
 
 
111
    public ArrayStack()
 
112
    {
 
113
        this.delegate = FastList.newList();
 
114
    }
 
115
 
 
116
    public ArrayStack(int initialCapacity)
 
117
    {
 
118
        this.delegate = FastList.newList(initialCapacity);
 
119
    }
 
120
 
 
121
    public ArrayStack(Iterable<T> items)
 
122
    {
 
123
        this.delegate = FastList.newList(items);
 
124
    }
 
125
 
 
126
    public ArrayStack(T... items)
 
127
    {
 
128
        this.delegate = FastList.wrapCopy(items);
 
129
    }
 
130
 
 
131
    public static <T> ArrayStack<T> newStack()
 
132
    {
 
133
        return new ArrayStack<T>();
 
134
    }
 
135
 
 
136
    public static <T> ArrayStack<T> newStack(Iterable<? extends T> items)
 
137
    {
 
138
        return new ArrayStack<T>((Iterable<T>) items);
 
139
    }
 
140
 
 
141
    public static <T> ArrayStack<T> newStackWith(T... items)
 
142
    {
 
143
        return new ArrayStack<T>(items);
 
144
    }
 
145
 
 
146
    public static <T> ArrayStack<T> newStackFromTopToBottom(T... items)
 
147
    {
 
148
        ArrayStack<T> stack = new ArrayStack<T>(items.length);
 
149
        for (int i = items.length - 1; i >= 0; i--)
 
150
        {
 
151
            stack.push(items[i]);
 
152
        }
 
153
        return stack;
 
154
    }
 
155
 
 
156
    public static <T> ArrayStack<T> newStackFromTopToBottom(Iterable<? extends T> items)
 
157
    {
 
158
        ArrayStack<T> stack = newStack();
 
159
        stack.delegate = FastList.newList(items).reverseThis();
 
160
        return stack;
 
161
    }
 
162
 
 
163
    public void push(T item)
 
164
    {
 
165
        this.delegate.add(item);
 
166
    }
 
167
 
 
168
    public T pop()
 
169
    {
 
170
        this.checkEmptyStack();
 
171
        return this.delegate.remove(this.delegate.size() - 1);
 
172
    }
 
173
 
 
174
    private void checkEmptyStack()
 
175
    {
 
176
        if (this.delegate.isEmpty())
 
177
        {
 
178
            throw new EmptyStackException();
 
179
        }
 
180
    }
 
181
 
 
182
    public ListIterable<T> pop(int count)
 
183
    {
 
184
        this.checkNegativeCount(count);
 
185
        MutableList<T> result = FastList.newList(count);
 
186
        if (this.checkZeroCount(count))
 
187
        {
 
188
            return result;
 
189
        }
 
190
        this.checkEmptyStack();
 
191
        this.checkSizeLessThanCount(count);
 
192
        while (count > 0)
 
193
        {
 
194
            result.add(this.pop());
 
195
            count--;
 
196
        }
 
197
        return result;
 
198
    }
 
199
 
 
200
    public <R extends Collection<T>> R pop(int count, R targetCollection)
 
201
    {
 
202
        this.checkNegativeCount(count);
 
203
        if (this.checkZeroCount(count))
 
204
        {
 
205
            return targetCollection;
 
206
        }
 
207
        this.checkEmptyStack();
 
208
        this.checkSizeLessThanCount(count);
 
209
        while (count > 0)
 
210
        {
 
211
            targetCollection.add(this.pop());
 
212
            count--;
 
213
        }
 
214
        return targetCollection;
 
215
    }
 
216
 
 
217
    public <R extends MutableStack<T>> R pop(int count, R targetStack)
 
218
    {
 
219
        this.checkNegativeCount(count);
 
220
        if (this.checkZeroCount(count))
 
221
        {
 
222
            return targetStack;
 
223
        }
 
224
        this.checkEmptyStack();
 
225
        this.checkSizeLessThanCount(count);
 
226
        while (count > 0)
 
227
        {
 
228
            targetStack.push(this.pop());
 
229
            count--;
 
230
        }
 
231
        return targetStack;
 
232
    }
 
233
 
 
234
    public void clear()
 
235
    {
 
236
        this.delegate.clear();
 
237
    }
 
238
 
 
239
    private boolean checkZeroCount(int count)
 
240
    {
 
241
        return count == 0;
 
242
    }
 
243
 
 
244
    public T peek()
 
245
    {
 
246
        this.checkEmptyStack();
 
247
        return this.delegate.getLast();
 
248
    }
 
249
 
 
250
    public ListIterable<T> peek(int count)
 
251
    {
 
252
        this.checkNegativeCount(count);
 
253
        if (this.checkZeroCount(count))
 
254
        {
 
255
            return FastList.newList();
 
256
        }
 
257
        this.checkEmptyStack();
 
258
        this.checkSizeLessThanCount(count);
 
259
        return FastList.newList(this.asLazy().take(count));
 
260
    }
 
261
 
 
262
    public T peekAt(int index)
 
263
    {
 
264
        this.checkNegativeCount(index);
 
265
        this.checkEmptyStack();
 
266
        this.checkSizeLessThanOrEqualToIndex(index);
 
267
        return this.delegate.get(this.delegate.size() - 1 - index);
 
268
    }
 
269
 
 
270
    public int size()
 
271
    {
 
272
        return this.delegate.size();
 
273
    }
 
274
 
 
275
    public boolean isEmpty()
 
276
    {
 
277
        return this.delegate.isEmpty();
 
278
    }
 
279
 
 
280
    public boolean notEmpty()
 
281
    {
 
282
        return this.delegate.notEmpty();
 
283
    }
 
284
 
 
285
    public T getFirst()
 
286
    {
 
287
        return this.peek();
 
288
    }
 
289
 
 
290
    public T getLast()
 
291
    {
 
292
        throw new UnsupportedOperationException("Cannot call getLast() on " + this.getClass().getSimpleName());
 
293
    }
 
294
 
 
295
    public boolean contains(Object object)
 
296
    {
 
297
        return this.delegate.asReversed().contains(object);
 
298
    }
 
299
 
 
300
    public boolean containsAllIterable(Iterable<?> source)
 
301
    {
 
302
        return this.delegate.asReversed().containsAllIterable(source);
 
303
    }
 
304
 
 
305
    public boolean containsAll(Collection<?> source)
 
306
    {
 
307
        return this.delegate.asReversed().containsAll(source);
 
308
    }
 
309
 
 
310
    public boolean containsAllArguments(Object... elements)
 
311
    {
 
312
        return this.delegate.asReversed().containsAllArguments(elements);
 
313
    }
 
314
 
 
315
    public <V> ArrayStack<V> collect(Function<? super T, ? extends V> function)
 
316
    {
 
317
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collect(function));
 
318
    }
 
319
 
 
320
    public MutableBooleanStack collectBoolean(final BooleanFunction<? super T> booleanFunction)
 
321
    {
 
322
        final BooleanArrayStack result = new BooleanArrayStack();
 
323
        this.delegate.forEach(new Procedure<T>()
 
324
        {
 
325
            public void value(T each)
 
326
            {
 
327
                result.push(booleanFunction.booleanValueOf(each));
 
328
            }
 
329
        });
 
330
        return result;
 
331
    }
 
332
 
 
333
    public <R extends MutableBooleanCollection> R collectBoolean(BooleanFunction<? super T> booleanFunction, R target)
 
334
    {
 
335
        return this.delegate.collectBoolean(booleanFunction, target);
 
336
    }
 
337
 
 
338
    public MutableByteStack collectByte(final ByteFunction<? super T> byteFunction)
 
339
    {
 
340
        final ByteArrayStack result = new ByteArrayStack();
 
341
        this.delegate.forEach(new Procedure<T>()
 
342
        {
 
343
            public void value(T each)
 
344
            {
 
345
                result.push(byteFunction.byteValueOf(each));
 
346
            }
 
347
        });
 
348
        return result;
 
349
    }
 
350
 
 
351
    public <R extends MutableByteCollection> R collectByte(ByteFunction<? super T> byteFunction, R target)
 
352
    {
 
353
        return this.delegate.collectByte(byteFunction, target);
 
354
    }
 
355
 
 
356
    public MutableCharStack collectChar(final CharFunction<? super T> charFunction)
 
357
    {
 
358
        final CharArrayStack result = new CharArrayStack();
 
359
        this.delegate.forEach(new Procedure<T>()
 
360
        {
 
361
            public void value(T each)
 
362
            {
 
363
                result.push(charFunction.charValueOf(each));
 
364
            }
 
365
        });
 
366
        return result;
 
367
    }
 
368
 
 
369
    public <R extends MutableCharCollection> R collectChar(CharFunction<? super T> charFunction, R target)
 
370
    {
 
371
        return this.delegate.collectChar(charFunction, target);
 
372
    }
 
373
 
 
374
    public MutableDoubleStack collectDouble(final DoubleFunction<? super T> doubleFunction)
 
375
    {
 
376
        final DoubleArrayStack result = new DoubleArrayStack();
 
377
        this.delegate.forEach(new Procedure<T>()
 
378
        {
 
379
            public void value(T each)
 
380
            {
 
381
                result.push(doubleFunction.doubleValueOf(each));
 
382
            }
 
383
        });
 
384
        return result;
 
385
    }
 
386
 
 
387
    public <R extends MutableDoubleCollection> R collectDouble(DoubleFunction<? super T> doubleFunction, R target)
 
388
    {
 
389
        return this.delegate.collectDouble(doubleFunction, target);
 
390
    }
 
391
 
 
392
    public MutableFloatStack collectFloat(final FloatFunction<? super T> floatFunction)
 
393
    {
 
394
        final FloatArrayStack result = new FloatArrayStack();
 
395
        this.delegate.forEach(new Procedure<T>()
 
396
        {
 
397
            public void value(T each)
 
398
            {
 
399
                result.push(floatFunction.floatValueOf(each));
 
400
            }
 
401
        });
 
402
        return result;
 
403
    }
 
404
 
 
405
    public <R extends MutableFloatCollection> R collectFloat(FloatFunction<? super T> floatFunction, R target)
 
406
    {
 
407
        return this.delegate.collectFloat(floatFunction, target);
 
408
    }
 
409
 
 
410
    public MutableIntStack collectInt(final IntFunction<? super T> intFunction)
 
411
    {
 
412
        final IntArrayStack result = new IntArrayStack();
 
413
        this.delegate.forEach(new Procedure<T>()
 
414
        {
 
415
            public void value(T each)
 
416
            {
 
417
                result.push(intFunction.intValueOf(each));
 
418
            }
 
419
        });
 
420
        return result;
 
421
    }
 
422
 
 
423
    public <R extends MutableIntCollection> R collectInt(IntFunction<? super T> intFunction, R target)
 
424
    {
 
425
        return this.delegate.collectInt(intFunction, target);
 
426
    }
 
427
 
 
428
    public MutableLongStack collectLong(final LongFunction<? super T> longFunction)
 
429
    {
 
430
        final LongArrayStack result = new LongArrayStack();
 
431
        this.delegate.forEach(new Procedure<T>()
 
432
        {
 
433
            public void value(T each)
 
434
            {
 
435
                result.push(longFunction.longValueOf(each));
 
436
            }
 
437
        });
 
438
        return result;
 
439
    }
 
440
 
 
441
    public <R extends MutableLongCollection> R collectLong(LongFunction<? super T> longFunction, R target)
 
442
    {
 
443
        return this.delegate.collectLong(longFunction, target);
 
444
    }
 
445
 
 
446
    public MutableShortStack collectShort(final ShortFunction<? super T> shortFunction)
 
447
    {
 
448
        final ShortArrayStack result = new ShortArrayStack();
 
449
        this.delegate.forEach(new Procedure<T>()
 
450
        {
 
451
            public void value(T each)
 
452
            {
 
453
                result.push(shortFunction.shortValueOf(each));
 
454
            }
 
455
        });
 
456
        return result;
 
457
    }
 
458
 
 
459
    public <R extends MutableShortCollection> R collectShort(ShortFunction<? super T> shortFunction, R target)
 
460
    {
 
461
        return this.delegate.collectShort(shortFunction, target);
 
462
    }
 
463
 
 
464
    public <V, R extends Collection<V>> R collect(Function<? super T, ? extends V> function, R target)
 
465
    {
 
466
        return this.delegate.asReversed().collect(function, target);
 
467
    }
 
468
 
 
469
    public <P, V> ArrayStack<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter)
 
470
    {
 
471
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectWith(function, parameter).toList());
 
472
    }
 
473
 
 
474
    public <V> ArrayStack<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function)
 
475
    {
 
476
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().collectIf(predicate, function).toList());
 
477
    }
 
478
 
 
479
    public <V, R extends Collection<V>> R collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function, R target)
 
480
    {
 
481
        return this.delegate.asReversed().collectIf(predicate, function, target);
 
482
    }
 
483
 
 
484
    public <P, V, R extends Collection<V>> R collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter, R targetCollection)
 
485
    {
 
486
        return this.delegate.asReversed().collectWith(function, parameter, targetCollection);
 
487
    }
 
488
 
 
489
    public <V> ArrayStack<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)
 
490
    {
 
491
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().flatCollect(function).toList());
 
492
    }
 
493
 
 
494
    public <V, R extends Collection<V>> R flatCollect(Function<? super T, ? extends Iterable<V>> function, R target)
 
495
    {
 
496
        return this.delegate.asReversed().flatCollect(function, target);
 
497
    }
 
498
 
 
499
    public ArrayStack<T> select(Predicate<? super T> predicate)
 
500
    {
 
501
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().select(predicate).toList());
 
502
    }
 
503
 
 
504
    public <P> ArrayStack<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
505
    {
 
506
        return this.select(Predicates.bind(predicate, parameter));
 
507
    }
 
508
 
 
509
    public <R extends Collection<T>> R select(Predicate<? super T> predicate, R target)
 
510
    {
 
511
        return this.delegate.asReversed().select(predicate, target);
 
512
    }
 
513
 
 
514
    public <S> ArrayStack<S> selectInstancesOf(Class<S> clazz)
 
515
    {
 
516
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().selectInstancesOf(clazz).toList());
 
517
    }
 
518
 
 
519
    public <P, R extends Collection<T>> R selectWith(Predicate2<? super T, ? super P> predicate, P parameter, R targetCollection)
 
520
    {
 
521
        return this.delegate.asReversed().selectWith(predicate, parameter, targetCollection);
 
522
    }
 
523
 
 
524
    public ArrayStack<T> reject(Predicate<? super T> predicate)
 
525
    {
 
526
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().reject(predicate).toList());
 
527
    }
 
528
 
 
529
    public <R extends Collection<T>> R reject(Predicate<? super T> predicate, R target)
 
530
    {
 
531
        return this.delegate.asReversed().reject(predicate, target);
 
532
    }
 
533
 
 
534
    public <P> ArrayStack<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
535
    {
 
536
        return this.reject(Predicates.bind(predicate, parameter));
 
537
    }
 
538
 
 
539
    public <P, R extends Collection<T>> R rejectWith(Predicate2<? super T, ? super P> predicate, P parameter, R targetCollection)
 
540
    {
 
541
        return this.delegate.asReversed().rejectWith(predicate, parameter, targetCollection);
 
542
    }
 
543
 
 
544
    public T detect(Predicate<? super T> predicate)
 
545
    {
 
546
        return this.delegate.asReversed().detect(predicate);
 
547
    }
 
548
 
 
549
    public <P> T detectWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
550
    {
 
551
        return this.delegate.asReversed().detectWith(predicate, parameter);
 
552
    }
 
553
 
 
554
    public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
 
555
    {
 
556
        return this.delegate.asReversed().detectIfNone(predicate, function);
 
557
    }
 
558
 
 
559
    public <P> T detectWithIfNone(Predicate2<? super T, ? super P> predicate, P parameter, Function0<? extends T> function)
 
560
    {
 
561
        return this.delegate.asReversed().detectWithIfNone(predicate, parameter, function);
 
562
    }
 
563
 
 
564
    public PartitionMutableStack<T> partition(Predicate<? super T> predicate)
 
565
    {
 
566
        PartitionArrayStack<T> partitionMutableStack = new PartitionArrayStack<T>();
 
567
        this.delegate.asReversed().forEach(new PartitionArrayStack.PartitionProcedure<T>(predicate, partitionMutableStack));
 
568
        return partitionMutableStack;
 
569
    }
 
570
 
 
571
    public <P> PartitionMutableStack<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
572
    {
 
573
        PartitionArrayStack<T> partitionMutableStack = new PartitionArrayStack<T>();
 
574
        this.delegate.asReversed().forEach(new PartitionArrayStack.PartitionPredicate2Procedure<T, P>(predicate, parameter, partitionMutableStack));
 
575
        return partitionMutableStack;
 
576
    }
 
577
 
 
578
    public <S> ArrayStack<Pair<T, S>> zip(Iterable<S> that)
 
579
    {
 
580
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().zip(that).toList());
 
581
    }
 
582
 
 
583
    public <S, R extends Collection<Pair<T, S>>> R zip(Iterable<S> that, R target)
 
584
    {
 
585
        return this.delegate.asReversed().zip(that, target);
 
586
    }
 
587
 
 
588
    public ArrayStack<Pair<T, Integer>> zipWithIndex()
 
589
    {
 
590
        int maxIndex = this.delegate.size() - 1;
 
591
        Interval indicies = Interval.fromTo(0, maxIndex);
 
592
 
 
593
        return ArrayStack.newStackFromTopToBottom(this.delegate.asReversed().zip(indicies).toList());
 
594
    }
 
595
 
 
596
    public <R extends Collection<Pair<T, Integer>>> R zipWithIndex(R target)
 
597
    {
 
598
        return this.delegate.asReversed().zipWithIndex(target);
 
599
    }
 
600
 
 
601
    public int count(Predicate<? super T> predicate)
 
602
    {
 
603
        return this.delegate.asReversed().count(predicate);
 
604
    }
 
605
 
 
606
    public <P> int countWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
607
    {
 
608
        return this.delegate.asReversed().countWith(predicate, parameter);
 
609
    }
 
610
 
 
611
    public boolean anySatisfy(Predicate<? super T> predicate)
 
612
    {
 
613
        return this.delegate.asReversed().anySatisfy(predicate);
 
614
    }
 
615
 
 
616
    public <P> boolean anySatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
617
    {
 
618
        return this.delegate.asReversed().anySatisfyWith(predicate, parameter);
 
619
    }
 
620
 
 
621
    public boolean allSatisfy(Predicate<? super T> predicate)
 
622
    {
 
623
        return this.delegate.asReversed().allSatisfy(predicate);
 
624
    }
 
625
 
 
626
    public <P> boolean allSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
627
    {
 
628
        return this.delegate.asReversed().allSatisfyWith(predicate, parameter);
 
629
    }
 
630
 
 
631
    public boolean noneSatisfy(Predicate<? super T> predicate)
 
632
    {
 
633
        return this.delegate.asReversed().noneSatisfy(predicate);
 
634
    }
 
635
 
 
636
    public <P> boolean noneSatisfyWith(Predicate2<? super T, ? super P> predicate, P parameter)
 
637
    {
 
638
        return this.delegate.asReversed().noneSatisfyWith(predicate, parameter);
 
639
    }
 
640
 
 
641
    public <IV> IV injectInto(IV injectedValue, Function2<? super IV, ? super T, ? extends IV> function)
 
642
    {
 
643
        return this.delegate.asReversed().injectInto(injectedValue, function);
 
644
    }
 
645
 
 
646
    public int injectInto(int injectedValue, IntObjectToIntFunction<? super T> intObjectToIntFunction)
 
647
    {
 
648
        return this.delegate.asReversed().injectInto(injectedValue, intObjectToIntFunction);
 
649
    }
 
650
 
 
651
    public long injectInto(long injectedValue, LongObjectToLongFunction<? super T> longObjectToLongFunction)
 
652
    {
 
653
        return this.delegate.asReversed().injectInto(injectedValue, longObjectToLongFunction);
 
654
    }
 
655
 
 
656
    public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super T> doubleObjectToDoubleFunction)
 
657
    {
 
658
        return this.delegate.asReversed().injectInto(injectedValue, doubleObjectToDoubleFunction);
 
659
    }
 
660
 
 
661
    public float injectInto(float injectedValue, FloatObjectToFloatFunction<? super T> floatObjectToFloatFunction)
 
662
    {
 
663
        return this.delegate.asReversed().injectInto(injectedValue, floatObjectToFloatFunction);
 
664
    }
 
665
 
 
666
    public long sumOfInt(IntFunction<? super T> intFunction)
 
667
    {
 
668
        return this.delegate.asReversed().sumOfInt(intFunction);
 
669
    }
 
670
 
 
671
    public double sumOfFloat(FloatFunction<? super T> floatFunction)
 
672
    {
 
673
        return this.delegate.asReversed().sumOfFloat(floatFunction);
 
674
    }
 
675
 
 
676
    public long sumOfLong(LongFunction<? super T> longFunction)
 
677
    {
 
678
        return this.delegate.asReversed().sumOfLong(longFunction);
 
679
    }
 
680
 
 
681
    public double sumOfDouble(DoubleFunction<? super T> doubleFunction)
 
682
    {
 
683
        return this.delegate.asReversed().sumOfDouble(doubleFunction);
 
684
    }
 
685
 
 
686
    public T max()
 
687
    {
 
688
        return this.delegate.asReversed().max();
 
689
    }
 
690
 
 
691
    public T max(Comparator<? super T> comparator)
 
692
    {
 
693
        return this.delegate.asReversed().max(comparator);
 
694
    }
 
695
 
 
696
    public <V extends Comparable<? super V>> T maxBy(Function<? super T, ? extends V> function)
 
697
    {
 
698
        return this.delegate.asReversed().maxBy(function);
 
699
    }
 
700
 
 
701
    public T min()
 
702
    {
 
703
        return this.delegate.asReversed().min();
 
704
    }
 
705
 
 
706
    public T min(Comparator<? super T> comparator)
 
707
    {
 
708
        return this.delegate.asReversed().min(comparator);
 
709
    }
 
710
 
 
711
    public <V extends Comparable<? super V>> T minBy(Function<? super T, ? extends V> function)
 
712
    {
 
713
        return this.delegate.asReversed().toList().minBy(function);
 
714
    }
 
715
 
 
716
    public String makeString()
 
717
    {
 
718
        return this.delegate.asReversed().makeString();
 
719
    }
 
720
 
 
721
    public String makeString(String separator)
 
722
    {
 
723
        return this.delegate.asReversed().makeString(separator);
 
724
    }
 
725
 
 
726
    public String makeString(String start, String separator, String end)
 
727
    {
 
728
        return this.delegate.asReversed().makeString(start, separator, end);
 
729
    }
 
730
 
 
731
    public void appendString(Appendable appendable)
 
732
    {
 
733
        this.delegate.asReversed().appendString(appendable);
 
734
    }
 
735
 
 
736
    public void appendString(Appendable appendable, String separator)
 
737
    {
 
738
        this.delegate.asReversed().appendString(appendable, separator);
 
739
    }
 
740
 
 
741
    public void appendString(Appendable appendable, String start, String separator, String end)
 
742
    {
 
743
        this.delegate.asReversed().appendString(appendable, start, separator, end);
 
744
    }
 
745
 
 
746
    public <V> MutableListMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
 
747
    {
 
748
        return this.groupBy(function, FastListMultimap.<V, T>newMultimap());
 
749
    }
 
750
 
 
751
    public <V, R extends MutableMultimap<V, T>> R groupBy(Function<? super T, ? extends V> function, R target)
 
752
    {
 
753
        return this.delegate.asReversed().groupBy(function, target);
 
754
    }
 
755
 
 
756
    public <V> MutableListMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
 
757
    {
 
758
        return this.groupByEach(function, FastListMultimap.<V, T>newMultimap());
 
759
    }
 
760
 
 
761
    public <V, R extends MutableMultimap<V, T>> R groupByEach(Function<? super T, ? extends Iterable<V>> function, R target)
 
762
    {
 
763
        return this.delegate.asReversed().groupByEach(function, target);
 
764
    }
 
765
 
 
766
    public <V> MutableMap<V, T> groupByUniqueKey(Function<? super T, ? extends V> function)
 
767
    {
 
768
        throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet");
 
769
    }
 
770
 
 
771
    public RichIterable<RichIterable<T>> chunk(int size)
 
772
    {
 
773
        return this.delegate.asReversed().chunk(size);
 
774
    }
 
775
 
 
776
    public void forEach(Procedure<? super T> procedure)
 
777
    {
 
778
        this.delegate.reverseForEach(procedure);
 
779
    }
 
780
 
 
781
    public <P> void forEachWith(Procedure2<? super T, ? super P> procedure, P parameter)
 
782
    {
 
783
        this.delegate.asReversed().forEachWith(procedure, parameter);
 
784
    }
 
785
 
 
786
    public void forEachWithIndex(ObjectIntProcedure<? super T> objectIntProcedure)
 
787
    {
 
788
        this.delegate.asReversed().forEachWithIndex(objectIntProcedure);
 
789
    }
 
790
 
 
791
    public MutableList<T> toList()
 
792
    {
 
793
        return this.delegate.asReversed().toList();
 
794
    }
 
795
 
 
796
    public MutableList<T> toSortedList()
 
797
    {
 
798
        return this.delegate.asReversed().toSortedList();
 
799
    }
 
800
 
 
801
    public MutableList<T> toSortedList(Comparator<? super T> comparator)
 
802
    {
 
803
        return this.delegate.asReversed().toSortedList(comparator);
 
804
    }
 
805
 
 
806
    public <V extends Comparable<? super V>> MutableList<T> toSortedListBy(Function<? super T, ? extends V> function)
 
807
    {
 
808
        return this.delegate.asReversed().toSortedListBy(function);
 
809
    }
 
810
 
 
811
    public MutableSet<T> toSet()
 
812
    {
 
813
        return this.delegate.asReversed().toSet();
 
814
    }
 
815
 
 
816
    public MutableSortedSet<T> toSortedSet()
 
817
    {
 
818
        return this.delegate.asReversed().toSortedSet();
 
819
    }
 
820
 
 
821
    public MutableSortedSet<T> toSortedSet(Comparator<? super T> comparator)
 
822
    {
 
823
        return this.delegate.asReversed().toSortedSet(comparator);
 
824
    }
 
825
 
 
826
    public MutableStack<T> toStack()
 
827
    {
 
828
        return ArrayStack.newStackFromTopToBottom(this);
 
829
    }
 
830
 
 
831
    public ImmutableStack<T> toImmutable()
 
832
    {
 
833
        return Stacks.immutable.withAll(this.delegate);
 
834
    }
 
835
 
 
836
    public <V extends Comparable<? super V>> MutableSortedSet<T> toSortedSetBy(Function<? super T, ? extends V> function)
 
837
    {
 
838
        return this.delegate.asReversed().toSortedSetBy(function);
 
839
    }
 
840
 
 
841
    public MutableBag<T> toBag()
 
842
    {
 
843
        return this.delegate.asReversed().toBag();
 
844
    }
 
845
 
 
846
    public <NK, NV> MutableMap<NK, NV> toMap(Function<? super T, ? extends NK> keyFunction, Function<? super T, ? extends NV> valueFunction)
 
847
    {
 
848
        return this.delegate.asReversed().toMap(keyFunction, valueFunction);
 
849
    }
 
850
 
 
851
    public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(Function<? super T, ? extends NK> keyFunction, Function<? super T, ? extends NV> valueFunction)
 
852
    {
 
853
        return this.delegate.asReversed().toSortedMap(keyFunction, valueFunction);
 
854
    }
 
855
 
 
856
    public <NK, NV> MutableSortedMap<NK, NV> toSortedMap(Comparator<? super NK> comparator, Function<? super T, ? extends NK> keyFunction, Function<? super T, ? extends NV> valueFunction)
 
857
    {
 
858
        return this.delegate.asReversed().toSortedMap(comparator, keyFunction, valueFunction);
 
859
    }
 
860
 
 
861
    public LazyIterable<T> asLazy()
 
862
    {
 
863
        return LazyIterate.adapt(this);
 
864
    }
 
865
 
 
866
    public MutableStack<T> asUnmodifiable()
 
867
    {
 
868
        return UnmodifiableStack.of(this);
 
869
    }
 
870
 
 
871
    public MutableStack<T> asSynchronized()
 
872
    {
 
873
        return SynchronizedStack.of(this);
 
874
    }
 
875
 
 
876
    public Object[] toArray()
 
877
    {
 
878
        return this.delegate.asReversed().toArray();
 
879
    }
 
880
 
 
881
    public <T> T[] toArray(T[] a)
 
882
    {
 
883
        return this.delegate.asReversed().toArray(a);
 
884
    }
 
885
 
 
886
    public Iterator<T> iterator()
 
887
    {
 
888
        return this.delegate.asReversed().iterator();
 
889
    }
 
890
 
 
891
    @Override
 
892
    public boolean equals(Object o)
 
893
    {
 
894
        if (this == o)
 
895
        {
 
896
            return true;
 
897
        }
 
898
        if (!(o instanceof StackIterable<?>))
 
899
        {
 
900
            return false;
 
901
        }
 
902
 
 
903
        StackIterable<?> that = (StackIterable<?>) o;
 
904
 
 
905
        if (that instanceof ArrayStack<?>)
 
906
        {
 
907
            return this.delegate.equals(((ArrayStack<?>) that).delegate);
 
908
        }
 
909
        Iterator<T> thisIterator = this.iterator();
 
910
        Iterator<?> thatIterator = that.iterator();
 
911
        while (thisIterator.hasNext() && thatIterator.hasNext())
 
912
        {
 
913
            if (!Comparators.nullSafeEquals(thisIterator.next(), thatIterator.next()))
 
914
            {
 
915
                return false;
 
916
            }
 
917
        }
 
918
        return !thisIterator.hasNext() && !thatIterator.hasNext();
 
919
    }
 
920
 
 
921
    @Override
 
922
    public String toString()
 
923
    {
 
924
        return this.delegate.asReversed().makeString("[", ", ", "]");
 
925
    }
 
926
 
 
927
    @Override
 
928
    public int hashCode()
 
929
    {
 
930
        int hashCode = 1;
 
931
        for (T each : this)
 
932
        {
 
933
            hashCode = 31 * hashCode + (each == null ? 0 : each.hashCode());
 
934
        }
 
935
        return hashCode;
 
936
    }
 
937
 
 
938
    public void writeExternal(ObjectOutput out) throws IOException
 
939
    {
 
940
        out.writeInt(this.size());
 
941
        for (T each : this.delegate.asReversed())
 
942
        {
 
943
            out.writeObject(each);
 
944
        }
 
945
    }
 
946
 
 
947
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
 
948
    {
 
949
        int size = in.readInt();
 
950
        T[] array = (T[]) new Object[size];
 
951
        for (int i = size - 1; i >= 0; i--)
 
952
        {
 
953
            array[i] = (T) in.readObject();
 
954
        }
 
955
        this.delegate = FastList.newListWith(array);
 
956
    }
 
957
 
 
958
    private void checkSizeLessThanCount(int count)
 
959
    {
 
960
        if (this.delegate.size() < count)
 
961
        {
 
962
            throw new IllegalArgumentException("Count must be less than size: Count = " + count + " Size = " + this.delegate.size());
 
963
        }
 
964
    }
 
965
 
 
966
    private void checkSizeLessThanOrEqualToIndex(int index)
 
967
    {
 
968
        if (this.delegate.size() <= index)
 
969
        {
 
970
            throw new IllegalArgumentException("Count must be less than size: Count = " + index + " Size = " + this.delegate.size());
 
971
        }
 
972
    }
 
973
 
 
974
    private void checkNegativeCount(int count)
 
975
    {
 
976
        if (count < 0)
 
977
        {
 
978
            throw new IllegalArgumentException("Count must be positive but was " + count);
 
979
        }
 
980
    }
 
981
 
 
982
    public <K, V> MutableMap<K, V> aggregateInPlaceBy(Function<? super T, ? extends K> groupBy, Function0<? extends V> zeroValueFactory, Procedure2<? super V, ? super T> mutatingAggregator)
 
983
    {
 
984
        MutableMap<K, V> map = UnifiedMap.newMap();
 
985
        this.forEach(new MutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, mutatingAggregator));
 
986
        return map;
 
987
    }
 
988
 
 
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)
 
990
    {
 
991
        MutableMap<K, V> map = UnifiedMap.newMap();
 
992
        this.forEach(new NonMutatingAggregationProcedure<T, K, V>(map, groupBy, zeroValueFactory, nonMutatingAggregator));
 
993
        return map;
 
994
    }
 
995
}