1 dojo.provide("calitha.collections.ArrayList"); 2 dojo.require("calitha.collections.AbstractList"); 3 dojo.require("calitha.collections.util"); 4 dojo.require("calitha.collections.arrays"); 5 dojo.require("calitha.exception.IndexOutOfBoundsException"); 6 7 dojo.declare("calitha.collections.ArrayList", calitha.collections.AbstractList, 8 /** @lends calitha.collections.ArrayList#*/ 9 { 10 11 /** 12 * @constructs 13 * @extends calitha.collections.AbstractList 14 * @param initialCapacity optional initial capacity of the underlying array 15 * @description This is a list implementation backed by an array. 16 * <p>This class is similar to the 17 * <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html">Java ArrayList class</a> 18 */ 19 constructor: function(/**Number?*/ initialCapacity) 20 { 21 if (initialCapacity == null) 22 { 23 initialCapacity = 10; 24 } 25 this._array = new Array(initialCapacity); 26 this._size = 0; 27 } 28 , 29 addAll: function(/**calitha.collections.ICollection*/ collection) 30 { 31 return this.insertAll(this._size, collection); 32 } 33 , 34 clear: function() 35 { 36 this._modCount++; 37 for (var i = 0; i < this._size; i++) 38 { 39 this._array[i] = null; 40 } 41 this._size = 0; 42 } 43 , 44 del: function(/**Number*/ index) 45 { 46 if (index < 0 || index >= this.size()) 47 { 48 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 49 } 50 this._modCount++; 51 var element = this.get(index); 52 this._array.splice(index, 1); 53 this._size--; 54 return element; 55 } 56 , 57 ensureCapacity: function(/**Number*/ minCapacity) 58 { 59 this._modCount++; 60 if (minCapacity > this._array.length) 61 { 62 this._array.length = minCapacity; 63 } 64 } 65 , 66 get: function(/**Number*/ index) 67 { 68 this._rangeCheck(index); 69 return this._array[index]; 70 } 71 , 72 indexOf: function indexOf(/**Object*/ element) 73 { 74 for (var i = 0; i < this.size(); i++) 75 { 76 if (calitha.collections.util.equals(this.get(i), element)) 77 { 78 return i; 79 } 80 } 81 return -1; 82 } 83 , 84 insert: function insert(/**Number*/ index, /**Object*/ element) 85 { 86 if (index < 0 || index > this.size()) 87 { 88 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 89 } 90 this._modCount++; 91 this._array.splice(index, 0, element); 92 this._size++; 93 } 94 , 95 insertAll: function(/**Number*/ index, /**calitha.collections.ICollection*/ collection) 96 { 97 if (index > this._size || index < 0) 98 { 99 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 100 } 101 if (collection.isEmpty()) 102 { 103 return false; 104 } 105 var a = collection.toArray(); 106 calitha.collections.arrays.insertRangeToArray(a, 0, a.length, this._array, index); 107 this._size += a.length; 108 return true; 109 } 110 , 111 lastIndexOf: function lastIndexOf(/**Object*/ element) 112 { 113 for (var i = this.size() - 1; i >= 0; i--) 114 { 115 if (calitha.collections.util.equals(this.get(i), element)) 116 { 117 return i; 118 } 119 } 120 return -1; 121 } 122 , 123 remove: function(/**Object*/ element) 124 { 125 for (var i = 0; i < this.size(); i++) 126 { 127 if (calitha.collections.util.equals(this.get(i), element)) 128 { 129 this.del(i); 130 return true; 131 } 132 } 133 return false; 134 } 135 , 136 set: function(/**Number*/ index, /**Object*/ element) 137 { 138 this._rangeCheck(index); 139 var oldValue = this._array[index]; 140 this._array[index] = element; 141 return oldValue; 142 } 143 , 144 size: function() 145 { 146 return this._size; 147 } 148 , 149 toArray: function() 150 { 151 return this._array.slice(0, this._size); 152 } 153 , 154 trimToSize: function() 155 { 156 this._modCount++; 157 this._array.length = this._size; 158 } 159 , 160 _rangeCheck: function(/**Number*/ index) 161 { 162 if ((index < 0) ||(index >= this._size)) 163 { 164 throw new calitha.exception.IndexOutOfBoundsException(Error(), index, this._size); 165 } 166 } 167 , 168 _removeRange: function(/**Number*/ fromIndex, /**Number*/ toIndex) 169 { 170 calitha.collections.arrays.copyRange(this._array, toIndex, this._size, fromIndex); 171 this._size -= toIndex - fromIndex; 172 } 173 174 }); 175