KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > js > rhino > ObjArray


1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * The contents of this file are subject to the Netscape Public
4  * License Version 1.1 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of
6  * the License at http://www.mozilla.org/NPL/
7  *
8  * Software distributed under the License is distributed on an "AS
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10  * implied. See the License for the specific language governing
11  * rights and limitations under the License.
12  *
13  * The Original Code is Rhino code, released
14  * May 6, 1999.
15  *
16  * The Initial Developer of the Original Code is Netscape
17  * Communications Corporation. Portions created by Netscape are
18  * Copyright (C) 1997-2000 Netscape Communications Corporation. All
19  * Rights Reserved.
20  *
21  * Contributor(s):
22  * Igor Bukanov
23  *
24  * Alternatively, the contents of this file may be used under the
25  * terms of the GNU Public License (the "GPL"), in which case the
26  * provisions of the GPL are applicable instead of those above.
27  * If you wish to allow use of your version of this file only
28  * under the terms of the GPL and not to allow others to use your
29  * version of this file under the NPL, indicate your decision by
30  * deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL. If you do not delete
32  * the provisions above, a recipient may use your version of this
33  * file under either the NPL or the GPL.
34  */

35 // Modified by Google
36

37 package com.google.gwt.dev.js.rhino;
38
39 import java.io.Serializable JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.ObjectInputStream JavaDoc;
42 import java.io.ObjectOutputStream JavaDoc;
43
44 /**
45 Implementation of resizable array with focus on minimizing memory usage by storing few initial array elements in object fields. Can also be used as a stack.
46 */

