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

« back to all changes in this revision

Viewing changes to gs-collections-code-generator/src/main/resources/impl/list/mutable/primitiveArrayList.stg

  • 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
import "copyright.stg"
 
2
import "primitiveEquals.stg"
 
3
import "primitiveHashCode.stg"
 
4
import "primitiveLiteral.stg"
 
5
 
 
6
targetPath() ::= "com/gs/collections/impl/list/mutable/primitive"
 
7
 
 
8
fileName(primitive) ::= "<primitive.name>ArrayList"
 
9
 
 
10
class(primitive) ::= <<
 
11
<body(primitive.type, primitive.name)>
 
12
>>
 
13
 
 
14
body(type, name) ::= <<
 
15
<copyright()>
 
16
 
 
17
package com.gs.collections.impl.list.mutable.primitive;
 
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.Arrays;
 
24
import java.util.NoSuchElementException;
 
25
 
 
26
import com.gs.collections.api.<name>Iterable;
 
27
import com.gs.collections.api.Lazy<name>Iterable;
 
28
import com.gs.collections.api.RichIterable;
 
29
import com.gs.collections.api.bag.primitive.Mutable<name>Bag;
 
30
import com.gs.collections.api.block.function.primitive.Object<name>IntToObjectFunction;
 
31
import com.gs.collections.api.block.function.primitive.Object<name>ToObjectFunction;
 
32
import com.gs.collections.api.block.function.primitive.<name>ToObjectFunction;
 
33
import com.gs.collections.api.block.predicate.primitive.<name>Predicate;
 
34
import com.gs.collections.api.block.procedure.primitive.<name>IntProcedure;
 
35
import com.gs.collections.api.block.procedure.primitive.<name>Procedure;
 
36
import com.gs.collections.api.iterator.<name>Iterator;
 
37
import com.gs.collections.api.list.MutableList;
 
38
import com.gs.collections.api.list.primitive.<name>List;
 
39
import com.gs.collections.api.list.primitive.Immutable<name>List;
 
40
import com.gs.collections.api.list.primitive.Mutable<name>List;
 
41
import com.gs.collections.api.set.primitive.<name>Set;
 
42
import com.gs.collections.api.set.primitive.Mutable<name>Set;
 
43
import com.gs.collections.impl.bag.mutable.primitive.<name>HashBag;
 
44
import com.gs.collections.impl.factory.primitive.<name>Lists;
 
45
import com.gs.collections.impl.lazy.primitive.Lazy<name>IterableAdapter;
 
46
import com.gs.collections.impl.lazy.primitive.Reverse<name>Iterable;
 
47
import com.gs.collections.impl.list.mutable.FastList;
 
48
import com.gs.collections.impl.set.mutable.primitive.<name>HashSet;
 
49
import net.jcip.annotations.NotThreadSafe;
 
50
 
 
51
/**
 
52
 * <name>ArrayList is similar to {@link FastList}, and is memory-optimized for <type> primitives.
 
53
 * This file was automatically generated from template file primitiveArrayList.stg.
 
54
 *
 
55
 * @since 3.0.
 
56
 */
 
57
@NotThreadSafe
 
58
public final class <name>ArrayList
 
59
        implements Mutable<name>List, Externalizable
 
