1 package org.sentrysoftware.jawk.jrt;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.TreeMap;
31
32 import org.sentrysoftware.jawk.intermediate.UninitializedObject;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class AssocArray implements Comparator<Object> {
48
49 private Map<Object, Object> map;
50
51
52
53
54
55
56 public AssocArray(boolean sortedArrayKeys) {
57 if (sortedArrayKeys) {
58 map = new TreeMap<Object, Object>(this);
59 } else {
60 map = new HashMap<Object, Object>();
61 }
62 }
63
64
65
66
67
68 public static final int MT_HASH = 2;
69
70
71
72
73 public static final int MT_LINKED = 2 << 1;
74
75
76
77
78 public static final int MT_TREE = 2 << 2;
79
80
81
82
83
84
85
86
87 public void useMapType(int mapType) {
88 assert map.isEmpty();
89 switch (mapType) {
90 case MT_HASH:
91 map = new HashMap<Object, Object>();
92 break;
93 case MT_LINKED:
94 map = new LinkedHashMap<Object, Object>();
95 break;
96 case MT_TREE:
97 map = new TreeMap<Object, Object>(this);
98 break;
99 default:
100 throw new Error("Invalid map type : " + mapType);
101 }
102 }
103
104
105
106
107
108
109
110
111 public String mapString() {
112
113
114
115 StringBuilder sb = new StringBuilder().append('{');
116 int cnt = 0;
117 for (Object o : map.keySet()) {
118 if (cnt > 0) {
119 sb.append(", ");
120 }
121 if (o instanceof AssocArray) {
122 sb.append(((AssocArray) o).mapString());
123 } else {
124 sb.append(o.toString());
125 }
126
127 sb.append('=');
128 Object o2 = map.get(o);
129 if (o2 instanceof AssocArray) {
130 sb.append(((AssocArray) o2).mapString());
131 } else {
132 sb.append(o2.toString());
133 }
134 ++cnt;
135 }
136 return sb.append('}').toString();
137 }
138
139
140 private static final UninitializedObject BLANK = new UninitializedObject();
141
142
143
144
145
146
147
148
149
150
151
152
153 public boolean isIn(Object key) {
154 return map.get(key) != null;
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public Object get(Object key) {
168 if (key == null || key instanceof UninitializedObject) {
169 key = (long)0;
170 }
171 Object result = map.get(key);
172 if (result != null) {
173 return result;
174 }
175
176
177 try {
178
179 key = Long.parseLong(key.toString());
180 result = map.get(key);
181 if (result != null) {
182 return result;
183 }
184 } catch (Exception e) {}
185
186
187
188
189 result = BLANK;
190 map.put(key, result);
191
192 return result;
193 }
194
195
196
197
198
199
200
201
202 public Object put(Object key, Object value) {
203 if (key == null || key instanceof UninitializedObject) {
204 key = (long)0;
205 }
206 try {
207
208 long iKey = Long.parseLong(key.toString());
209 return map.put(iKey, value);
210 } catch (Exception e) {
211 }
212
213 return map.put(key, value);
214 }
215
216
217
218
219
220
221
222
223 public Object put(long key, Object value) {
224 return map.put(key, value);
225 }
226
227
228
229
230
231
232 public Set<Object> keySet() {
233 return map.keySet();
234 }
235
236
237
238
239 public void clear() {
240 map.clear();
241 }
242
243
244
245
246
247
248
249 public Object remove(Object key) {
250 return map.remove(key);
251 }
252
253
254
255
256
257
258 @Override
259 public String toString() {
260 throw new AwkRuntimeException("Cannot evaluate an unindexed array.");
261 }
262
263
264
265
266
267
268
269 @Override
270 public int compare(Object o1, Object o2) {
271
272 if (o1 instanceof String || o2 instanceof String) {
273
274 String s1 = o1.toString();
275 String s2 = o2.toString();
276 return s1.compareTo(s2);
277 } else {
278 if (o1 instanceof Double || o2 instanceof Double) {
279 Double d1 = ((Double) o1);
280 Double d2 = ((Double) o2);
281 return d1.compareTo(d2);
282 } else {
283 Integer i1 = ((Integer) o1);
284 Integer i2 = ((Integer) o2);
285 return i1.compareTo(i2);
286 }
287 }
288 }
289
290
291
292
293
294
295 public String getMapVersion() {
296 return map.getClass().getPackage().getSpecificationVersion();
297 }
298 }