d52b319bd63cf1c7b67c83e831e75d329dd8e56f
[jalviewjs.git] / site / j2s / java / util / ArrayDeque.js
1 Clazz.declarePackage ("java.util");
2 Clazz.load (["java.util.AbstractCollection", "$.Iterator", "java.util.Deque"], "java.util.ArrayDeque", ["java.lang.AssertionError", "$.IllegalStateException", "$.NullPointerException", "java.lang.reflect.Array", "java.util.Arrays", "$.ConcurrentModificationException", "$.NoSuchElementException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.elements = null;
5 this.head = 0;
6 this.tail = 0;
7 if (!Clazz.isClassDefined ("java.util.ArrayDeque.DeqIterator")) {
8 java.util.ArrayDeque.$ArrayDeque$DeqIterator$ ();
9 }
10 if (!Clazz.isClassDefined ("java.util.ArrayDeque.DescendingIterator")) {
11 java.util.ArrayDeque.$ArrayDeque$DescendingIterator$ ();
12 }
13 Clazz.instantialize (this, arguments);
14 }, java.util, "ArrayDeque", java.util.AbstractCollection, [java.util.Deque, Cloneable, java.io.Serializable]);
15 Clazz.defineMethod (c$, "allocateElements", 
16  function (numElements) {
17 var initialCapacity = 8;
18 if (numElements >= initialCapacity) {
19 initialCapacity = numElements;
20 initialCapacity |= (initialCapacity >>> 1);
21 initialCapacity |= (initialCapacity >>> 2);
22 initialCapacity |= (initialCapacity >>> 4);
23 initialCapacity |= (initialCapacity >>> 8);
24 initialCapacity |= (initialCapacity >>> 16);
25 initialCapacity++;
26 if (initialCapacity < 0) initialCapacity >>>= 1;
27 }this.elements =  new Array (initialCapacity);
28 }, "~N");
29 Clazz.defineMethod (c$, "doubleCapacity", 
30  function () {
31 var p = this.head;
32 var n = this.elements.length;
33 var r = n - p;
34 var newCapacity = n << 1;
35 if (newCapacity < 0) throw  new IllegalStateException ("Sorry, deque too big");
36 var a =  new Array (newCapacity);
37 System.arraycopy (this.elements, p, a, 0, r);
38 System.arraycopy (this.elements, 0, a, r, p);
39 this.elements = a;
40 this.head = 0;
41 this.tail = n;
42 });
43 Clazz.defineMethod (c$, "copyElements", 
44  function (a) {
45 if (this.head < this.tail) {
46 System.arraycopy (this.elements, this.head, a, 0, this.size ());
47 } else if (this.head > this.tail) {
48 var headPortionLen = this.elements.length - this.head;
49 System.arraycopy (this.elements, this.head, a, 0, headPortionLen);
50 System.arraycopy (this.elements, 0, a, headPortionLen, this.tail);
51 }return a;
52 }, "~A");
53 Clazz.makeConstructor (c$, 
54 function () {
55 Clazz.superConstructor (this, java.util.ArrayDeque, []);
56 this.elements =  new Array (16);
57 });
58 Clazz.makeConstructor (c$, 
59 function (numElements) {
60 Clazz.superConstructor (this, java.util.ArrayDeque, []);
61 this.allocateElements (numElements);
62 }, "~N");
63 Clazz.makeConstructor (c$, 
64 function (c) {
65 Clazz.superConstructor (this, java.util.ArrayDeque, []);
66 this.allocateElements (c.size ());
67 this.addAll (c);
68 }, "java.util.Collection");
69 Clazz.overrideMethod (c$, "addFirst", 
70 function (e) {
71 if (e == null) throw  new NullPointerException ();
72 this.elements[this.head = (this.head - 1) & (this.elements.length - 1)] = e;
73 if (this.head == this.tail) this.doubleCapacity ();
74 }, "~O");
75 Clazz.overrideMethod (c$, "addLast", 
76 function (e) {
77 if (e == null) throw  new NullPointerException ();
78 this.elements[this.tail] = e;
79 if ((this.tail = (this.tail + 1) & (this.elements.length - 1)) == this.head) this.doubleCapacity ();
80 }, "~O");
81 Clazz.overrideMethod (c$, "offerFirst", 
82 function (e) {
83 this.addFirst (e);
84 return true;
85 }, "~O");
86 Clazz.overrideMethod (c$, "offerLast", 
87 function (e) {
88 this.addLast (e);
89 return true;
90 }, "~O");
91 Clazz.overrideMethod (c$, "removeFirst", 
92 function () {
93 var x = this.pollFirst ();
94 if (x == null) throw  new java.util.NoSuchElementException ();
95 return x;
96 });
97 Clazz.overrideMethod (c$, "removeLast", 
98 function () {
99 var x = this.pollLast ();
100 if (x == null) throw  new java.util.NoSuchElementException ();
101 return x;
102 });
103 Clazz.overrideMethod (c$, "pollFirst", 
104 function () {
105 var h = this.head;
106 var result = this.elements[h];
107 if (result == null) return null;
108 this.elements[h] = null;
109 this.head = (h + 1) & (this.elements.length - 1);
110 return result;
111 });
112 Clazz.overrideMethod (c$, "pollLast", 
113 function () {
114 var t = (this.tail - 1) & (this.elements.length - 1);
115 var result = this.elements[t];
116 if (result == null) return null;
117 this.elements[t] = null;
118 this.tail = t;
119 return result;
120 });
121 Clazz.overrideMethod (c$, "getFirst", 
122 function () {
123 var x = this.elements[this.head];
124 if (x == null) throw  new java.util.NoSuchElementException ();
125 return x;
126 });
127 Clazz.overrideMethod (c$, "getLast", 
128 function () {
129 var x = this.elements[(this.tail - 1) & (this.elements.length - 1)];
130 if (x == null) throw  new java.util.NoSuchElementException ();
131 return x;
132 });
133 Clazz.overrideMethod (c$, "peekFirst", 
134 function () {
135 return this.elements[this.head];
136 });
137 Clazz.overrideMethod (c$, "peekLast", 
138 function () {
139 return this.elements[(this.tail - 1) & (this.elements.length - 1)];
140 });
141 Clazz.overrideMethod (c$, "removeFirstOccurrence", 
142 function (o) {
143 if (o == null) return false;
144 var mask = this.elements.length - 1;
145 var i = this.head;
146 var x;
147 while ((x = this.elements[i]) != null) {
148 if (o.equals (x)) {
149 this.$delete (i);
150 return true;
151 }i = (i + 1) & mask;
152 }
153 return false;
154 }, "~O");
155 Clazz.overrideMethod (c$, "removeLastOccurrence", 
156 function (o) {
157 if (o == null) return false;
158 var mask = this.elements.length - 1;
159 var i = (this.tail - 1) & mask;
160 var x;
161 while ((x = this.elements[i]) != null) {
162 if (o.equals (x)) {
163 this.$delete (i);
164 return true;
165 }i = (i - 1) & mask;
166 }
167 return false;
168 }, "~O");
169 Clazz.overrideMethod (c$, "add", 
170 function (e) {
171 this.addLast (e);
172 return true;
173 }, "~O");
174 Clazz.overrideMethod (c$, "offer", 
175 function (e) {
176 return this.offerLast (e);
177 }, "~O");
178 Clazz.defineMethod (c$, "remove", 
179 function () {
180 return this.removeFirst ();
181 });
182 Clazz.overrideMethod (c$, "poll", 
183 function () {
184 return this.pollFirst ();
185 });
186 Clazz.overrideMethod (c$, "element", 
187 function () {
188 return this.getFirst ();
189 });
190 Clazz.overrideMethod (c$, "peek", 
191 function () {
192 return this.peekFirst ();
193 });
194 Clazz.overrideMethod (c$, "push", 
195 function (e) {
196 this.addFirst (e);
197 }, "~O");
198 Clazz.overrideMethod (c$, "pop", 
199 function () {
200 return this.removeFirst ();
201 });
202 Clazz.defineMethod (c$, "checkInvariants", 
203  function () {
204 });
205 Clazz.defineMethod (c$, "$delete", 
206  function (i) {
207 this.checkInvariants ();
208 var elements = this.elements;
209 var mask = elements.length - 1;
210 var h = this.head;
211 var t = this.tail;
212 var front = (i - h) & mask;
213 var back = (t - i) & mask;
214 if (front >= ((t - h) & mask)) throw  new java.util.ConcurrentModificationException ();
215 if (front < back) {
216 if (h <= i) {
217 System.arraycopy (elements, h, elements, h + 1, front);
218 } else {
219 System.arraycopy (elements, 0, elements, 1, i);
220 elements[0] = elements[mask];
221 System.arraycopy (elements, h, elements, h + 1, mask - h);
222 }elements[h] = null;
223 this.head = (h + 1) & mask;
224 return false;
225 } else {
226 if (i < t) {
227 System.arraycopy (elements, i + 1, elements, i, back);
228 this.tail = t - 1;
229 } else {
230 System.arraycopy (elements, i + 1, elements, i, mask - i);
231 elements[mask] = elements[0];
232 System.arraycopy (elements, 1, elements, 0, t);
233 this.tail = (t - 1) & mask;
234 }return true;
235 }}, "~N");
236 Clazz.overrideMethod (c$, "size", 
237 function () {
238 return (this.tail - this.head) & (this.elements.length - 1);
239 });
240 Clazz.overrideMethod (c$, "isEmpty", 
241 function () {
242 return this.head == this.tail;
243 });
244 Clazz.overrideMethod (c$, "iterator", 
245 function () {
246 return Clazz.innerTypeInstance (java.util.ArrayDeque.DeqIterator, this, null);
247 });
248 Clazz.overrideMethod (c$, "descendingIterator", 
249 function () {
250 return Clazz.innerTypeInstance (java.util.ArrayDeque.DescendingIterator, this, null);
251 });
252 Clazz.overrideMethod (c$, "contains", 
253 function (o) {
254 if (o == null) return false;
255 var mask = this.elements.length - 1;
256 var i = this.head;
257 var x;
258 while ((x = this.elements[i]) != null) {
259 if (o.equals (x)) return true;
260 i = (i + 1) & mask;
261 }
262 return false;
263 }, "~O");
264 Clazz.defineMethod (c$, "remove", 
265 function (o) {
266 return this.removeFirstOccurrence (o);
267 }, "~O");
268 Clazz.overrideMethod (c$, "clear", 
269 function () {
270 var h = this.head;
271 var t = this.tail;
272 if (h != t) {
273 this.head = this.tail = 0;
274 var i = h;
275 var mask = this.elements.length - 1;
276 do {
277 this.elements[i] = null;
278 i = (i + 1) & mask;
279 } while (i != t);
280 }});
281 Clazz.defineMethod (c$, "toArray", 
282 function () {
283 return this.copyElements ( new Array (this.size ()));
284 });
285 Clazz.defineMethod (c$, "toArray", 
286 function (a) {
287 var size = this.size ();
288 if (a.length < size) a = java.lang.reflect.Array.newInstance (a.getClass ().getComponentType (), size);
289 this.copyElements (a);
290 if (a.length > size) a[size] = null;
291 return a;
292 }, "~A");
293 Clazz.defineMethod (c$, "clone", 
294 function () {
295 try {
296 var result = Clazz.superCall (this, java.util.ArrayDeque, "clone", []);
297 result.elements = java.util.Arrays.copyOf (this.elements, this.elements.length);
298 return result;
299 } catch (e) {
300 if (Clazz.exceptionOf (e, CloneNotSupportedException)) {
301 throw  new AssertionError ();
302 } else {
303 throw e;
304 }
305 }
306 });
307 c$.$ArrayDeque$DeqIterator$ = function () {
308 Clazz.pu$h(self.c$);
309 c$ = Clazz.decorateAsClass (function () {
310 Clazz.prepareCallback (this, arguments);
311 this.cursor = 0;
312 this.fence = 0;
313 this.lastRet = -1;
314 Clazz.instantialize (this, arguments);
315 }, java.util.ArrayDeque, "DeqIterator", null, java.util.Iterator);
316 Clazz.prepareFields (c$, function () {
317 this.cursor = this.b$["java.util.ArrayDeque"].head;
318 this.fence = this.b$["java.util.ArrayDeque"].tail;
319 });
320 Clazz.overrideMethod (c$, "hasNext", 
321 function () {
322 return this.cursor != this.fence;
323 });
324 Clazz.overrideMethod (c$, "next", 
325 function () {
326 if (this.cursor == this.fence) throw  new java.util.NoSuchElementException ();
327 var a = this.b$["java.util.ArrayDeque"].elements[this.cursor];
328 if (this.b$["java.util.ArrayDeque"].tail != this.fence || a == null) throw  new java.util.ConcurrentModificationException ();
329 this.lastRet = this.cursor;
330 this.cursor = (this.cursor + 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
331 return a;
332 });
333 Clazz.overrideMethod (c$, "remove", 
334 function () {
335 if (this.lastRet < 0) throw  new IllegalStateException ();
336 if (this.b$["java.util.ArrayDeque"].$delete (this.lastRet)) {
337 this.cursor = (this.cursor - 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
338 this.fence = this.b$["java.util.ArrayDeque"].tail;
339 }this.lastRet = -1;
340 });
341 c$ = Clazz.p0p ();
342 };
343 c$.$ArrayDeque$DescendingIterator$ = function () {
344 Clazz.pu$h(self.c$);
345 c$ = Clazz.decorateAsClass (function () {
346 Clazz.prepareCallback (this, arguments);
347 this.cursor = 0;
348 this.fence = 0;
349 this.lastRet = -1;
350 Clazz.instantialize (this, arguments);
351 }, java.util.ArrayDeque, "DescendingIterator", null, java.util.Iterator);
352 Clazz.prepareFields (c$, function () {
353 this.cursor = this.b$["java.util.ArrayDeque"].tail;
354 this.fence = this.b$["java.util.ArrayDeque"].head;
355 });
356 Clazz.overrideMethod (c$, "hasNext", 
357 function () {
358 return this.cursor != this.fence;
359 });
360 Clazz.overrideMethod (c$, "next", 
361 function () {
362 if (this.cursor == this.fence) throw  new java.util.NoSuchElementException ();
363 this.cursor = (this.cursor - 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
364 var a = this.b$["java.util.ArrayDeque"].elements[this.cursor];
365 if (this.b$["java.util.ArrayDeque"].head != this.fence || a == null) throw  new java.util.ConcurrentModificationException ();
366 this.lastRet = this.cursor;
367 return a;
368 });
369 Clazz.overrideMethod (c$, "remove", 
370 function () {
371 if (this.lastRet < 0) throw  new IllegalStateException ();
372 if (!this.b$["java.util.ArrayDeque"].$delete (this.lastRet)) {
373 this.cursor = (this.cursor + 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
374 this.fence = this.b$["java.util.ArrayDeque"].head;
375 }this.lastRet = -1;
376 });
377 c$ = Clazz.p0p ();
378 };
379 Clazz.defineStatics (c$,
380 "MIN_INITIAL_CAPACITY", 8);
381 });