60
{
 
61
    private static final long serialVersionUID = 1L;
 
62
    private static final <type>[] DEFAULT_SIZED_EMPTY_ARRAY = {};
 
63
    private static final <type>[] ZERO_SIZED_ARRAY = {};
 
64
    private static final int MAXIMUM_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 
65
    private int size;
 
66
    private transient <type>[] items = DEFAULT_SIZED_EMPTY_ARRAY;
 
67
 
 
68
    public <name>ArrayList()
 
69
    {
 
70
    }
 
71
 
 
72
    public <name>ArrayList(int initialCapacity)
 
73
    {
 
74
        this.items = initialCapacity == 0 ? ZERO_SIZED_ARRAY : new <type>[initialCapacity];
 
75
    }
 
76
 
 
77
    public <name>ArrayList(<type>... array)
 
78
    {
 
79
        this.size = array.length;
 
80
        this.items = array;
 
81
    }
 
82
 
 
83
    /**
 
84
     * Creates a new list using the passed {@code elements} argument as the backing store.
 
85
     * \<p/>
 
86
     * !!! WARNING: This method uses the passed in array, so can be very unsafe if the original
 
87
     * array is held onto anywhere else. !!!
 
88
     */
 
89
    public static <name>ArrayList newListWith(<type>... elements)
 
90
    {
 
91
        return new <name>ArrayList(elements);
 
92
    }
 
93
 
 
94
    public static <name>ArrayList newList(<name>Iterable source)
 
95
    {
 
96
        return <name>ArrayList.newListWith(source.toArray());
 
97
    }
 
98
 
 
99
    public static <name>ArrayList newWithNValues(int size, <type> value)
 
100
    {
 
101
        <name>ArrayList newList = new <name>ArrayList(size);
 
102
        for (int i = 0; i \< size; i++)
 
103
        {
 
104
            newList.add(value);
 
105
        }
 
106
        return newList;
 
107
    }
 
108
 
 
109
    public int size()
 
110
    {
 
111
        return this.size;
 
112
    }
 
113
 
 
114
    public boolean isEmpty()
 
115
    {
 
116
        return this.size == 0;
 
117
    }
 
118
 
 
119
    public boolean notEmpty()
 
120
    {
 
121
        return this.size > 0;
 
122
    }
 
123
 
 
124
    public void clear()
 
125
    {
 
126
        Arrays.fill(this.items, <(zero.(type))>);
 
127
        this.size = 0;
 
128
    }
 
129
 
 
130
    public boolean contains(<type> value)
 
131
    {
 
132
        for (int i = 0; i \< this.size; i++)
 
133
        {
 
134
            if (<(equals.(type))("this.items[i]", "value")>)
 
135
            {
 
136
                return true;
 
137
            }
 
138
        }
 
139
        return false;
 
140
    }
 
141
 
 
142
    public boolean containsAll(<type>... source)
 
143
    {
 
144
        for (<type> value : source)
 
145
        {
 
146
            if (!this.contains(value))
 
147
            {
 
148
                return false;
 
149
            }
 
150
        }
 
151
        return true;
 
152
    }
 
153
 
 
154
    public boolean containsAll(<name>Iterable source)
 
155
    {
 
156
        for (<name>Iterator iterator = source.<type>Iterator(); iterator.hasNext(); )
 
157
        {
 
158
            if (!this.contains(iterator.next()))
 
159
            {
 
160
                return false;
 
161
            }
 
162
        }
 
163
        return true;
 
164
    }
 
165
 
 
166
    public <type> get(int index)
 
167
    {
 
168
        if (index \< this.size)
 
169
        {
 
170
            return this.items[index];
 
171
        }
 
172
        throw this.newIndexOutOfBoundsException(index);
 
173
    }
 
174
 
 
175
    private IndexOutOfBoundsException newIndexOutOfBoundsException(int index)
 
176
    {
 
177
        return new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size);
 
178
    }
 
179
 
 
180
    public <type> getFirst()
 
181
    {
 
182
        this.checkEmpty();
 
183
        return this.items[0];
 
184
    }
 
185
 
 
186
    public <type> getLast()
 
187
    {
 
188
        this.checkEmpty();
 
189
        return this.items[this.size() - 1];
 
190
    }
 
191
 
 
192
    private void checkEmpty()
 
193
    {
 
194
        if (this.isEmpty())
 
195
        {
 
196
            throw this.newIndexOutOfBoundsException(0);
 
197
        }
 
198
    }
 
199
 
 
200
    public int indexOf(<type> value)
 
201
    {
 
202
        for (int i = 0; i \< this.size; i++)
 
203
        {
 
204
            if (<(equals.(type))("this.items[i]", "value")>)
 
205
            {
 
206
                return i;
 
207
            }
 
208
        }
 
209
        return -1;
 
210
    }
 
211
 
 
212
    public int lastIndexOf(<type> value)
 
213
    {
 
214
        for (int i = this.size - 1; i >= 0; i--)
 
215
        {
 
216
            if (<(equals.(type))("this.items[i]", "value")>)
 
217
            {
 
218
                return i;
 
219
            }
 
220
        }
 
221
        return -1;
 
222
    }
 
223
 
 
224
    public void trimToSize()
 
225
    {
 
226
        if (this.size \< this.items.length)
 
227
        {
 
228
            this.transferItemsToNewArrayWithCapacity(this.size);
 
229
        }
 
230
    }
 
