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