1
2
3
4
5
6
7 package org.mule.transport.http.multipart;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.lang.reflect.Array;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.ListIterator;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class LazyList
63 implements Cloneable, Serializable
64 {
65 private static final String[] __EMTPY_STRING_ARRAY = new String[0];
66
67
68 private LazyList()
69 {}
70
71
72
73
74
75
76
77 @SuppressWarnings("unchecked")
78 public static Object add(Object list, Object item)
79 {
80 if (list==null)
81 {
82 if (item instanceof List || item==null)
83 {
84 List<Object> l = new ArrayList<Object>();
85 l.add(item);
86 return l;
87 }
88
89 return item;
90 }
91
92 if (list instanceof List)
93 {
94 ((List<Object>)list).add(item);
95 return list;
96 }
97
98 List<Object> l=new ArrayList<Object>();
99 l.add(list);
100 l.add(item);
101 return l;
102 }
103
104
105
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 public static Object add(Object list, int index, Object item)
113 {
114 if (list==null)
115 {
116 if (index>0 || item instanceof List || item==null)
117 {
118 List<Object> l = new ArrayList<Object>();
119 l.add(index,item);
120 return l;
121 }
122 return item;
123 }
124
125 if (list instanceof List)
126 {
127 ((List<Object>)list).add(index,item);
128 return list;
129 }
130
131 List<Object> l=new ArrayList<Object>();
132 l.add(list);
133 l.add(index,item);
134 return l;
135 }
136
137
138
139
140
141
142
143 public static Object addCollection(Object list, Collection<?> collection)
144 {
145 Iterator<?> i=collection.iterator();
146 while(i.hasNext())
147 list=LazyList.add(list,i.next());
148 return list;
149 }
150
151
152
153
154
155
156
157 public static Object addArray(Object list, Object[] array)
158 {
159 for(int i=0;array!=null && i<array.length;i++)
160 list=LazyList.add(list,array[i]);
161 return list;
162 }
163
164
165
166
167
168 public static Object ensureSize(Object list, int initialSize)
169 {
170 if (list==null)
171 return new ArrayList<Object>(initialSize);
172 if (list instanceof ArrayList)
173 {
174 ArrayList<?> ol=(ArrayList<?>)list;
175 if (ol.size()>initialSize)
176 return ol;
177 ArrayList<Object> nl = new ArrayList<Object>(initialSize);
178 nl.addAll(ol);
179 return nl;
180 }
181 List<Object> l= new ArrayList<Object>(initialSize);
182 l.add(list);
183 return l;
184 }
185
186
187 public static Object remove(Object list, Object o)
188 {
189 if (list==null)
190 return null;
191
192 if (list instanceof List)
193 {
194 List<?> l = (List<?>)list;
195 l.remove(o);
196 if (l.size()==0)
197 return null;
198 return list;
199 }
200
201 if (list.equals(o))
202 return null;
203 return list;
204 }
205
206
207 public static Object remove(Object list, int i)
208 {
209 if (list==null)
210 return null;
211
212 if (list instanceof List)
213 {
214 List<?> l = (List<?>)list;
215 l.remove(i);
216 if (l.size()==0)
217 return null;
218 return list;
219 }
220
221 if (i==0)
222 return null;
223 return list;
224 }
225
226
227
228
229
230
231
232
233
234
235 public static<E> List<E> getList(Object list)
236 {
237 return getList(list,false);
238 }
239
240
241
242
243
244
245
246
247
248
249
250 @SuppressWarnings("unchecked")
251 public static<E> List<E> getList(Object list, boolean nullForEmpty)
252 {
253 if (list==null)
254 {
255 if (nullForEmpty)
256 return null;
257 return Collections.emptyList();
258 }
259 if (list instanceof List)
260 return (List<E>)list;
261
262 return (List<E>)Collections.singletonList(list);
263 }
264
265
266
267 public static String[] toStringArray(Object list)
268 {
269 if (list==null)
270 return __EMTPY_STRING_ARRAY;
271
272 if (list instanceof List)
273 {
274 List<?> l = (List<?>)list;
275 String[] a = new String[l.size()];
276 for (int i=l.size();i-->0;)
277 {
278 Object o=l.get(i);
279 if (o!=null)
280 a[i]=o.toString();
281 }
282 return a;
283 }
284
285 return new String[] {list.toString()};
286 }
287
288
289
290
291
292
293
294 @SuppressWarnings("unchecked")
295 public static Object toArray(Object list,Class<?> clazz)
296 {
297 if (list==null)
298 return Array.newInstance(clazz,0);
299
300 if (list instanceof List)
301 {
302 List<?> l = (List<?>)list;
303 if (clazz.isPrimitive())
304 {
305 Object a = Array.newInstance(clazz,l.size());
306 for (int i=0;i<l.size();i++)
307 Array.set(a,i,l.get(i));
308 return a;
309 }
310 return l.toArray((Object[])Array.newInstance(clazz,l.size()));
311
312 }
313
314 Object a = Array.newInstance(clazz,1);
315 Array.set(a,0,list);
316 return a;
317 }
318
319
320
321
322
323
324 public static int size(Object list)
325 {
326 if (list==null)
327 return 0;
328 if (list instanceof List)
329 return ((List<?>)list).size();
330 return 1;
331 }
332
333
334
335
336
337
338
339 @SuppressWarnings("unchecked")
340 public static <E> E get(Object list, int i)
341 {
342 if (list==null)
343 throw new IndexOutOfBoundsException();
344
345 if (list instanceof List)
346 return (E)((List<?>)list).get(i);
347
348 if (i==0)
349 return (E)list;
350
351 throw new IndexOutOfBoundsException();
352 }
353
354
355 public static boolean contains(Object list,Object item)
356 {
357 if (list==null)
358 return false;
359
360 if (list instanceof List)
361 return ((List<?>)list).contains(item);
362
363 return list.equals(item);
364 }
365
366
367
368 public static Object clone(Object list)
369 {
370 if (list==null)
371 return null;
372 if (list instanceof List)
373 return new ArrayList<Object>((List<?>)list);
374 return list;
375 }
376
377
378 public static String toString(Object list)
379 {
380 if (list==null)
381 return "[]";
382 if (list instanceof List)
383 return list.toString();
384 return "["+list+"]";
385 }
386
387
388 @SuppressWarnings("unchecked")
389 public static<E> Iterator<E> iterator(Object list)
390 {
391 if (list==null)
392 {
393 List<E> empty=Collections.emptyList();
394 return empty.iterator();
395 }
396 if (list instanceof List)
397 {
398 return ((List<E>)list).iterator();
399 }
400 List<E> l=getList(list);
401 return l.iterator();
402 }
403
404
405 @SuppressWarnings("unchecked")
406 public static<E> ListIterator<E> listIterator(Object list)
407 {
408 if (list==null)
409 {
410 List<E> empty=Collections.emptyList();
411 return empty.listIterator();
412 }
413 if (list instanceof List)
414 return ((List<E>)list).listIterator();
415
416 List<E> l=getList(list);
417 return l.listIterator();
418 }
419
420
421
422
423
424
425 public static<E> List<E> array2List(E[] array)
426 {
427 if (array==null || array.length==0)
428 return new ArrayList<E>();
429 return new ArrayList<E>(Arrays.asList(array));
430 }
431
432
433
434
435
436
437
438
439 @SuppressWarnings("unchecked")
440 public static Object[] addToArray(Object[] array, Object item, Class<?> type)
441 {
442 if (array==null)
443 {
444 if (type==null && item!=null)
445 type= item.getClass();
446 Object[] na = (Object[])Array.newInstance(type, 1);
447 na[0]=item;
448 return na;
449 }
450 else
451 {
452 Class<?> c = array.getClass().getComponentType();
453 Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)+1);
454 System.arraycopy(array, 0, na, 0, array.length);
455 na[array.length]=item;
456 return na;
457 }
458 }
459
460
461 @SuppressWarnings("unchecked")
462 public static Object removeFromArray(Object[] array, Object item)
463 {
464 if (item==null || array==null)
465 return array;
466 for (int i=array.length;i-->0;)
467 {
468 if (item.equals(array[i]))
469 {
470 Class<?> c = array==null?item.getClass():array.getClass().getComponentType();
471 Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)-1);
472 if (i>0)
473 System.arraycopy(array, 0, na, 0, i);
474 if (i+1<array.length)
475 System.arraycopy(array, i+1, na, i, array.length-(i+1));
476 return na;
477 }
478 }
479 return array;
480 }
481
482 }
483