231
 
 
232
    private void transferItemsToNewArrayWithCapacity(int newCapacity)
 
233
    {
 
234
        this.items = this.copyItemsWithNewCapacity(newCapacity);
 
235
    }
 
236
 
 
237
    private <type>[] copyItemsWithNewCapacity(int newCapacity)
 
238
    {
 
239
        <type>[] newItems = new <type>[newCapacity];
 
240
        System.arraycopy(this.items, 0, newItems, 0, Math.min(this.size, newCapacity));
 
241
        return newItems;
 
242
    }
 
243
 
 
244
    private int sizePlusFiftyPercent(int oldSize)
 
245
    {
 
246
        int result = oldSize + (oldSize >\> 1) + 1;
 
247
        return result \< oldSize ? MAXIMUM_ARRAY_SIZE : result;
 
248
    }
 
249
 
 
250
    public void ensureCapacity(int minCapacity)
 
251
    {
 
252
        int oldCapacity = this.items.length;
 
253
        if (minCapacity > oldCapacity)
 
254
        {
 
255
            int newCapacity = Math.max(this.sizePlusFiftyPercent(oldCapacity), minCapacity);
 
256
            this.transferItemsToNewArrayWithCapacity(newCapacity);
 
257
        }
 
258
    }
 
259
 
 
260
    private void ensureCapacityForAdd()
 
261
    {
 
262
        if (this.items == DEFAULT_SIZED_EMPTY_ARRAY)
 
263
        {
 
264
            this.items = new <type>[10];
 
265
        }
 
266
        else
 
267
        {
 
268
            this.transferItemsToNewArrayWithCapacity(this.sizePlusFiftyPercent(this.size));
 
269
        }
 
270
    }
 
271
 
 
272
    public boolean add(<type> newItem)
 
273
    {
 
274
        if (this.items.length == this.size)
 
275
        {
 
276
            this.ensureCapacityForAdd();
 
277
        }
 
278
        this.items[this.size] = newItem;
 
279
        this.size++;
 
280
        return true;
 
281
    }
 
282
 
 
283
    public boolean addAll(<type>... source)
 
284
    {
 
285
        if (source.length \< 1)
 
286
        {
 
287
            return false;
 
288
        }
 
289
        int sourceSize = source.length;
 
290
        int newSize = this.size + sourceSize;
 
291
        this.ensureCapacity(newSize);
 
292
        System.arraycopy(source, 0, this.items, this.size, sourceSize);
 
293
        this.size = newSize;
 
294
        return true;
 
295
    }
 
296
 
 
297
    public boolean addAll(<name>Iterable source)
 
298
    {
 
299
        return this.addAll(source.toArray());
 
300
    }
 
301
 
 
302
    private void throwOutOfBounds(int index)
 
303
    {
 
304
        throw this.newIndexOutOfBoundsException(index);
 
305
    }
 
306
 
 
307
    public void addAtIndex(int index, <type> element)
 
308
    {
 
309
        if (index > -1 && index \< this.size)
 
310
        {
 
311
            this.addAtIndexLessThanSize(index, element);
 
312
        }
 
313
        else if (index == this.size)
 
314
        {
 
315
            this.add(element);
 
316
        }
 
317
        else
 
318
        {
 
319
            this.throwOutOfBounds(index);
 
320
        }
 
321
    }
 
322
 
 
323
    private void addAtIndexLessThanSize(int index, <type> element)
 
324
    {
 
325
        int oldSize = this.size;
 
326
        this.size++;
 
327
        if (this.items.length == oldSize)
 
328
        {
 
329
            <type>[] newItems = new <type>[this.sizePlusFiftyPercent(oldSize)];
 
330
            if (index > 0)
 
331
            {
 
332
                System.arraycopy(this.items, 0, newItems, 0, index);
 
333
            }
 
334
            System.arraycopy(this.items, index, newItems, index + 1, oldSize - index);
 
335
            this.items = newItems;
 
336
        }
 
337
        else
 
338
        {
 
339
            System.arraycopy(this.items, index, this.items, index + 1, oldSize - index);
 
340
        }
 
341
        this.items[index] = element;
 
342
    }
 
343
 
 
344
    public boolean addAllAtIndex(int index, <type>... source)
 
