1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.util.Iterator;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24
25 import org.mozilla.javascript.Scriptable;
26
27
28
29
30
31
32
33
34 public class ScriptableLinkedHashMap<K,V> extends LinkedHashMap<K, V> implements ScriptableMap<K,V>
35 {
36 private static final long serialVersionUID = 3774167893214964123L;
37
38 private Scriptable parentScope;
39 private Scriptable prototype;
40
41
42 public ScriptableLinkedHashMap()
43 {
44 }
45
46 public ScriptableLinkedHashMap(int initialCapacity)
47 {
48 super(initialCapacity);
49 }
50
51 public ScriptableLinkedHashMap(Map<K, V> source)
52 {
53 super(source);
54 }
55
56
57
58
59 public String getClassName()
60 {
61 return "ScriptableMap";
62 }
63
64
65
66
67 public Object get(String name, Scriptable start)
68 {
69
70 if ("length".equals(name))
71 {
72 return this.size();
73 }
74 else
75 {
76 return get(name);
77 }
78 }
79
80
81
82
83 public Object get(int index, Scriptable start)
84 {
85 Object value = null;
86 int i=0;
87 Iterator itrValues = this.values().iterator();
88 while (i++ <= index && itrValues.hasNext())
89 {
90 value = itrValues.next();
91 }
92 return value;
93 }
94
95
96
97
98 public boolean has(String name, Scriptable start)
99 {
100
101 return containsKey(name);
102 }
103
104
105
106
107 public boolean has(int index, Scriptable start)
108 {
109 return (index >= 0 && this.values().size() > index);
110 }
111
112
113
114
115 @SuppressWarnings("unchecked")
116 public void put(String name, Scriptable start, Object value)
117 {
118
119 put((K)name, (V)value);
120 }
121
122
123
124
125 public void put(int index, Scriptable start, Object value)
126 {
127
128 }
129
130
131
132
133 public void delete(String name)
134 {
135
136 remove(name);
137 }
138
139
140
141
142 public void delete(int index)
143 {
144 int i=0;
145 Iterator itrKeys = this.keySet().iterator();
146 while (i <= index && itrKeys.hasNext())
147 {
148 Object key = itrKeys.next();
149 if (i == index)
150 {
151 remove(key);
152 break;
153 }
154 }
155 }
156
157
158
159
160 public Scriptable getPrototype()
161 {
162 return this.prototype;
163 }
164
165
166
167
168 public void setPrototype(Scriptable prototype)
169 {
170 this.prototype = prototype;
171 }
172
173
174
175
176 public Scriptable getParentScope()
177 {
178 return this.parentScope;
179 }
180
181
182
183
184 public void setParentScope(Scriptable parent)
185 {
186 this.parentScope = parent;
187 }
188
189
190
191
192 public Object[] getIds()
193 {
194 return keySet().toArray();
195 }
196
197
198
199
200 public Object getDefaultValue(Class hint)
201 {
202 return null;
203 }
204
205
206
207
208 public boolean hasInstance(Scriptable instance)
209 {
210 return instance instanceof ScriptableLinkedHashMap;
211 }
212 }
213