1
2
3
4
5
6
7
8
9
10 package org.mule.transport.http.multipart;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
35
36
37
38
39
40
41
42
43
44
45
46 public class MultiMap<K> implements ConcurrentMap<K,Object>
47 {
48 Map<K,Object> _map;
49 ConcurrentMap<K, Object> _cmap;
50
51 public MultiMap()
52 {
53 _map=new HashMap<K, Object>();
54 }
55
56 public MultiMap(Map map)
57 {
58 if (map instanceof ConcurrentMap)
59 _map=_cmap=new ConcurrentHashMap<K, Object>(map);
60 else
61 _map=new HashMap<K, Object>(map);
62 }
63
64 public MultiMap(int capacity)
65 {
66 _map=new HashMap<K, Object>(capacity);
67 }
68
69 public MultiMap(boolean concurrent)
70 {
71 if (concurrent)
72 _map=_cmap=new ConcurrentHashMap<K, Object>();
73 else
74 _map=new HashMap<K, Object>();
75 }
76
77
78
79
80
81
82
83
84 public List getValues(Object name)
85 {
86 return LazyList.getList(_map.get(name),true);
87 }
88
89
90
91
92
93
94
95
96
97 public Object getValue(Object name,int i)
98 {
99 Object l=_map.get(name);
100 if (i==0 && LazyList.size(l)==0)
101 return null;
102 return LazyList.get(l,i);
103 }
104
105
106
107
108
109
110
111
112
113
114 public String getString(Object name)
115 {
116 Object l=_map.get(name);
117 switch(LazyList.size(l))
118 {
119 case 0:
120 return null;
121 case 1:
122 Object o=LazyList.get(l,0);
123 return o==null?null:o.toString();
124 default:
125 {
126 StringBuilder values=new StringBuilder(128);
127 for (int i=0; i<LazyList.size(l); i++)
128 {
129 Object e=LazyList.get(l,i);
130 if (e!=null)
131 {
132 if (values.length()>0)
133 values.append(',');
134 values.append(e.toString());
135 }
136 }
137 return values.toString();
138 }
139 }
140 }
141
142
143 public Object get(Object name)
144 {
145 Object l=_map.get(name);
146 switch(LazyList.size(l))
147 {
148 case 0:
149 return null;
150 case 1:
151 Object o=LazyList.get(l,0);
152 return o;
153 default:
154 return LazyList.getList(l,true);
155 }
156 }
157
158
159
160
161
162
163
164 public Object put(K name, Object value)
165 {
166 return _map.put(name,LazyList.add(null,value));
167 }
168
169
170
171
172
173
174
175 public Object putValues(K name, List values)
176 {
177 return _map.put(name,values);
178 }
179
180
181
182
183
184
185
186 public Object putValues(K name, String[] values)
187 {
188 Object list=null;
189 for (int i=0;i<values.length;i++)
190 list=LazyList.add(list,values[i]);
191 return put(name,list);
192 }
193
194
195
196
197
198
199
200
201
202 public void add(K name, Object value)
203 {
204 Object lo = _map.get(name);
205 Object ln = LazyList.add(lo,value);
206 if (lo!=ln)
207 _map.put(name,ln);
208 }
209
210
211
212
213
214
215
216
217 public void addValues(K name, List values)
218 {
219 Object lo = _map.get(name);
220 Object ln = LazyList.addCollection(lo,values);
221 if (lo!=ln)
222 _map.put(name,ln);
223 }
224
225
226
227
228
229
230
231
232 public void addValues(K name, String[] values)
233 {
234 Object lo = _map.get(name);
235 Object ln = LazyList.addCollection(lo,Arrays.asList(values));
236 if (lo!=ln)
237 _map.put(name,ln);
238 }
239
240
241
242
243
244
245
246 public boolean removeValue(K name,Object value)
247 {
248 Object lo = _map.get(name);
249 Object ln=lo;
250 int s=LazyList.size(lo);
251 if (s>0)
252 {
253 ln=LazyList.remove(lo,value);
254 if (ln==null)
255 _map.remove(name);
256 else
257 _map.put(name, ln);
258 }
259 return LazyList.size(ln)!=s;
260 }
261
262
263
264
265
266 public void putAll(Map m)
267 {
268 Iterator i = m.entrySet().iterator();
269 boolean multi=m instanceof MultiMap;
270 while(i.hasNext())
271 {
272 Map.Entry entry = (Map.Entry)i.next();
273 if (multi)
274 _map.put((K)(entry.getKey()),LazyList.clone(entry.getValue()));
275 else
276 put((K)(entry.getKey()),entry.getValue());
277 }
278 }
279
280
281
282
283
284 public Map toStringArrayMap()
285 {
286 HashMap map = new HashMap(_map.size()*3/2);
287
288 Iterator i = _map.entrySet().iterator();
289 while(i.hasNext())
290 {
291 Map.Entry entry = (Map.Entry)i.next();
292 Object l = entry.getValue();
293 String[] a = LazyList.toStringArray(l);
294
295
296
297 map.put(entry.getKey(),a);
298 }
299 return map;
300 }
301
302 public void clear()
303 {
304 _map.clear();
305 }
306
307 public boolean containsKey(Object key)
308 {
309 return _map.containsKey(key);
310 }
311
312 public boolean containsValue(Object value)
313 {
314 return _map.containsValue(value);
315 }
316
317 public Set<Entry<K, Object>> entrySet()
318 {
319 return _map.entrySet();
320 }
321
322 @Override
323 public boolean equals(Object o)
324 {
325 return _map.equals(o);
326 }
327
328 @Override
329 public int hashCode()
330 {
331 return _map.hashCode();
332 }
333
334 public boolean isEmpty()
335 {
336 return _map.isEmpty();
337 }
338
339 public Set<K> keySet()
340 {
341 return _map.keySet();
342 }
343
344 public Object remove(Object key)
345 {
346 return _map.remove(key);
347 }
348
349 public int size()
350 {
351 return _map.size();
352 }
353
354 public Collection<Object> values()
355 {
356 return _map.values();
357 }
358
359
360
361 public Object putIfAbsent(K key, Object value)
362 {
363 if (_cmap==null)
364 throw new UnsupportedOperationException();
365 return _cmap.putIfAbsent(key,value);
366 }
367
368 public boolean remove(Object key, Object value)
369 {
370 if (_cmap==null)
371 throw new UnsupportedOperationException();
372 return _cmap.remove(key,value);
373 }
374
375 public boolean replace(K key, Object oldValue, Object newValue)
376 {
377 if (_cmap==null)
378 throw new UnsupportedOperationException();
379 return _cmap.replace(key,oldValue,newValue);
380 }
381
382 public Object replace(K key, Object value)
383 {
384 if (_cmap==null)
385 throw new UnsupportedOperationException();
386 return _cmap.replace(key,value);
387 }
388
389
390 }