345
    {
 
346
        if (index > this.size || index \< 0)
 
347
        {
 
348
            this.throwOutOfBounds(index);
 
349
        }
 
350
        if (source.length == 0)
 
351
        {
 
352
            return false;
 
353
        }
 
354
        int sourceSize = source.length;
 
355
        int newSize = this.size + sourceSize;
 
356
        this.ensureCapacity(newSize);
 
357
        this.shiftElementsAtIndex(index, sourceSize);
 
358
        System.arraycopy(source, 0, this.items, index, sourceSize);
 
359
        this.size = newSize;
 
360
        return true;
 
361
    }
 
362
 
 
363
    public boolean addAllAtIndex(int index, <name>Iterable source)
 
364
    {
 
365
        return this.addAllAtIndex(index, source.toArray());
 
366
    }
 
367
 
 
368
    private void shiftElementsAtIndex(int index, int sourceSize)
 
369
    {
 
370
        int numberToMove = this.size - index;
 
371
        if (numberToMove > 0)
 
372
        {
 
373
            System.arraycopy(this.items, index, this.items, index + sourceSize, numberToMove);
 
374
        }
 
375
    }
 
376
 
 
377
    public boolean remove(<type> value)
 
378
    {
 
379
        int index = this.indexOf(value);
 
380
        if (index >= 0)
 
381
        {
 
382
            this.removeAtIndex(index);
 
383
            return true;
 
384
        }
 
385
        return false;
 
386
    }
 
387
 
 
388
    public boolean removeAll(<name>Iterable source)
 
389
    {
 
390
        boolean modified = false;
 
391
        for (int index = 0; index \< this.size; index++)
 
392
        {
 
393
            if (source.contains(this.get(index)))
 
394
            {
 
395
                this.removeAtIndex(index);
 
396
                index--;
 
397
                modified = true;
 
398
            }
 
399
        }
 
400
        return modified;
 
401
    }
 
402
 
 
403
    public boolean removeAll(<type>... source)
 
404
    {
 
405
        <name>HashSet set = <name>HashSet.newSetWith(source);
 
406
        <type>[] newItems = new <type>[this.size];
 
407
        int count = 0;
 
408
        int oldSize = this.size;
 
409
        for (int index = 0; index \< this.size; index++)
 
410
        {
 
411
            if (!set.contains(this.items[index]))
 
412
            {
 
413
                newItems[count] = this.items[index];
 
414
                count++;
 
415
            }
 
416
        }
 
417
        this.items = newItems;
 
418
        this.size = count;
 
419
        return oldSize != this.size;
 
420
    }
 
421
 
 
422
    public boolean retainAll(<name>Iterable source)
 
423
    {
 
424
        int oldSize = this.size();
 
425
        final <name>Set sourceSet = source instanceof <name>Set ? (<name>Set) source : source.toSet();
 
426
        <name>ArrayList retained = this.select(new <name>Predicate()
 
427
        {
 
428
            public boolean accept(<type> value)
 
429
            {
 
430
                return sourceSet.contains(value);
 
431
            }
 
432
        });
 
433
        this.size = retained.size;
 
434
        this.items = retained.items;
 
435
        return oldSize != this.size();
 
436
    }
 
437
 
 
438
    public boolean retainAll(<type>... source)
 
439
    {
 
440
        return this.retainAll(<name>HashSet.newSetWith(source));
 
441
    }
 
442
 
 
443
    public <type> removeAtIndex(int index)
 
444
    {
 
445
        <type> previous = this.get(index);
 
446
        int totalOffset = this.size - index - 1;
 
447
        if (totalOffset > 0)
 
448
        {
 
449
            System.arraycopy(this.items, index + 1, this.items, index, totalOffset);
 
450
        }
 
451
        --this.size;
 
452
        this.items[this.size] = <zero.(type)>;
 
453
        return previous;
 
454
    }
 
455
 
 
456
    public <type> set(int index, <type> element)
 
457
    {
 
458
        <type> previous = this.get(index);
 
459
        this.items[index] = element;
 
460
        return previous;
 
461
    }
 
462
 
 
463
    public <name>ArrayList with(<type> element)
 
464
    {
 
465
        this.add(element);
 
466
        return this;
 
467
    }
 
468
 
 
469
    public <name>ArrayList without(<type> element)
 