47
48 public class ObjArray implements Serializable JavaDoc {
49
50     public ObjArray() { }
51
52     public ObjArray(int capacityHint) {
53         if (capacityHint < 0) throw new IllegalArgumentException JavaDoc();
54         if (capacityHint > FIELDS_STORE_SIZE) {
55             data = new Object JavaDoc[capacityHint - FIELDS_STORE_SIZE];
56         }
57     }
58
59     public final boolean isEmpty() {
60         return size == 0;
61     }
62
63     public final int size() {
64         return size;
65     }
66
67     public final void setSize(int newSize) {
68         if (newSize < 0) throw new IllegalArgumentException JavaDoc();
69         int N = size;
70         if (newSize < N) {
71             for (int i = newSize; i != N; ++i) {
72                 setImpl(i, null);
73             }
74         }else if (newSize > N) {
75             if (newSize > FIELDS_STORE_SIZE) {
76                 ensureCapacity(newSize);
77             }
78         }
79         size = newSize;
80     }
81
82     public final Object JavaDoc get(int index) {
83         if (!(0 <= index && index < size)) throw invalidIndex(index, size);
84         return getImpl(index);
85     }
86
87     public final void set(int index, Object JavaDoc value) {
88         if (!(0 <= index && index < size)) throw invalidIndex(index, size);
89         setImpl(index, value);
90     }
91
92     private Object JavaDoc getImpl(int index) {
93         switch (index) {
94             case 0: return f0;
95             case 1: return f1;
96             case 2: return f2;
97             case 3: return f3;
98             case 4: return f4;
99             case 5: return f5;
100         }
101         return data[index - FIELDS_STORE_SIZE];
102     }
103
104     private void setImpl(int index, Object JavaDoc value) {
105         switch (index) {
106             case 0: f0 = value; break;
107             case 1: f1 = value; break;
108             case 2: f2 = value; break;
109             case 3: f3 = value; break;
110             case 4: f4 = value; break;
111             case 5: f5 = value; break;
112             default: data[index - FIELDS_STORE_SIZE] = value;
113         }
114
115     }
116
117     public int indexOf(Object JavaDoc obj) {
118         int N = size;
119         for (int i = 0; i != N; ++i) {
120             Object JavaDoc current = getImpl(i);
121             if (current == obj || (current != null && current.equals(obj))) {
122                 return i;
123             }
124         }
125         return -1;
126     }
127
128     public int lastIndexOf(Object JavaDoc obj) {
129         for (int i = size; i != 0;) {
130             --i;
131             Object JavaDoc current = getImpl(i);
132             if (current == obj || (current != null && current.equals(obj))) {
133                 return i;
134             }
135         }
136         return -1;
137     }
138
139     public final Object JavaDoc peek() {
140         int N = size;
141         if (N == 0) throw invalidEmptyStackAccess();
142         return getImpl(N - 1);
143     }
144
145     public final Object JavaDoc pop() {
146         int N = size;
147         --N;
148         Object JavaDoc top;
149         switch (N) {
150             case -1: throw invalidEmptyStackAccess();
151             case 0: top = f0; f0 = null; break;
152             case 1: top = f1; f1 = null; break;
153             case 2: top = f2; f2 = null; break;
154             case 3: top = f3; f3 = null; break;
155             case 4: top = f4; f4 = null; break;
156             case 5: top = f5; f5 = null; break;
157             default:
158                 top = data[N - FIELDS_STORE_SIZE];
159                 data[N - FIELDS_STORE_SIZE] = null;
160         }
161         size = N;
162         return top;
163     }
164
165     public final void push(Object JavaDoc value) {
166         add(value);
167     }
168
169     public final void add(Object JavaDoc value) {
170         int N = size;
171         if (N >= FIELDS_STORE_SIZE) {
172             ensureCapacity(N + 1);
173         }
174         size = N + 1;
175         setImpl(N, value);
176     }
177
178     public final void add(int index, Object JavaDoc value) {
179         Object JavaDoc tmp;
180         int N = size;
181         if (!(0 <= index && index <= N)) throw invalidIndex(index, N + 1);
182         switch (index) {
183             case 0:
184                 if (N == 0) { f0 = value; break; }
185                 tmp = f0; f0 = value; value = tmp;
186             case 1:
187                 if (N == 1) { f1 = value; break; }
188                 tmp = f1; f1 = value; value = tmp;
189             case 2:
190                 if (N == 2) { f2 = value; break; }
191                 tmp = f2; f2 = value; value = tmp;
192             case 3:
193                 if (N == 3) { f3 = value; break; }
194                 tmp = f3; f3 = value; value = tmp;
195             case 4:
196                 if (N == 4) { f4 = value; break; }
197                 tmp = f4; f4 = value; value = tmp;
198             case 5:
199                 if (N == 5) { f5 = value; break; }
200                 tmp = f5; f5 = value; value = tmp;
201
202                 index = FIELDS_STORE_SIZE;
203             default:
204                 ensureCapacity(N + 1);
205                 if (index != N) {
206                     System.arraycopy(data, index - FIELDS_STORE_SIZE,
207                                      data, index - FIELDS_STORE_SIZE + 1,
208                                      N - index);
209                 }
210                 data[index - FIELDS_STORE_SIZE] = value;
211         }
212         size = N + 1;
213     }
214
215     public final void remove(int index) {
216         int N = size;
217         if (!(0 <= index && index < N)) throw invalidIndex(index, N);
218         --N;
219         switch (index) {
220             case 0:
221                 if (N == 0) { f0 = null; break; }
222                 f0 = f1;
223             case 1:
224                 if (N == 1) { f1 = null; break; }
225                 f1 = f2;
226             case 2:
227                 if (N == 2) { f2 = null; break; }
228                 f2 = f3;
229             case 3:
230                 if (N == 3) { f3 = null; break; }
231                 f3 = f4;
232             case 4:
233                 if (N == 4) { f4 = null; break; }
234                 f4 = f5;
235             case 5:
236                 if (N == 5) { f5 = null; break; }
237                 f5 = data[0];
238
239                 index = FIELDS_STORE_SIZE;
240             default:
241                 if (index != N) {
242                     System.arraycopy(data, index - FIELDS_STORE_SIZE + 1,
243                                      data, index - FIELDS_STORE_SIZE,
244                                      N - index);
245                 }
246                 data[N - FIELDS_STORE_SIZE] = null;
247         }
248         size = N;
249     }
250
251     public final void clear() {
252         int N = size;
253         for (int i = 0; i != N; ++i) {
254             setImpl(i, null);
255         }
256         size = 0;
257     }
258
259     public final Object JavaDoc[] toArray() {
260         Object JavaDoc[] array = new Object JavaDoc[size];
261         toArray(array, 0);
262         return array;
263     }
264
265     public final void toArray(Object JavaDoc[] array) {
266         toArray(array, 0);
267     }
268
269     public final void toArray(Object JavaDoc[] array, int offset) {
270         int N = size;
271         switch (N) {
272             default:
273                 System.arraycopy(data, 0, array, offset + FIELDS_STORE_SIZE,
274                                  N - FIELDS_STORE_SIZE);
275             case 6: array[offset + 5] = f5;
276             case 5: array[offset + 4] = f4;
277             case 4: array[offset + 3] = f3;
278             case 3: array[offset + 2] = f2;
279             case 2: array[offset + 1] = f1;
280             case 1: array[offset + 0] = f0;
281             case 0: break;
282         }
283     }
284
285     private void ensureCapacity(int minimalCapacity) {
286         int required = minimalCapacity - FIELDS_STORE_SIZE;
287         if (required <= 0) throw new IllegalArgumentException JavaDoc();
288         if (data == null) {
289             int alloc = FIELDS_STORE_SIZE * 2;
290             if (alloc < required) {
291                 alloc = required;
292             }
293             data = new Object JavaDoc[alloc];
294         } else {
295             int alloc = data.length;
296             if (alloc < required) {
297                    if (alloc <= FIELDS_STORE_SIZE) {
298                     alloc = FIELDS_STORE_SIZE * 2;
299                 } else {
300                     alloc *= 2;
301                 }
302                 if (alloc < required) {
303                     alloc = required;
304                 }
305                 Object JavaDoc[] tmp = new Object JavaDoc[alloc];
306                 if (size > FIELDS_STORE_SIZE) {
307                     System.arraycopy(data, 0, tmp, 0,
308                                      size - FIELDS_STORE_SIZE);
309                 }
310                 data = tmp;
311             }
312         }
313     }
314
315     private static RuntimeException JavaDoc invalidIndex(int index, int upperBound) {
316         // \u2209 is "NOT ELEMENT OF"
317
String JavaDoc msg = index+" \u2209 [0, "+upperBound+')';
318         return new IndexOutOfBoundsException JavaDoc(msg);
319     }
320
321     private static RuntimeException JavaDoc invalidEmptyStackAccess() {
322         throw new RuntimeException JavaDoc("Empty stack");
323     }
324
325     private void writeObject(ObjectOutputStream JavaDoc os) throws IOException JavaDoc {
326         os.defaultWriteObject();
327         int N = size;
328         for (int i = 0; i != N; ++i) {
329             Object JavaDoc obj = getImpl(i);
330             os.writeObject(obj);
331         }
332     }
333
334     private void readObject(ObjectInputStream JavaDoc is)
335         throws IOException JavaDoc, ClassNotFoundException JavaDoc
336     {
337         is.defaultReadObject(); // It reads size
338
int N = size;
339         if (N > FIELDS_STORE_SIZE) {
340             data = new Object JavaDoc[N - FIELDS_STORE_SIZE];
341         }
342         for (int i = 0; i != N; ++i) {
343             Object JavaDoc obj = is.readObject();
344             setImpl(i, obj);
345         }
346     }
347
348     static final long serialVersionUID = 7448768847663119705L;
349
350 // Number of data elements
351
private int size;
352
353     private static final int FIELDS_STORE_SIZE = 6;
354     private transient Object JavaDoc f0, f1, f2, f3, f4, f5;
355     private transient Object JavaDoc[] data;
356 }
357
Popular Tags