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