470
    {
 
471
        this.remove(element);
 
472
        return this;
 
473
    }
 
474
 
 
475
    public <name>ArrayList withAll(<name>Iterable elements)
 
476
    {
 
477
        this.addAll(elements.toArray());
 
478
        return this;
 
479
    }
 
480
 
 
481
    public <name>ArrayList withoutAll(<name>Iterable elements)
 
482
    {
 
483
        this.removeAll(elements);
 
484
        return this;
 
485
    }
 
486
 
 
487
    public <name>ArrayList with(<type> element1, <type> element2)
 
488
    {
 
489
        this.add(element1);
 
490
        this.add(element2);
 
491
        return this;
 
492
    }
 
493
 
 
494
    public <name>ArrayList with(<type> element1, <type> element2, <type> element3)
 
495
    {
 
496
        this.add(element1);
 
497
        this.add(element2);
 
498
        this.add(element3);
 
499
        return this;
 
500
    }
 
501
 
 
502
    public <name>ArrayList with(<type> element1, <type> element2, <type> element3, <type>... elements)
 
503
    {
 
504
        this.add(element1);
 
505
        this.add(element2);
 
506
        this.add(element3);
 
507
        return this.withArrayCopy(elements, 0, elements.length);
 
508
    }
 
509
 
 
510
    private <name>ArrayList withArrayCopy(<type>[] elements, int begin, int length)
 
511
    {
 
512
        this.ensureCapacity(this.size + length);
 
513
        System.arraycopy(elements, begin, this.items, this.size, length);
 
514
        this.size += length;
 
515
        return this;
 
516
    }
 
517
 
 
518
    public <name>Iterator <type>Iterator()
 
519
    {
 
520
        return new Internal<name>Iterator();
 
521
    }
 
522
 
 
523
    public void forEach(<name>Procedure procedure)
 
524
    {
 
525
        for (int i = 0; i \< this.size; i++)
 
526
        {
 
527
            procedure.value(this.items[i]);
 
528
        }
 
529
    }
 
530
 
 
531
    public void forEachWithIndex(<name>IntProcedure procedure)
 
532
    {
 
533
        for (int i = 0; i \< this.size; i++)
 
534
        {
 
535
            procedure.value(this.items[i], i);
 
536
        }
 
537
    }
 
538
 
 
539
    public \<T> T injectInto(T injectedValue, Object<name>ToObjectFunction\<? super T, ? extends T> function)
 
540
    {
 
541
        T result = injectedValue;
 
542
        for (int i = 0; i \< this.size; i++)
 
543
        {
 
544
            result = function.valueOf(result, this.items[i]);
 
545
        }
 
546
        return result;
 
547
    }
 
548
 
 
549
    public \<T> T injectIntoWithIndex(T injectedValue, Object<name>IntToObjectFunction\<? super T, ? extends T> function)
 
550
    {
 
551
        T result = injectedValue;
 
552
        for (int i = 0; i \< this.size; i++)
 
553
        {
 
554
            result = function.valueOf(result, this.items[i], i);
 
555
        }
 
556
        return result;
 
557
    }
 
558
 
 
559
    public int count(<name>Predicate predicate)
 
560
    {
 
561
        int count = 0;
 
562
        for (int i = 0; i \< this.size; i++)
 
563
        {
 
564
            if (predicate.accept(this.items[i]))
 
565
            {
 
566
                count++;
 
567
            }
 
568
        }
 
569
        return count;
 
570
    }
 
571
 
 
572
    public boolean anySatisfy(<name>Predicate predicate)
 
573
    {
 
574
        for (int i = 0; i \< this.size; i++)
 
575
        {
 
576
            if (predicate.accept(this.items[i]))
 
577
            {
 
578
                return true;
 
579
            }
 
580
        }
 
581
        return false;
 
582
    }
 
583
 
 
584
    public boolean allSatisfy(<name>Predicate predicate)
 
585
    {
 
586
        for (int i = 0; i \< this.size; i++)
 
587
        {
 
588
            if (!predicate.accept(this.items[i]))
 
589
            {
 
590
                return false;
 
591
            }
 
592
        }
 
593
        return true;
 
594
    }
 
595
 
 
596
    public boolean noneSatisfy(<name>Predicate predicate)
 
