001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: /**
032: * JavaScript object
033: */
034: public class ESArrayWrapper {
035: public static ESJavaWrapper wrapper(Global g, Class cl) {
036: Class baseClass = cl.getComponentType();
037: String name = baseClass.getName();
038:
039: if (name.equals("boolean"))
040: return new BooleanArray(g);
041: else if (name.equals("byte"))
042: return new ByteArray(g);
043: else if (name.equals("short"))
044: return new ShortArray(g);
045: else if (name.equals("char"))
046: return new CharArray(g);
047: else if (name.equals("int"))
048: return new IntArray(g);
049: else if (name.equals("long"))
050: return new LongArray(g);
051: else if (name.equals("float"))
052: return new FloatArray(g);
053: else if (name.equals("double"))
054: return new DoubleArray(g);
055: else
056: return new ObjArray(g);
057: }
058:
059: static class ObjArray extends ESArrayWrap {
060: protected ObjArray() {
061: }
062:
063: protected ESObject dup() {
064: ObjArray dup = new ObjArray();
065: dup.value = value;
066: return dup;
067: }
068:
069: protected int length() {
070: return ((Object[]) value).length;
071: }
072:
073: public ESBase getProperty(int i) throws Throwable {
074: Object[] array = (Object[]) value;
075:
076: if (i >= 0 && i < array.length)
077: return Global.getGlobalProto().wrap(array[i]);
078: else
079: return esEmpty;
080: }
081:
082: public void setProperty(int i, ESBase value) throws ESException {
083: Object[] array = (Object[]) this .value;
084:
085: if (i >= 0 && i < array.length) {
086: array[i] = value.toJavaObject();
087: }
088: }
089:
090: public ESBase delete(int i) throws ESException {
091: Object[] array = (Object[]) this .value;
092:
093: if (i >= 0 && i < array.length) {
094: array[i] = null;
095: return ESBoolean.TRUE;
096: }
097:
098: return ESBoolean.FALSE;
099: }
100:
101: ObjArray(Global g) {
102: super (g.arrayProto, null);
103: }
104: }
105:
106: static class BooleanArray extends ESArrayWrap {
107: protected BooleanArray() {
108: }
109:
110: protected ESObject dup() {
111: BooleanArray dup = new BooleanArray();
112: dup.value = value;
113: return dup;
114: }
115:
116: protected int length() {
117: return ((boolean[]) value).length;
118: }
119:
120: public ESBase getProperty(int i) {
121: boolean[] array = (boolean[]) value;
122:
123: if (i >= 0 && i < array.length)
124: return ESBoolean.create(array[i]);
125: else
126: return esEmpty;
127: }
128:
129: public void setProperty(int i, ESBase value) throws ESException {
130: boolean[] array = (boolean[]) this .value;
131:
132: if (i >= 0 && i < array.length) {
133: array[i] = value.toBoolean();
134: }
135: }
136:
137: public ESBase delete(int i) throws ESException {
138: boolean[] array = (boolean[]) this .value;
139:
140: if (i >= 0 && i < array.length) {
141: array[i] = false;
142: return ESBoolean.TRUE;
143: }
144:
145: return ESBoolean.FALSE;
146: }
147:
148: BooleanArray(Global g) {
149: super (g.arrayProto, null);
150: }
151: }
152:
153: static class ByteArray extends ESArrayWrap {
154: protected ByteArray() {
155: }
156:
157: protected ESObject dup() {
158: ByteArray dup = new ByteArray();
159: dup.value = value;
160: return dup;
161: }
162:
163: protected int length() {
164: return ((byte[]) value).length;
165: }
166:
167: public ESBase getProperty(int i) {
168: byte[] array = (byte[]) value;
169:
170: if (i >= 0 && i < array.length)
171: return ESNumber.create(array[i]);
172: else
173: return esEmpty;
174: }
175:
176: public void setProperty(int i, ESBase value) throws Throwable {
177: byte[] array = (byte[]) this .value;
178:
179: if (i >= 0 && i < array.length) {
180: array[i] = (byte) value.toInt32();
181: }
182: }
183:
184: public ESBase delete(int i) throws ESException {
185: byte[] array = (byte[]) this .value;
186:
187: if (i >= 0 && i < array.length) {
188: array[i] = 0;
189: return ESBoolean.TRUE;
190: }
191:
192: return ESBoolean.FALSE;
193: }
194:
195: ByteArray(Global g) {
196: super (g.arrayProto, null);
197: }
198: }
199:
200: static class ShortArray extends ESArrayWrap {
201: protected ShortArray() {
202: }
203:
204: protected ESObject dup() {
205: ShortArray dup = new ShortArray();
206: dup.value = value;
207: return dup;
208: }
209:
210: protected int length() {
211: return ((short[]) value).length;
212: }
213:
214: public ESBase getProperty(int i) {
215: short[] array = (short[]) value;
216:
217: if (i >= 0 && i < array.length)
218: return ESNumber.create(array[i]);
219: else
220: return esEmpty;
221: }
222:
223: public void setProperty(int i, ESBase value) throws Throwable {
224: short[] array = (short[]) this .value;
225:
226: if (i >= 0 && i < array.length) {
227: array[i] = (short) value.toInt32();
228: }
229: }
230:
231: public ESBase delete(int i) throws ESException {
232: short[] array = (short[]) this .value;
233:
234: if (i >= 0 && i < array.length) {
235: array[i] = 0;
236: return ESBoolean.TRUE;
237: }
238:
239: return ESBoolean.FALSE;
240: }
241:
242: ShortArray(Global g) {
243: super (g.arrayProto, null);
244: }
245: }
246:
247: static class CharArray extends ESArrayWrap {
248: protected CharArray() {
249: }
250:
251: protected ESObject dup() {
252: CharArray dup = new CharArray();
253: dup.value = value;
254: return dup;
255: }
256:
257: protected int length() {
258: return ((char[]) value).length;
259: }
260:
261: public ESBase getProperty(int i) {
262: char[] array = (char[]) value;
263:
264: if (i >= 0 && i < array.length)
265: return ESString.create("" + array[i]);
266: else
267: return esEmpty;
268: }
269:
270: public void setProperty(int i, ESBase value) throws Throwable {
271: char[] array = (char[]) this .value;
272:
273: if (i >= 0 && i < array.length) {
274: ESString str = value.toStr();
275:
276: array[i] = str.length() > 0 ? str.charAt(0) : 0;
277: }
278: }
279:
280: public ESBase delete(int i) throws ESException {
281: char[] array = (char[]) this .value;
282:
283: if (i >= 0 && i < array.length) {
284: array[i] = 0;
285: return ESBoolean.TRUE;
286: }
287:
288: return ESBoolean.FALSE;
289: }
290:
291: CharArray(Global g) {
292: super (g.arrayProto, null);
293: }
294: }
295:
296: static class IntArray extends ESArrayWrap {
297: protected IntArray() {
298: }
299:
300: protected ESObject dup() {
301: IntArray dup = new IntArray();
302: dup.value = value;
303: return dup;
304: }
305:
306: protected int length() {
307: return ((int[]) value).length;
308: }
309:
310: public ESBase getProperty(int i) {
311: int[] array = (int[]) value;
312:
313: if (i >= 0 && i < array.length)
314: return ESNumber.create(array[i]);
315: else
316: return esEmpty;
317: }
318:
319: public void setProperty(int i, ESBase value) throws Throwable {
320: int[] array = (int[]) this .value;
321:
322: if (i >= 0 && i < array.length) {
323: array[i] = (int) value.toInt32();
324: }
325: }
326:
327: public ESBase delete(int i) throws ESException {
328: int[] array = (int[]) this .value;
329:
330: if (i >= 0 && i < array.length) {
331: array[i] = 0;
332: return ESBoolean.TRUE;
333: }
334:
335: return ESBoolean.FALSE;
336: }
337:
338: IntArray(Global g) {
339: super (g.arrayProto, null);
340: }
341: }
342:
343: static class LongArray extends ESArrayWrap {
344: protected LongArray() {
345: }
346:
347: protected ESObject dup() {
348: LongArray dup = new LongArray();
349: dup.value = value;
350: return dup;
351: }
352:
353: protected int length() {
354: return ((long[]) value).length;
355: }
356:
357: public ESBase getProperty(int i) {
358: long[] array = (long[]) value;
359:
360: if (i >= 0 && i < array.length)
361: return ESNumber.create(array[i]);
362: else
363: return esEmpty;
364: }
365:
366: public void setProperty(int i, ESBase value) throws Throwable {
367: long[] array = (long[]) this .value;
368:
369: if (i >= 0 && i < array.length) {
370: array[i] = (long) value.toNum();
371: }
372: }
373:
374: public ESBase delete(int i) throws ESException {
375: long[] array = (long[]) this .value;
376:
377: if (i >= 0 && i < array.length) {
378: array[i] = 0;
379: return ESBoolean.TRUE;
380: }
381:
382: return ESBoolean.FALSE;
383: }
384:
385: LongArray(Global g) {
386: super (g.arrayProto, null);
387: }
388: }
389:
390: static class FloatArray extends ESArrayWrap {
391: protected FloatArray() {
392: }
393:
394: protected ESObject dup() {
395: FloatArray dup = new FloatArray();
396: dup.value = value;
397: return dup;
398: }
399:
400: protected int length() {
401: return ((float[]) value).length;
402: }
403:
404: public ESBase getProperty(int i) {
405: float[] array = (float[]) value;
406:
407: if (i >= 0 && i < array.length)
408: return ESNumber.create(array[i]);
409: else
410: return esEmpty;
411: }
412:
413: public void setProperty(int i, ESBase value) throws Throwable {
414: float[] array = (float[]) this .value;
415:
416: if (i >= 0 && i < array.length) {
417: array[i] = (float) value.toNum();
418: }
419: }
420:
421: public ESBase delete(int i) throws ESException {
422: float[] array = (float[]) this .value;
423:
424: if (i >= 0 && i < array.length) {
425: array[i] = 0;
426: return ESBoolean.TRUE;
427: }
428:
429: return ESBoolean.FALSE;
430: }
431:
432: FloatArray(Global g) {
433: super (g.arrayProto, null);
434: }
435: }
436:
437: static class DoubleArray extends ESArrayWrap {
438: protected DoubleArray() {
439: }
440:
441: protected ESObject dup() {
442: DoubleArray dup = new DoubleArray();
443: dup.value = value;
444: return dup;
445: }
446:
447: protected int length() {
448: return ((double[]) value).length;
449: }
450:
451: public ESBase getProperty(int i) {
452: double[] array = (double[]) value;
453:
454: if (i >= 0 && i < array.length)
455: return ESNumber.create(array[i]);
456: else
457: return esEmpty;
458: }
459:
460: public void setProperty(int i, ESBase value) throws Throwable {
461: double[] array = (double[]) this .value;
462:
463: if (i >= 0 && i < array.length) {
464: array[i] = (double) value.toNum();
465: }
466: }
467:
468: public ESBase delete(int i) throws ESException {
469: double[] array = (double[]) this .value;
470:
471: if (i >= 0 && i < array.length) {
472: array[i] = 0;
473: return ESBoolean.TRUE;
474: }
475:
476: return ESBoolean.FALSE;
477: }
478:
479: DoubleArray(Global g) {
480: super(g.arrayProto, null);
481: }
482: }
483: }
|