Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / java / util / ArrayList.js
1 Clazz.load(["java.util.AbstractList","$.List","$.RandomAccess"],"java.util.ArrayList",["java.lang.IllegalArgumentException","$.IndexOutOfBoundsException","java.lang.reflect.Array","java.util.Arrays"],function(){
2 c$=Clazz.decorateAsClass(function(){
3 this.firstIndex=0;
4 this.lastIndex=0;
5 this.array=null;
6 Clazz.instantialize(this,arguments);
7 },java.util,"ArrayList",java.util.AbstractList,[java.util.List,Cloneable,java.io.Serializable,java.util.RandomAccess]);
8
9 Clazz.overrideConstructor(c$,
10 function(){
11 this.setup(0);
12 });
13
14 Clazz.defineMethod(c$, "setup",
15 function(capacity){
16 //Clazz.superConstructor(this,java.util.ArrayList,[]);
17 this.firstIndex=this.lastIndex=0;
18 try{
19 this.array=this.newElementArray(capacity);
20 }catch(e){
21 if(Clazz.instanceOf(e,NegativeArraySizeException)){
22 throw new IllegalArgumentException();
23 }else{
24 throw e;
25 }
26 }
27 },"~N");
28 /*
29 Clazz.makeConstructor(c$,
30 function(collection){
31 Clazz.superConstructor(this,java.util.ArrayList,[]);
32 var size=collection.size();
33 this.firstIndex=this.lastIndex=0;
34 this.array=this.newElementArray(size+(Math.floor(size/10)));
35 this.addAll(collection);
36 },"java.util.Collection");
37
38 */
39
40 Clazz.defineMethod(c$,"newElementArray",
41 ($fz=function(size){
42 return new Array(size);
43 },$fz.isPrivate=true,$fz),"~N");
44
45 Clazz.overrideMethod(c$,"add",
46 function(location,object){
47
48 if (arguments.length == 1) {
49         // coming from Java methods, e.g. Collections.list()
50         // location is actually the object
51         return this.add1(location);
52 }
53 var size=this.size();
54 if(0<location&&location<size){
55 if(this.firstIndex==0&&this.lastIndex==this.array.length){
56 this.growForInsert(location,1);
57 }else if((location<Math.floor(size/2)&&this.firstIndex>0)||this.lastIndex==this.array.length){
58 System.arraycopy(this.array,this.firstIndex,this.array,--this.firstIndex,location);
59 }else{
60 var index=location+this.firstIndex;
61 System.arraycopy(this.array,index,this.array,index+1,size-location);
62 this.lastIndex++;
63 }this.array[location+this.firstIndex]=object;
64 }else if(location==0){
65 if(this.firstIndex==0){
66 this.growAtFront(1);
67 }this.array[--this.firstIndex]=object;
68 }else if(location==size){
69 if(this.lastIndex==this.array.length){
70 this.growAtEnd(1);
71 }this.array[this.lastIndex++]=object;
72 }else{
73 throw new IndexOutOfBoundsException();
74 }this.modCount++;
75 },"~N,~O");
76
77 Clazz.overrideMethod(c$,"add1",
78 function(object){
79 if(this.lastIndex==this.array.length){
80 this.growAtEnd(1);
81 }this.array[this.lastIndex++]=object;
82 this.modCount++;
83 return true;
84 },"~O");
85
86 /* BH disallow addAll(int,List)
87  * 
88 Clazz.defineMethod(c$,"addAll",
89 function(location,collection){
90 var size=this.size();
91 if(location<0||location>size){
92 throw new IndexOutOfBoundsException();
93 }var growSize=collection.size();
94 if(0<location&&location<size){
95 if(this.array.length-size<growSize){
96 this.growForInsert(location,growSize);
97 }else if((location<Math.floor(size/2)&&this.firstIndex>0)||this.lastIndex>this.array.length-growSize){
98 var newFirst=this.firstIndex-growSize;
99 if(newFirst<0){
100 var index=location+this.firstIndex;
101 System.arraycopy(this.array,index,this.array,index-newFirst,size-location);
102 this.lastIndex-=newFirst;
103 newFirst=0;
104 }System.arraycopy(this.array,this.firstIndex,this.array,newFirst,location);
105 this.firstIndex=newFirst;
106 }else{
107 var index=location+this.firstIndex;
108 System.arraycopy(this.array,index,this.array,index+growSize,size-location);
109 this.lastIndex+=growSize;
110 }}else if(location==0){
111 this.growAtFront(growSize);
112 this.firstIndex-=growSize;
113 }else if(location==size){
114 if(this.lastIndex>this.array.length-growSize){
115 this.growAtEnd(growSize);
116 }this.lastIndex+=growSize;
117 }if(growSize>0){
118 var it=collection.iterator();
119 var index=location+this.firstIndex;
120 var end=index+growSize;
121 while(index<end){
122 this.array[index++]=it.next();
123 }
124 this.modCount++;
125 return true;
126 }return false;
127 },"~N,java.util.Collection");
128
129  */
130
131 Clazz.overrideMethod(c$,"addAll",
132 function(collection){
133 var growSize=collection.size();
134 if(growSize>0){
135         if(this.lastIndex>this.array.length-growSize){
136                 this.growAtEnd(growSize);
137 }
138 var it=collection.iterator();
139 var end=this.lastIndex+growSize;
140 while(this.lastIndex<end){
141         this.array[this.lastIndex++]=it.next();
142 }
143 this.modCount++;
144 return true;
145 }return false;
146 },"java.util.Collection");
147
148 Clazz.overrideMethod(c$,"clear",
149 function(){
150 if(this.firstIndex!=this.lastIndex){
151 this.fill(this.firstIndex,this.lastIndex);
152 this.firstIndex=this.lastIndex=0;
153 this.modCount++;
154 }});
155
156 Clazz.defineMethod(c$,"fill", function(i1, i2) { // BH
157 for (var i = i2; --i >= i1;)
158 this.array[i] = null;
159 },"~N,~N");
160
161 Clazz.defineMethod(c$,"clone",
162 function(){
163 try{
164 var newList=Clazz.superCall(this,java.util.ArrayList,"clone",[]);
165 newList.array=this.array.clone();
166 return newList;
167 }catch(e){
168 if(Clazz.instanceOf(e,CloneNotSupportedException)){
169 return null;
170 }else{
171 throw e;
172 }
173 }
174 });
175 Clazz.overrideMethod(c$,"contains",
176 function(object){
177 if(object!=null){
178 for(var i=this.firstIndex;i<this.lastIndex;i++){
179 if(object.equals(this.array[i])){
180 return true;
181 }}
182 }else{
183 for(var i=this.firstIndex;i<this.lastIndex;i++){
184 if(this.array[i]==null){
185 return true;
186 }}
187 }return false;
188 },"~O");
189 Clazz.defineMethod(c$,"ensureCapacity",
190 function(minimumCapacity){
191 if(this.array.length<minimumCapacity){
192 if(this.firstIndex>0){
193 this.growAtFront(minimumCapacity-this.array.length);
194 }else{
195 this.growAtEnd(minimumCapacity-this.array.length);
196 }}},"~N");
197 Clazz.overrideMethod(c$,"get",
198 function(location){
199 if(0<=location&&location<this.size()){
200 return this.array[this.firstIndex+location];
201 }throw new IndexOutOfBoundsException();
202 },"~N");
203
204
205 Clazz.defineMethod(c$,"growAtEnd",
206 ($fz=function(required){
207 var size=this.size();
208 if(this.firstIndex>=required-(this.array.length-this.lastIndex)){
209         var newLast=this.lastIndex-this.firstIndex;
210         if(size>0){
211                 System.arraycopy(this.array,this.firstIndex,this.array,0,size);
212                 var start=newLast<this.firstIndex?this.firstIndex:newLast;
213                 this.fill(start,this.array.length);
214         }
215         this.firstIndex=0;
216         this.lastIndex=newLast;
217 }else{
218         var increment=Math.floor(size/2);
219         if(required>increment){
220                 increment=required;
221         }
222         if(increment<12){
223                 increment=12;
224         }
225         var newArray=this.newElementArray(size+increment);
226         if(size>0){
227                 System.arraycopy(this.array,this.firstIndex,newArray,this.firstIndex,size);
228         }
229         this.array=newArray;
230 }
231
232 },$fz.isPrivate=true,$fz),"~N");
233 Clazz.defineMethod(c$,"growAtFront",
234 ($fz=function(required){
235 var size=this.size();
236 if(this.array.length-this.lastIndex>=required){
237 var newFirst=this.array.length-size;
238 if(size>0){
239 System.arraycopy(this.array,this.firstIndex,this.array,newFirst,size);
240 var length=this.firstIndex+size>newFirst?newFirst:this.firstIndex+size;
241 this.fill(this.firstIndex,length);
242 }this.firstIndex=newFirst;
243 this.lastIndex=this.array.length;
244 }else{
245 var increment=Math.floor(size/2);
246 if(required>increment){
247 increment=required;
248 }if(increment<12){
249 increment=12;
250 }var newArray=this.newElementArray(size+increment);
251 if(size>0){
252 System.arraycopy(this.array,this.firstIndex,newArray,newArray.length-size,size);
253 }this.firstIndex=newArray.length-size;
254 this.lastIndex=newArray.length;
255 this.array=newArray;
256 }},$fz.isPrivate=true,$fz),"~N");
257 Clazz.defineMethod(c$,"growForInsert",
258 ($fz=function(location,required){
259 var size=this.size();
260 var increment=Math.floor(size/2);
261 if(required>increment){
262 increment=required;
263 }if(increment<12){
264 increment=12;
265 }var newArray=this.newElementArray(size+increment);
266 if(location<Math.floor(size/2)){
267 var newFirst=newArray.length-(size+required);
268 System.arraycopy(this.array,location,newArray,location+increment,size-location);
269 System.arraycopy(this.array,this.firstIndex,newArray,newFirst,location);
270 this.firstIndex=newFirst;
271 this.lastIndex=newArray.length;
272 }else{
273 System.arraycopy(this.array,this.firstIndex,newArray,0,location);
274 System.arraycopy(this.array,location,newArray,location+required,size-location);
275 this.firstIndex=0;
276 this.lastIndex+=required;
277 }this.array=newArray;
278 },$fz.isPrivate=true,$fz),"~N,~N");
279 Clazz.overrideMethod(c$,"indexOf",
280 function(object){
281 if(object!=null){
282 for(var i=this.firstIndex;i<this.lastIndex;i++){
283 if(object.equals(this.array[i])){
284 return i-this.firstIndex;
285 }}
286 }else{
287 for(var i=this.firstIndex;i<this.lastIndex;i++){
288 if(this.array[i]==null){
289 return i-this.firstIndex;
290 }}
291 }return-1;
292 },"~O");
293 Clazz.overrideMethod(c$,"isEmpty",
294 function(){
295 return this.lastIndex==this.firstIndex;
296 });
297 Clazz.overrideMethod(c$,"lastIndexOf",
298 function(object){
299 if(object!=null){
300 for(var i=this.lastIndex-1;i>=this.firstIndex;i--){
301 if(object.equals(this.array[i])){
302 return i-this.firstIndex;
303 }}
304 }else{
305 for(var i=this.lastIndex-1;i>=this.firstIndex;i--){
306 if(this.array[i]==null){
307 return i-this.firstIndex;
308 }}
309 }return-1;
310 },"~O");
311 Clazz.overrideMethod(c$,"remove",
312 function(location){
313 if (0+location != location) return this.removeObject(location)
314 var result;
315 var size=this.size();
316 if(0<=location&&location<size){
317 if(location==size-1){
318 result=this.array[--this.lastIndex];
319 this.array[this.lastIndex]=null;
320 }else if(location==0){
321 result=this.array[this.firstIndex];
322 this.array[this.firstIndex++]=null;
323 }else{
324 var elementIndex=this.firstIndex+location;
325 result=this.array[elementIndex];
326 if(location<Math.floor(size/2)){
327 System.arraycopy(this.array,this.firstIndex,this.array,this.firstIndex+1,location);
328 this.array[this.firstIndex++]=null;
329 }else{
330 System.arraycopy(this.array,elementIndex+1,this.array,elementIndex,size-location-1);
331 this.array[--this.lastIndex]=null;
332 }}}else{
333 throw new IndexOutOfBoundsException();
334 }this.modCount++;
335 return result;
336 },"~N"); 
337
338 Clazz.defineMethod(c$, "removeObject", function(o) {
339         var i = this.indexOf(o);
340         if (i < 0)return null;
341         return this.remove(i);
342 }, "~O");
343
344 Clazz.overrideMethod(c$,"removeRange",
345 function(start,end){
346 if(start>=0&&start<=end&&end<=this.size()){
347 if(start==end){
348 return;
349 }var size=this.size();
350 if(end==size){
351         this.fill(this.firstIndex+start,this.lastIndex);
352 this.lastIndex=this.firstIndex+start;
353 }else if(start==0){
354         this.fill(this.firstIndex,this.firstIndex+end);
355 this.firstIndex+=end;
356 }else{
357 System.arraycopy(this.array,this.firstIndex+end,this.array,this.firstIndex+start,size-end);
358 var newLast=this.lastIndex+start-end;
359 this.fill(newLast,this.lastIndex);
360 this.lastIndex=newLast;
361 }this.modCount++;
362 }else{
363 throw new IndexOutOfBoundsException();
364 }},"~N,~N");
365 Clazz.overrideMethod(c$,"set",
366 function(location,object){
367 if(0<=location&&location<this.size()){
368 var result=this.array[this.firstIndex+location];
369 this.array[this.firstIndex+location]=object;
370 return result;
371 }throw new IndexOutOfBoundsException();
372 },"~N,~O");
373 Clazz.overrideMethod(c$,"size",
374 function(){
375 return this.lastIndex-this.firstIndex;
376 });
377 /*
378 Clazz.defineMethod(c$,"toArray",
379 function(){
380 var size=this.size();
381 var result=new Array(size);
382 System.arraycopy(this.array,this.firstIndex,result,0,size);
383 return result;
384 });
385 */
386
387 Clazz.overrideMethod(c$,"toArray",
388 function(contents){
389 var size=this.size();
390 if(!contents || size>contents.length)
391         contents= new Array(size);
392 System.arraycopy(this.array,this.firstIndex,contents,0,size);
393 if(size<contents.length){
394 contents[size]=null;
395 }return contents;
396 },"~O");
397 Clazz.defineMethod(c$,"trimToSize",
398 function(){
399 var size=this.size();
400 var newArray=this.newElementArray(size);
401 System.arraycopy(this.array,this.firstIndex,newArray,0,size);
402 this.array=newArray;
403 this.firstIndex=0;
404 this.lastIndex=this.array.length;
405 });
406 });