597
    {
 
598
        for (int i = 0; i \< this.size; i++)
 
599
        {
 
600
            if (predicate.accept(this.items[i]))
 
601
            {
 
602
                return false;
 
603
            }
 
604
        }
 
605
        return true;
 
606
    }
 
607
 
 
608
    public <name>ArrayList select(<name>Predicate predicate)
 
609
    {
 
610
        <name>ArrayList result = new <name>ArrayList();
 
611
        for (int i = 0; i \< this.size; i++)
 
612
        {
 
613
            <type> item = this.items[i];
 
614
            if (predicate.accept(item))
 
615
            {
 
616
                result.add(item);
 
617
            }
 
618
        }
 
619
        return result;
 
620
    }
 
621
 
 
622
    public <name>ArrayList reject(<name>Predicate predicate)
 
623
    {
 
624
        <name>ArrayList result = new <name>ArrayList();
 
625
        for (int i = 0; i \< this.size; i++)
 
626
        {
 
627
            <type> item = this.items[i];
 
628
            if (!predicate.accept(item))
 
629
            {
 
630
                result.add(item);
 
631
            }
 
632
        }
 
633
        return result;
 
634
    }
 
635
 
 
636
    public <type> detectIfNone(<name>Predicate predicate, <type> ifNone)
 
637
    {
 
638
        for (int i = 0; i \< this.size; i++)
 
639
        {
 
640
            <type> item = this.items[i];
 
641
            if (predicate.accept(item))
 
642
            {
 
643
                return item;
 
644
            }
 
645
        }
 
646
        return ifNone;
 
647
    }
 
648
 
 
649
    public \<V> MutableList\<V> collect(<name>ToObjectFunction\<? extends V> function)
 
650
    {
 
651
        FastList\<V> target = FastList.newList(this.size);
 
652
        for (int i = 0; i \< this.size; i++)
 
653
        {
 
654
            target.add(function.valueOf(this.items[i]));
 
655
        }
 
656
        return target;
 
657
    }
 
658
 
 
659
    public <type> max()
 
660
    {
 
661
        if (this.isEmpty())
 
662
        {
 
663
            throw new NoSuchElementException();
 
664
        }
 
665
        <type> max = this.items[0];
 
666
        for (int i = 1; i \< this.size; i++)
 
667
        {
 
668
            <type> value = this.items[i];
 
669
            if (<(lessThan.(type))("max", "value")>)
 
670
            {
 
671
                max = value;
 
672
            }
 
673
        }
 
674
        return max;
 
675
    }
 
676
 
 
677
    public <type> min()
 
678
    {
 
679
        if (this.isEmpty())
 
680
        {
 
681
            throw new NoSuchElementException();
 
682
        }
 
683
        <type> min = this.items[0];
 
684
        for (int i = 1; i \< this.size; i++)
 
685
        {
 
686
            <type> value = this.items[i];
 
687
            if (<(lessThan.(type))("value", "min")>)
 
688
            {
 
689
                min = value;
 
690
            }
 
691
        }
 
692
        return min;
 
693
    }
 
694
 
 
695
    public <type> minIfEmpty(<type> defaultValue)
 
696
    {
 
697
        if (this.isEmpty())
 
698
        {
 
699
            return defaultValue;
 
700
        }
 
701
        return this.min();
 
702
    }
 
703
 
 
704
    public <type> maxIfEmpty(<type> defaultValue)
 
705
    {
 
706
        if (this.isEmpty())
 
707
        {
 
708
            return defaultValue;
 
709
        }
 
710
        return this.max();
 
711
    }
 
712
 
 
713
    public <wideType.(type)> sum()
 
714
    {
 
715
        <wideType.(type)> result = <wideZero.(type)>;
 
716
        for (int i = 0; i \< this.size; i++)
 
717
        {
 
718
            result += this.items[i];
 
719
        }
 
720
        return result;
 
721
    }
 
722
 
 
723
    public <wideType.(type)> dotProduct(<name>List list)
 
724
    {
 
725
        if (this.size != list.size())
 
726
        {
 
727
            throw new IllegalArgumentException("Lists used in dotProduct must be the same size");
 
728
        }
 
729
        <wideType.(type)> sum = <wideZero.(type)>;
 
730
        for (int i = 0; i \< this.size; i++)
 
731
        {
 
732
            sum += <castWideType.(type)>this.items[i] * list.get(i);
 
733
        }
 
734
        return sum;
 
735
    }
 
