KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > ESArrayWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 /**
32  * JavaScript object
33  */

34 public class ESArrayWrapper {
35   public static ESJavaWrapper wrapper(Global g, Class JavaDoc cl)
36   {
37     Class JavaDoc baseClass = cl.getComponentType();
38     String JavaDoc name = baseClass.getName();
39
40     if (name.equals("boolean"))
41       return new BooleanArray(g);
42     else if (name.equals("byte"))
43       return new ByteArray(g);
44     else if (name.equals("short"))
45       return new ShortArray(g);
46     else if (name.equals("char"))
47       return new CharArray(g);
48     else if (name.equals("int"))
49       return new IntArray(g);
50     else if (name.equals("long"))
51       return new LongArray(g);
52     else if (name.equals("float"))
53       return new FloatArray(g);
54     else if (name.equals("double"))
55       return new DoubleArray(g);
56     else
57       return new ObjArray(g);
58   }
59
60   static class ObjArray extends ESArrayWrap {
61     protected ObjArray() {}
62     protected ESObject dup()
63     {
64       ObjArray dup = new ObjArray();
65       dup.value = value;
66       return dup;
67     }
68
69     protected int length() { return ((Object JavaDoc []) value).length; }
70
71     public ESBase getProperty(int i)
72       throws Throwable JavaDoc
73     {
74       Object JavaDoc []array = (Object JavaDoc []) value;
75
76       if (i >= 0 && i < array.length)
77         return Global.getGlobalProto().wrap(array[i]);
78       else
79         return esEmpty;
80     }
81
82     public void setProperty(int i, ESBase value) throws ESException
83     {
84       Object JavaDoc []array = (Object JavaDoc []) this.value;
85
86       if (i >= 0 && i < array.length) {
87         array[i] = value.toJavaObject();
88       }
89     }
90
91     public ESBase delete(int i) throws ESException
92     {
93       Object JavaDoc []array = (Object JavaDoc []) this.value;
94
95       if (i >= 0 && i < array.length) {
96         array[i] = null;
97         return ESBoolean.TRUE;
98       }
99
100       return ESBoolean.FALSE;
101     }
102
103     ObjArray(Global g)
104     {
105       super(g.arrayProto, null);
106     }
107   }
108
109   static class BooleanArray extends ESArrayWrap {
110     protected BooleanArray() {}
111     protected ESObject dup()
112     {
113       BooleanArray dup = new BooleanArray();
114       dup.value = value;
115       return dup;
116     }
117
118     protected int length() { return ((boolean []) value).length; }
119
120     public ESBase getProperty(int i)
121     {
122       boolean []array = (boolean []) value;
123
124       if (i >= 0 && i < array.length)
125         return ESBoolean.create(array[i]);
126       else
127         return esEmpty;
128     }
129
130     public void setProperty(int i, ESBase value) throws ESException
131     {
132       boolean []array = (boolean []) this.value;
133
134       if (i >= 0 && i < array.length) {
135         array[i] = value.toBoolean();
136       }
137     }
138
139     public ESBase delete(int i) throws ESException
140     {
141       boolean []array = (boolean []) this.value;
142
143       if (i >= 0 && i < array.length) {
144         array[i] = false;
145         return ESBoolean.TRUE;
146       }
147
148       return ESBoolean.FALSE;
149     }
150
151     BooleanArray(Global g)
152     {
153       super(g.arrayProto, null);
154     }
155   }
156
157   static class ByteArray extends ESArrayWrap {
158     protected ByteArray() {}
159     protected ESObject dup()
160     {
161       ByteArray dup = new ByteArray();
162       dup.value = value;
163       return dup;
164     }
165
166     protected int length() { return ((byte []) value).length; }
167
168     public ESBase getProperty(int i)
169     {
170       byte []array = (byte []) value;
171
172       if (i >= 0 && i < array.length)
173         return ESNumber.create(array[i]);
174       else
175         return esEmpty;
176     }
177
178     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
179     {
180       byte []array = (byte []) this.value;
181
182       if (i >= 0 && i < array.length) {
183         array[i] = (byte) value.toInt32();
184       }
185     }
186
187     public ESBase delete(int i) throws ESException
188     {
189       byte []array = (byte []) this.value;
190
191       if (i >= 0 && i < array.length) {
192         array[i] = 0;
193         return ESBoolean.TRUE;
194       }
195
196       return ESBoolean.FALSE;
197     }
198
199     ByteArray(Global g)
200     {
201       super(g.arrayProto, null);
202     }
203   }
204
205   static class ShortArray extends ESArrayWrap {
206     protected ShortArray() {}
207     protected ESObject dup()
208     {
209       ShortArray dup = new ShortArray();
210       dup.value = value;
211       return dup;
212     }
213
214     protected int length() { return ((short []) value).length; }
215
216     public ESBase getProperty(int i)
217     {
218       short []array = (short []) value;
219
220       if (i >= 0 && i < array.length)
221         return ESNumber.create(array[i]);
222       else
223         return esEmpty;
224     }
225
226     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
227     {
228       short []array = (short []) this.value;
229
230       if (i >= 0 && i < array.length) {
231         array[i] = (short) value.toInt32();
232       }
233     }
234
235     public ESBase delete(int i) throws ESException
236     {
237       short []array = (short []) this.value;
238
239       if (i >= 0 && i < array.length) {
240         array[i] = 0;
241         return ESBoolean.TRUE;
242       }
243
244       return ESBoolean.FALSE;
245     }
246
247     ShortArray(Global g)
248     {
249       super(g.arrayProto, null);
250     }
251   }
252
253   static class CharArray extends ESArrayWrap {
254     protected CharArray() {}
255     protected ESObject dup()
256     {
257       CharArray dup = new CharArray();
258       dup.value = value;
259       return dup;
260     }
261
262     protected int length() { return ((char []) value).length; }
263
264     public ESBase getProperty(int i)
265     {
266       char []array = (char []) value;
267
268       if (i >= 0 && i < array.length)
269         return ESString.create("" + array[i]);
270       else
271         return esEmpty;
272     }
273
274     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
275     {
276       char []array = (char []) this.value;
277
278       if (i >= 0 && i < array.length) {
279         ESString str = value.toStr();
280
281         array[i] = str.length() > 0 ? str.charAt(0) : 0;
282       }
283     }
284
285     public ESBase delete(int i) throws ESException
286     {
287       char []array = (char []) this.value;
288
289       if (i >= 0 && i < array.length) {
290         array[i] = 0;
291         return ESBoolean.TRUE;
292       }
293
294       return ESBoolean.FALSE;
295     }
296
297     CharArray(Global g)
298     {
299       super(g.arrayProto, null);
300     }
301   }
302
303   static class IntArray extends ESArrayWrap {
304     protected IntArray() {}
305     protected ESObject dup()
306     {
307       IntArray dup = new IntArray();
308       dup.value = value;
309       return dup;
310     }
311
312     protected int length() { return ((int []) value).length; }
313
314     public ESBase getProperty(int i)
315     {
316       int []array = (int []) value;
317
318       if (i >= 0 && i < array.length)
319         return ESNumber.create(array[i]);
320       else
321         return esEmpty;
322     }
323
324     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
325     {
326       int []array = (int []) this.value;
327
328       if (i >= 0 && i < array.length) {
329         array[i] = (int) value.toInt32();
330       }
331     }
332
333     public ESBase delete(int i) throws ESException
334     {
335       int []array = (int []) this.value;
336
337       if (i >= 0 && i < array.length) {
338         array[i] = 0;
339         return ESBoolean.TRUE;
340       }
341
342       return ESBoolean.FALSE;
343     }
344
345     IntArray(Global g)
346     {
347       super(g.arrayProto, null);
348     }
349   }
350
351   static class LongArray extends ESArrayWrap {
352     protected LongArray() {}
353     protected ESObject dup()
354     {
355       LongArray dup = new LongArray();
356       dup.value = value;
357       return dup;
358     }
359
360     protected int length() { return ((long []) value).length; }
361
362     public ESBase getProperty(int i)
363     {
364       long []array = (long []) value;
365
366       if (i >= 0 && i < array.length)
367         return ESNumber.create(array[i]);
368       else
369         return esEmpty;
370     }
371
372     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
373     {
374       long []array = (long []) this.value;
375
376       if (i >= 0 && i < array.length) {
377         array[i] = (long) value.toNum();
378       }
379     }
380
381     public ESBase delete(int i) throws ESException
382     {
383       long []array = (long []) this.value;
384
385       if (i >= 0 && i < array.length) {
386         array[i] = 0;
387         return ESBoolean.TRUE;
388       }
389
390       return ESBoolean.FALSE;
391     }
392
393     LongArray(Global g)
394     {
395       super(g.arrayProto, null);
396     }
397   }
398
399   static class FloatArray extends ESArrayWrap {
400     protected FloatArray() {}
401     protected ESObject dup()
402     {
403       FloatArray dup = new FloatArray();
404       dup.value = value;
405       return dup;
406     }
407
408     protected int length() { return ((float []) value).length; }
409
410     public ESBase getProperty(int i)
411     {
412       float []array = (float []) value;
413
414       if (i >= 0 && i < array.length)
415         return ESNumber.create(array[i]);
416       else
417         return esEmpty;
418     }
419
420     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
421     {
422       float []array = (float []) this.value;
423
424       if (i >= 0 && i < array.length) {
425         array[i] = (float) value.toNum();
426       }
427     }
428
429     public ESBase delete(int i) throws ESException
430     {
431       float []array = (float []) this.value;
432
433       if (i >= 0 && i < array.length) {
434         array[i] = 0;
435         return ESBoolean.TRUE;
436       }
437
438       return ESBoolean.FALSE;
439     }
440
441     FloatArray(Global g)
442     {
443       super(g.arrayProto, null);
444     }
445   }
446
447   static class DoubleArray extends ESArrayWrap {
448     protected DoubleArray() {}
449     protected ESObject dup()
450     {
451       DoubleArray dup = new DoubleArray();
452       dup.value = value;
453       return dup;
454     }
455
456     protected int length() { return ((double []) value).length; }
457
458     public ESBase getProperty(int i)
459     {
460       double []array = (double []) value;
461
462       if (i >= 0 && i < array.length)
463         return ESNumber.create(array[i]);
464       else
465         return esEmpty;
466     }
467
468     public void setProperty(int i, ESBase value) throws Throwable JavaDoc
469     {
470       double []array = (double []) this.value;
471
472       if (i >= 0 && i < array.length) {
473         array[i] = (double) value.toNum();
474       }
475     }
476
477     public ESBase delete(int i) throws ESException
478     {
479       double []array = (double []) this.value;
480
481       if (i >= 0 && i < array.length) {
482         array[i] = 0;
483         return ESBoolean.TRUE;
484       }
485
486       return ESBoolean.FALSE;
487     }
488
489     DoubleArray(Global g)
490     {
491       super(g.arrayProto, null);
492     }
493   }
494 }
495
Popular Tags