2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
8 import dstm2.AtomicArray;
9 import dstm2.factory.Factory;
12 import java.util.Arrays;
20 public class ArrayList<V extends dstm2.AtomicSuperClass>{
21 transient volatile int modCount;
22 private volatile int size;
23 AtomicArray<entry<V>> elementData;
24 private Factory<entry> factory;
30 public ArrayList(int initialCapacity) {
32 if (initialCapacity < 0)
33 throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
34 this .elementData = new AtomicArray<entry<V>>(entry.class,initialCapacity);
38 public void ensureCapacity(int minCapacity) {
40 int oldCapacity = elementData.size();
41 if (minCapacity > oldCapacity) {
42 //Object oldData[] = elementData;
43 int newCapacity = (oldCapacity * 3) / 2 + 1;
44 if (newCapacity < minCapacity)
45 newCapacity = minCapacity;
46 // minCapacity is usually close to size, so this is a win:
47 AtomicArray<entry<V>> newar = new AtomicArray<entry<V>>(entry.class, newCapacity);
48 for (int i=0; i<newCapacity; i++)
49 newar.set(i, elementData.get(i));
55 * Returns the number of elements in this list.
57 * @return the number of elements in this list
63 public boolean add(V value) {
64 ensureCapacity(size + 1); // Increments modCount!
65 entry<V> e = factory.create();
67 elementData.set(size, e);
72 public V get(int index){
73 return elementData.get(index).getValue();
77 public V remove(int index) {
80 entry<V> oldValue = elementData.get(index);
82 int numMoved = size - index - 1;
84 System.arraycopy(elementData, index + 1, elementData,
87 elementData.set(size, null); // Let gc do its work
88 return oldValue.getValue();
91 private void rangeCheck(int index) {
92 if (index < 0 || index >= this .size)
93 throw new IndexOutOfBoundsException();
99 for (int i = 0; i < size; i++)
100 elementData.set(i, null);
104 @atomic interface entry<V>{
106 void setValue(V val);