736
 
 
737
    public double average()
 
738
    {
 
739
        if (this.isEmpty())
 
740
        {
 
741
            throw new ArithmeticException();
 
742
        }
 
743
        return <castSum.(type)>this.sum() / (double) this.size();
 
744
    }
 
745
 
 
746
    public double median()
 
747
    {
 
748
        if (this.isEmpty())
 
749
        {
 
750
            throw new ArithmeticException();
 
751
        }
 
752
        <type>[] sortedArray = this.toSortedArray();
 
753
        int middleIndex = sortedArray.length >\> 1;
 
754
        if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
 
755
        {
 
756
            <type> first = sortedArray[middleIndex];
 
757
            <type> second = sortedArray[middleIndex - 1];
 
758
            return (<castDouble.(type)>first + <castDouble.(type)>second) / 2.0;
 
759
        }
 
760
        return <castDouble.(type)>sortedArray[middleIndex];
 
761
    }
 
762
 
 
763
    public <type>[] toArray()
 
764
    {
 
765
        <type>[] newItems = new <type>[this.size];
 
766
        System.arraycopy(this.items, 0, newItems, 0, this.size);
 
767
        return newItems;
 
768
    }
 
769
 
 
770
    public <type>[] toSortedArray()
 
771
    {
 
772
        <type>[] array = this.toArray();
 
773
        Arrays.sort(array);
 
774
        return array;
 
775
    }
 
776
 
 
777
    @Override
 
778
    public boolean equals(Object otherList)
 
779
    {
 
780
        if (otherList == this)
 
781
        {
 
782
            return true;
 
783
        }
 
784
        if (!(otherList instanceof <name>List))
 
785
        {
 
786
            return false;
 
787
        }
 
788
        <name>List list = (<name>List) otherList;
 
789
        if (this.size != list.size())
 
790
        {
 
791
            return false;
 
792
        }
 
793
        for (int i = 0; i \< this.size; i++)
 
794
        {
 
795
            if (<(notEquals.(type))("this.items[i]", "list.get(i)")>)
 
796
            {
 
797
                return false;
 
798
            }
 
799
        }
 
800
        return true;
 
801
    }
 
802
 
 
803
    @Override
 
804
    public int hashCode()
 
805
    {
 
806
        int hashCode = 1;
 
807
        for (int i = 0; i \< this.size; i++)
 
808
        {
 
809
            <type> item = this.items[i];
 
810
            hashCode = 31 * hashCode + <(hashCode.(type))("item")>;
 
811
        }
 
812
        return hashCode;
 
813
    }
 
814
 
 
815
    @Override
 
816
    public String toString()
 
817
    {
 
818
        return this.makeString("[", ", ", "]");
 
819
    }
 
820
 
 
821
    public String makeString()
 
822
    {
 
823
        return this.makeString(", ");
 
824
    }
 
825
 
 
826
    public String makeString(String separator)
 
827
    {
 
828
        return this.makeString("", separator, "");
 
829
    }
 
830
 
 
831
    public String makeString(String start, String separator, String end)
 
832
    {
 
833
        Appendable stringBuilder = new StringBuilder();
 
834
        this.appendString(stringBuilder, start, separator, end);
 
835
        return stringBuilder.toString();
 
836
    }
 
837
 
 
838
    public void appendString(Appendable appendable)
 
839
    {
 
840
        this.appendString(appendable, ", ");
 
841
    }
 
842
 
 
843
    public void appendString(Appendable appendable, String separator)
 
844
    {
 
845
        this.appendString(appendable, "", separator, "");
 
846
    }
 
847
 
 
848
    public void appendString(
 
849
            Appendable appendable,
 
850
            String start,
 
851
            String separator,
 
852
            String end)
 
853
    {
 
854
        try
 
855
        {
 
856
            appendable.append(start);
 
857
            for (int i = 0; i \< this.size; i++)
 
858
            {
 
859
                if (i > 0)
 
860
                {
 
861
                    appendable.append(separator);
 
862
                }
 
863
                <type> value = this.items[i];
 
864
                appendable.append(String.valueOf(value));
 
865
            }
 
866
            appendable.append(end);
 
867
        }
 
868
        catch (IOException e)
 
869
        {
 
870
            throw new RuntimeException(e);
 
871
        }
 
872
    }
 
