2
import "primitiveEquals.stg"
3
import "primitiveHashCode.stg"
4
import "primitiveLiteral.stg"
6
targetPath() ::= "com/gs/collections/impl/list/mutable/primitive"
8
fileName(primitive) ::= "<primitive.name>ArrayList"
10
class(primitive) ::= <<
11
<body(primitive.type, primitive.name)>
14
body(type, name) ::= <<
17
package com.gs.collections.impl.list.mutable.primitive;
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;
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;
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.
58
public final class <name>ArrayList
59
implements Mutable<name>List, Externalizable
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;
66
private transient <type>[] items = DEFAULT_SIZED_EMPTY_ARRAY;
68
public <name>ArrayList()
72
public <name>ArrayList(int initialCapacity)
74
this.items = initialCapacity == 0 ? ZERO_SIZED_ARRAY : new <type>[initialCapacity];
77
public <name>ArrayList(<type>... array)
79
this.size = array.length;
84
* Creates a new list using the passed {@code elements} argument as the backing store.
86
* !!! WARNING: This method uses the passed in array, so can be very unsafe if the original
87
* array is held onto anywhere else. !!!
89
public static <name>ArrayList newListWith(<type>... elements)
91
return new <name>ArrayList(elements);
94
public static <name>ArrayList newList(<name>Iterable source)
96
return <name>ArrayList.newListWith(source.toArray());
99
public static <name>ArrayList newWithNValues(int size, <type> value)
101
<name>ArrayList newList = new <name>ArrayList(size);
102
for (int i = 0; i \< size; i++)
114
public boolean isEmpty()
116
return this.size == 0;
119
public boolean notEmpty()
121
return this.size > 0;
126
Arrays.fill(this.items, <(zero.(type))>);
130
public boolean contains(<type> value)
132
for (int i = 0; i \< this.size; i++)
134
if (<(equals.(type))("this.items[i]", "value")>)
142
public boolean containsAll(<type>... source)
144
for (<type> value : source)
146
if (!this.contains(value))
154
public boolean containsAll(<name>Iterable source)
156
for (<name>Iterator iterator = source.<type>Iterator(); iterator.hasNext(); )
158
if (!this.contains(iterator.next()))
166
public <type> get(int index)
168
if (index \< this.size)
170
return this.items[index];
172
throw this.newIndexOutOfBoundsException(index);
175
private IndexOutOfBoundsException newIndexOutOfBoundsException(int index)
177
return new IndexOutOfBoundsException("Index: " + index + " Size: " + this.size);
180
public <type> getFirst()
183
return this.items[0];
186
public <type> getLast()
189
return this.items[this.size() - 1];
192
private void checkEmpty()
196
throw this.newIndexOutOfBoundsException(0);
200
public int indexOf(<type> value)
202
for (int i = 0; i \< this.size; i++)
204
if (<(equals.(type))("this.items[i]", "value")>)
212
public int lastIndexOf(<type> value)
214
for (int i = this.size - 1; i >= 0; i--)
216
if (<(equals.(type))("this.items[i]", "value")>)
224
public void trimToSize()
226
if (this.size \< this.items.length)
228
this.transferItemsToNewArrayWithCapacity(this.size);
232
private void transferItemsToNewArrayWithCapacity(int newCapacity)
234
this.items = this.copyItemsWithNewCapacity(newCapacity);
237
private <type>[] copyItemsWithNewCapacity(int newCapacity)
239
<type>[] newItems = new <type>[newCapacity];
240
System.arraycopy(this.items, 0, newItems, 0, Math.min(this.size, newCapacity));
244
private int sizePlusFiftyPercent(int oldSize)
246
int result = oldSize + (oldSize >\> 1) + 1;
247
return result \< oldSize ? MAXIMUM_ARRAY_SIZE : result;
250
public void ensureCapacity(int minCapacity)
252
int oldCapacity = this.items.length;
253
if (minCapacity > oldCapacity)
255
int newCapacity = Math.max(this.sizePlusFiftyPercent(oldCapacity), minCapacity);
256
this.transferItemsToNewArrayWithCapacity(newCapacity);
260
private void ensureCapacityForAdd()
262
if (this.items == DEFAULT_SIZED_EMPTY_ARRAY)
264
this.items = new <type>[10];
268
this.transferItemsToNewArrayWithCapacity(this.sizePlusFiftyPercent(this.size));
272
public boolean add(<type> newItem)
274
if (this.items.length == this.size)
276
this.ensureCapacityForAdd();
278
this.items[this.size] = newItem;
283
public boolean addAll(<type>... source)
285
if (source.length \< 1)
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);
297
public boolean addAll(<name>Iterable source)
299
return this.addAll(source.toArray());
302
private void throwOutOfBounds(int index)
304
throw this.newIndexOutOfBoundsException(index);
307
public void addAtIndex(int index, <type> element)
309
if (index > -1 && index \< this.size)
311
this.addAtIndexLessThanSize(index, element);
313
else if (index == this.size)
319
this.throwOutOfBounds(index);
323
private void addAtIndexLessThanSize(int index, <type> element)
325
int oldSize = this.size;
327
if (this.items.length == oldSize)
329
<type>[] newItems = new <type>[this.sizePlusFiftyPercent(oldSize)];
332
System.arraycopy(this.items, 0, newItems, 0, index);
334
System.arraycopy(this.items, index, newItems, index + 1, oldSize - index);
335
this.items = newItems;
339
System.arraycopy(this.items, index, this.items, index + 1, oldSize - index);
341
this.items[index] = element;
344
public boolean addAllAtIndex(int index, <type>... source)
346
if (index > this.size || index \< 0)
348
this.throwOutOfBounds(index);
350
if (source.length == 0)
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);
363
public boolean addAllAtIndex(int index, <name>Iterable source)
365
return this.addAllAtIndex(index, source.toArray());
368
private void shiftElementsAtIndex(int index, int sourceSize)
370
int numberToMove = this.size - index;
371
if (numberToMove > 0)
373
System.arraycopy(this.items, index, this.items, index + sourceSize, numberToMove);
377
public boolean remove(<type> value)
379
int index = this.indexOf(value);
382
this.removeAtIndex(index);
388
public boolean removeAll(<name>Iterable source)
390
boolean modified = false;
391
for (int index = 0; index \< this.size; index++)
393
if (source.contains(this.get(index)))
395
this.removeAtIndex(index);
403
public boolean removeAll(<type>... source)
405
<name>HashSet set = <name>HashSet.newSetWith(source);
406
<type>[] newItems = new <type>[this.size];
408
int oldSize = this.size;
409
for (int index = 0; index \< this.size; index++)
411
if (!set.contains(this.items[index]))
413
newItems[count] = this.items[index];
417
this.items = newItems;
419
return oldSize != this.size;
422
public boolean retainAll(<name>Iterable source)
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()
428
public boolean accept(<type> value)
430
return sourceSet.contains(value);
433
this.size = retained.size;
434
this.items = retained.items;
435
return oldSize != this.size();
438
public boolean retainAll(<type>... source)
440
return this.retainAll(<name>HashSet.newSetWith(source));
443
public <type> removeAtIndex(int index)
445
<type> previous = this.get(index);
446
int totalOffset = this.size - index - 1;
449
System.arraycopy(this.items, index + 1, this.items, index, totalOffset);
452
this.items[this.size] = <zero.(type)>;
456
public <type> set(int index, <type> element)
458
<type> previous = this.get(index);
459
this.items[index] = element;
463
public <name>ArrayList with(<type> element)
469
public <name>ArrayList without(<type> element)
471
this.remove(element);
475
public <name>ArrayList withAll(<name>Iterable elements)
477
this.addAll(elements.toArray());
481
public <name>ArrayList withoutAll(<name>Iterable elements)
483
this.removeAll(elements);
487
public <name>ArrayList with(<type> element1, <type> element2)
494
public <name>ArrayList with(<type> element1, <type> element2, <type> element3)
502
public <name>ArrayList with(<type> element1, <type> element2, <type> element3, <type>... elements)
507
return this.withArrayCopy(elements, 0, elements.length);
510
private <name>ArrayList withArrayCopy(<type>[] elements, int begin, int length)
512
this.ensureCapacity(this.size + length);
513
System.arraycopy(elements, begin, this.items, this.size, length);
518
public <name>Iterator <type>Iterator()
520
return new Internal<name>Iterator();
523
public void forEach(<name>Procedure procedure)
525
for (int i = 0; i \< this.size; i++)
527
procedure.value(this.items[i]);
531
public void forEachWithIndex(<name>IntProcedure procedure)
533
for (int i = 0; i \< this.size; i++)
535
procedure.value(this.items[i], i);
539
public \<T> T injectInto(T injectedValue, Object<name>ToObjectFunction\<? super T, ? extends T> function)
541
T result = injectedValue;
542
for (int i = 0; i \< this.size; i++)
544
result = function.valueOf(result, this.items[i]);
549
public \<T> T injectIntoWithIndex(T injectedValue, Object<name>IntToObjectFunction\<? super T, ? extends T> function)
551
T result = injectedValue;
552
for (int i = 0; i \< this.size; i++)
554
result = function.valueOf(result, this.items[i], i);
559
public int count(<name>Predicate predicate)
562
for (int i = 0; i \< this.size; i++)
564
if (predicate.accept(this.items[i]))
572
public boolean anySatisfy(<name>Predicate predicate)
574
for (int i = 0; i \< this.size; i++)
576
if (predicate.accept(this.items[i]))
584
public boolean allSatisfy(<name>Predicate predicate)
586
for (int i = 0; i \< this.size; i++)
588
if (!predicate.accept(this.items[i]))
596
public boolean noneSatisfy(<name>Predicate predicate)
598
for (int i = 0; i \< this.size; i++)
600
if (predicate.accept(this.items[i]))
608
public <name>ArrayList select(<name>Predicate predicate)
610
<name>ArrayList result = new <name>ArrayList();
611
for (int i = 0; i \< this.size; i++)
613
<type> item = this.items[i];
614
if (predicate.accept(item))
622
public <name>ArrayList reject(<name>Predicate predicate)
624
<name>ArrayList result = new <name>ArrayList();
625
for (int i = 0; i \< this.size; i++)
627
<type> item = this.items[i];
628
if (!predicate.accept(item))
636
public <type> detectIfNone(<name>Predicate predicate, <type> ifNone)
638
for (int i = 0; i \< this.size; i++)
640
<type> item = this.items[i];
641
if (predicate.accept(item))
649
public \<V> MutableList\<V> collect(<name>ToObjectFunction\<? extends V> function)
651
FastList\<V> target = FastList.newList(this.size);
652
for (int i = 0; i \< this.size; i++)
654
target.add(function.valueOf(this.items[i]));
663
throw new NoSuchElementException();
665
<type> max = this.items[0];
666
for (int i = 1; i \< this.size; i++)
668
<type> value = this.items[i];
669
if (<(lessThan.(type))("max", "value")>)
681
throw new NoSuchElementException();
683
<type> min = this.items[0];
684
for (int i = 1; i \< this.size; i++)
686
<type> value = this.items[i];
687
if (<(lessThan.(type))("value", "min")>)
695
public <type> minIfEmpty(<type> defaultValue)
704
public <type> maxIfEmpty(<type> defaultValue)
713
public <wideType.(type)> sum()
715
<wideType.(type)> result = <wideZero.(type)>;
716
for (int i = 0; i \< this.size; i++)
718
result += this.items[i];
723
public <wideType.(type)> dotProduct(<name>List list)
725
if (this.size != list.size())
727
throw new IllegalArgumentException("Lists used in dotProduct must be the same size");
729
<wideType.(type)> sum = <wideZero.(type)>;
730
for (int i = 0; i \< this.size; i++)
732
sum += <castWideType.(type)>this.items[i] * list.get(i);
737
public double average()
741
throw new ArithmeticException();
743
return <castSum.(type)>this.sum() / (double) this.size();
746
public double median()
750
throw new ArithmeticException();
752
<type>[] sortedArray = this.toSortedArray();
753
int middleIndex = sortedArray.length >\> 1;
754
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
756
<type> first = sortedArray[middleIndex];
757
<type> second = sortedArray[middleIndex - 1];
758
return (<castDouble.(type)>first + <castDouble.(type)>second) / 2.0;
760
return <castDouble.(type)>sortedArray[middleIndex];
763
public <type>[] toArray()
765
<type>[] newItems = new <type>[this.size];
766
System.arraycopy(this.items, 0, newItems, 0, this.size);
770
public <type>[] toSortedArray()
772
<type>[] array = this.toArray();
778
public boolean equals(Object otherList)
780
if (otherList == this)
784
if (!(otherList instanceof <name>List))
788
<name>List list = (<name>List) otherList;
789
if (this.size != list.size())
793
for (int i = 0; i \< this.size; i++)
795
if (<(notEquals.(type))("this.items[i]", "list.get(i)")>)
804
public int hashCode()
807
for (int i = 0; i \< this.size; i++)
809
<type> item = this.items[i];
810
hashCode = 31 * hashCode + <(hashCode.(type))("item")>;
816
public String toString()
818
return this.makeString("[", ", ", "]");
821
public String makeString()
823
return this.makeString(", ");
826
public String makeString(String separator)
828
return this.makeString("", separator, "");
831
public String makeString(String start, String separator, String end)
833
Appendable stringBuilder = new StringBuilder();
834
this.appendString(stringBuilder, start, separator, end);
835
return stringBuilder.toString();
838
public void appendString(Appendable appendable)
840
this.appendString(appendable, ", ");
843
public void appendString(Appendable appendable, String separator)
845
this.appendString(appendable, "", separator, "");
848
public void appendString(
849
Appendable appendable,
856
appendable.append(start);
857
for (int i = 0; i \< this.size; i++)
861
appendable.append(separator);
863
<type> value = this.items[i];
864
appendable.append(String.valueOf(value));
866
appendable.append(end);
868
catch (IOException e)
870
throw new RuntimeException(e);
874
public Mutable<name>List toList()
876
return <name>ArrayList.newList(this);
879
public Mutable<name>List toSortedList()
881
return <name>ArrayList.newList(this).sortThis();
884
public Mutable<name>Set toSet()
886
return <name>HashSet.newSet(this);
889
public Mutable<name>Bag toBag()
891
return <name>HashBag.newBag(this);
894
public Lazy<name>Iterable asLazy()
896
return new Lazy<name>IterableAdapter(this);
899
public Mutable<name>List asUnmodifiable()
901
return new Unmodifiable<name>List(this);
904
public Mutable<name>List asSynchronized()
906
return new Synchronized<name>List(this);
909
public Immutable<name>List toImmutable()
911
if (this.items.length == 0)
913
return <name>Lists.immutable.with();
915
if (this.items.length == 1)
917
return <name>Lists.immutable.with(this.items[0]);
919
return <name>Lists.immutable.with(this.toArray());
922
public void writeExternal(ObjectOutput out) throws IOException
924
out.writeInt(this.size);
925
for (int i = 0; i \< this.size; i++)
927
out.write<name>(this.items[i]);
931
public void readExternal(ObjectInput in) throws IOException
933
this.size = in.readInt();
934
this.items = new <type>[this.size];
935
for (int i = 0; i \< this.size; i++)
937
this.items[i] = in.read<name>();
941
public Lazy<name>Iterable asReversed()
943
return Reverse<name>Iterable.adapt(this);
946
public <name>ArrayList reverseThis()
948
int size = this.items.length;
949
int endIndex = size - 1;
950
for (int i = 0; i \< size / 2; i++)
952
<type> tempSwapValue = this.items[i];
953
this.items[i] = this.items[endIndex - i];
954
this.items[endIndex - i] = tempSwapValue;
959
public <name>ArrayList sortThis()
961
Arrays.sort(this.items, 0, this.items.length);
965
public <name>ArrayList toReversed()
967
return <name>ArrayList.newList(this.asReversed());
970
public Mutable<name>List subList(int fromIndex, int toIndex)
972
throw new UnsupportedOperationException("subList not yet implemented!");
975
private class Internal<name>Iterator implements <name>Iterator
978
* Index of element to be returned by subsequent call to next.
980
private int currentIndex;
982
public boolean hasNext()
984
return this.currentIndex != <name>ArrayList.this.size();
991
throw new NoSuchElementException();
993
<type> next = <name>ArrayList.this.items[this.currentIndex];