comparison agent/src/share/classes/sun/jvm/hotspot/utilities/soql/JSList.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.utilities.soql;
26
27 import java.util.*;
28 import sun.jvm.hotspot.oops.*;
29
30 /**
31 This is JavaScript wrapper for Java List.
32 */
33
34 public class JSList extends DefaultScriptObject {
35 public JSList(List list, JSJavaFactory fac) {
36 this.list = list;
37 this.factory = fac;
38 }
39
40 public Object get(String name) {
41 if (name.equals("length")) {
42 return new Integer(list.size());
43 } else {
44 return super.get(name);
45 }
46 }
47
48 public Object get(int index) {
49 if (isInRange(index)) {
50 Object item = list.get(index);
51 return wrapObject(item);
52 } else {
53 return super.get(index);
54 }
55 }
56
57 public Object[] getIds() {
58 Object[] superIds = super.getIds();
59 final int size = list.size();
60 Object[] res = new Object[superIds.length + size];
61 for (int i = 0; i < size; i++) {
62 res[i] = new Integer(i);
63 }
64 System.arraycopy(superIds, 0, res, size, superIds.length);
65 return res;
66 }
67
68 public boolean has(String name) {
69 if (name.equals("length")) {
70 return true;
71 } else {
72 return super.has(name);
73 }
74 }
75
76 public boolean has(int index) {
77 if (isInRange(index)) {
78 return true;
79 } else {
80 return super.has(index);
81 }
82 }
83
84 public void put(String name, Object value) {
85 if (! name.equals("length")) {
86 super.put(name, value);
87 }
88 }
89
90 public void put(int index, Object value) {
91 if (! isInRange(index)) {
92 super.put(index, value);
93 }
94 }
95
96 public String toString() {
97 StringBuffer buf = new StringBuffer();
98 buf.append('[');
99 for (Iterator itr = list.iterator(); itr.hasNext();) {
100 buf.append(wrapObject(itr.next()));
101 if (itr.hasNext()) {
102 buf.append(", ");
103 }
104 }
105 buf.append(']');
106 return buf.toString();
107 }
108
109 //-- Internals only below this point
110 private boolean isInRange(int index) {
111 return index >= 0 && index < list.size();
112 }
113
114 private Object wrapObject(Object obj) {
115 return factory.newJSJavaWrapper(obj);
116 }
117
118 private final List list;
119 private final JSJavaFactory factory;
120 }