873
 
 
874
    public Mutable<name>List toList()
 
875
    {
 
876
        return <name>ArrayList.newList(this);
 
877
    }
 
878
 
 
879
    public Mutable<name>List toSortedList()
 
880
    {
 
881
        return <name>ArrayList.newList(this).sortThis();
 
882
    }
 
883
 
 
884
    public Mutable<name>Set toSet()
 
885
    {
 
886
        return <name>HashSet.newSet(this);
 
887
    }
 
888
 
 
889
    public Mutable<name>Bag toBag()
 
890
    {
 
891
        return <name>HashBag.newBag(this);
 
892
    }
 
893
 
 
894
    public Lazy<name>Iterable asLazy()
 
895
    {
 
896
        return new Lazy<name>IterableAdapter(this);
 
897
    }
 
898
 
 
899
    public Mutable<name>List asUnmodifiable()
 
900
    {
 
901
        return new Unmodifiable<name>List(this);
 
902
    }
 
903
 
 
904
    public Mutable<name>List asSynchronized()
 
905
    {
 
906
        return new Synchronized<name>List(this);
 
907
    }
 
908
 
 
909
    public Immutable<name>List toImmutable()
 
910
    {
 
911
        if (this.items.length == 0)
 
912
        {
 
913
            return <name>Lists.immutable.with();
 
914
        }
 
915
        if (this.items.length == 1)
 
916
        {
 
917
            return <name>Lists.immutable.with(this.items[0]);
 
918
        }
 
919
        return <name>Lists.immutable.with(this.toArray());
 
920
    }
 
921
 
 
922
    public void writeExternal(ObjectOutput out) throws IOException
 
923
    {
 
924
        out.writeInt(this.size);
 
925
        for (int i = 0; i \< this.size; i++)
 
926
        {
 
927
            out.write<name>(this.items[i]);
 
928
        }
 
929
    }
 
930
 
 
931
    public void readExternal(ObjectInput in) throws IOException
 
932
    {
 
933
        this.size = in.readInt();
 
934
        this.items = new <type>[this.size];
 
935
        for (int i = 0; i \< this.size; i++)
 
936
        {
 
937
            this.items[i] = in.read<name>();
 
938
        }
 
939
    }
 
940
 
 
941
    public Lazy<name>Iterable asReversed()
 
942
    {
 
943
        return Reverse<name>Iterable.adapt(this);
 
944
    }
 
945
 
 
946
    public <name>ArrayList reverseThis()
 
947
    {
 
948
        int size = this.items.length;
 
949
        int endIndex = size - 1;
 
950
        for (int i = 0; i \< size / 2; i++)
 
951
        {
 
952
            <type> tempSwapValue = this.items[i];
 
953
            this.items[i] = this.items[endIndex - i];
 
954
            this.items[endIndex - i] = tempSwapValue;
 
955
        }
 
956
        return this;
 
957
    }
 
958
 
 
959
    public <name>ArrayList sortThis()
 
960
    {
 
961
        Arrays.sort(this.items, 0, this.items.length);
 
962
        return this;
 
963
    }
 
964
 
 
965
    public <name>ArrayList toReversed()
 
966
    {
 
967
        return <name>ArrayList.newList(this.asReversed());
 
968
    }
 
969
 
 
970
    public Mutable<name>List subList(int fromIndex, int toIndex)
 
971
    {
 
972
        throw new UnsupportedOperationException("subList not yet implemented!");
 
973
    }
 
974
 
 
975
    private class Internal<name>Iterator implements <name>Iterator
 
976
    {
 
977
        /**
 
978
         * Index of element to be returned by subsequent call to next.
 
979
         */
 
980
        private int currentIndex;
 
981
 
 
982
        public boolean hasNext()
 
983
        {
 
984
            return this.currentIndex != <name>ArrayList.this.size();
 
985
        }
 
986
 
 
987
        public <type> next()
 
988
        {
 
989
            if (!this.hasNext())
 
990
            {
 
991
                throw new NoSuchElementException();
 
992
            }
 
993
            <type> next = <name>ArrayList.this.items[this.currentIndex];
 
994
            this.currentIndex++;
 
995
            return next;
 
996
        }
 
997
    }
 
998
}
 
999
 
 
1000
>>