0
|
1 /*
|
|
2 * Copyright 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 package sun.jvm.hotspot.utilities.soql;
|
|
25
|
|
26 import java.util.Map;
|
|
27 import java.util.HashMap;
|
|
28 import java.util.Collections;
|
|
29 import java.lang.reflect.Method;
|
|
30 import java.lang.reflect.Constructor;
|
|
31 import javax.script.Invocable;
|
|
32
|
|
33 /**
|
|
34 * Simple implementation of ScriptObject interface
|
|
35 * with property storage backed by a Map. This class
|
|
36 * can be extended to override any of the methods, in
|
|
37 * particular to add "function" valued properties.
|
|
38 */
|
|
39 public class MapScriptObject implements ScriptObject {
|
|
40 // map to store the properties
|
|
41 private Map map;
|
|
42
|
|
43 public MapScriptObject() {
|
|
44 this(new HashMap());
|
|
45 }
|
|
46
|
|
47 public MapScriptObject(Map map) {
|
|
48 // make it synchronized
|
|
49 this.map = Collections.synchronizedMap(map);
|
|
50 }
|
|
51
|
|
52 public Object[] getIds() {
|
|
53 return map.keySet().toArray();
|
|
54 }
|
|
55
|
|
56 public Object get(String name) {
|
|
57 if (has(name)) {
|
|
58 return map.get(name);
|
|
59 } else {
|
|
60 return UNDEFINED;
|
|
61 }
|
|
62 }
|
|
63
|
|
64 public Object get(int index) {
|
|
65 if (has(index)) {
|
|
66 Object key = Integer.valueOf(index);
|
|
67 return map.get(key);
|
|
68 } else {
|
|
69 return UNDEFINED;
|
|
70 }
|
|
71 }
|
|
72
|
|
73 public void put(String name, Object value) {
|
|
74 map.put(name, value);
|
|
75 }
|
|
76
|
|
77 public void put(int index, Object value) {
|
|
78 map.put(Integer.valueOf(index), value);
|
|
79 }
|
|
80
|
|
81 public boolean has(String name) {
|
|
82 return map.containsKey(name);
|
|
83 }
|
|
84
|
|
85 public boolean has(int index) {
|
|
86 return map.containsKey(Integer.valueOf(index));
|
|
87 }
|
|
88
|
|
89 public boolean delete(String name) {
|
|
90 if (map.containsKey(name)) {
|
|
91 map.remove(name);
|
|
92 return true;
|
|
93 } else {
|
|
94 return false;
|
|
95 }
|
|
96 }
|
|
97
|
|
98 public boolean delete(int index) {
|
|
99 Object key = Integer.valueOf(index);
|
|
100 if (map.containsKey(key)) {
|
|
101 map.remove(key);
|
|
102 return true;
|
|
103 } else {
|
|
104 return false;
|
|
105 }
|
|
106 }
|
|
107
|
|
108 // add a function valued property that invokes given Method
|
|
109 protected void putFunction(Object target, Method method) {
|
|
110 putFunction(target, method, true);
|
|
111 }
|
|
112
|
|
113 // add a function valued property that invokes given Method
|
|
114 protected void putFunction(Object target, Method method, boolean wrapArgs) {
|
|
115 map.put(method.getName(), new MethodCallable(target, method, wrapArgs));
|
|
116 }
|
|
117
|
|
118 // add a function valued property that invokes given script function
|
|
119 protected void putFunction(Object target, String name, Invocable invocable) {
|
|
120 map.put(name, new InvocableCallable(target, name, invocable));
|
|
121 }
|
|
122 }
|