Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LazyList |
|
| 0.0;0 |
1 | /* | |
2 | * $Id: $ | |
3 | * ------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | package org.mule.transport.http.multipart; | |
11 | ||
12 | // ======================================================================== | |
13 | // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd. | |
14 | // ------------------------------------------------------------------------ | |
15 | // All rights reserved. This program and the accompanying materials | |
16 | // are made available under the terms of the Eclipse Public License v1.0 | |
17 | // and Apache License v2.0 which accompanies this distribution. | |
18 | // The Eclipse Public License is available at | |
19 | // http://www.eclipse.org/legal/epl-v10.html | |
20 | // The Apache License v2.0 is available at | |
21 | // http://www.opensource.org/licenses/apache2.0.php | |
22 | // You may elect to redistribute this code under either of these licenses. | |
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 | /** Lazy List creation. | |
37 | * A List helper class that attempts to avoid unnecessary List | |
38 | * creation. If a method needs to create a List to return, but it is | |
39 | * expected that this will either be empty or frequently contain a | |
40 | * single item, then using LazyList will avoid additional object | |
41 | * creations by using Collections.EMPTY_LIST or | |
42 | * Collections.singletonList where possible. | |
43 | * <p> | |
44 | * LazyList works by passing an opaque representation of the list in | |
45 | * and out of all the LazyList methods. This opaque object is either | |
46 | * null for an empty list, an Object for a list with a single entry | |
47 | * or an ArrayList<Object> for a list of items. | |
48 | * | |
49 | * <p><h4>Usage</h4> | |
50 | * <pre> | |
51 | * Object lazylist =null; | |
52 | * while(loopCondition) | |
53 | * { | |
54 | * Object item = getItem(); | |
55 | * if (item.isToBeAdded()) | |
56 | * lazylist = LazyList.add(lazylist,item); | |
57 | * } | |
58 | * return LazyList.getList(lazylist); | |
59 | * </pre> | |
60 | * | |
61 | * An ArrayList of default size is used as the initial LazyList. | |
62 | * | |
63 | * @see java.util.List | |
64 | */ | |
65 | public class LazyList | |
66 | implements Cloneable, Serializable | |
67 | { | |
68 | 0 | private static final String[] __EMTPY_STRING_ARRAY = new String[0]; |
69 | ||
70 | /* ------------------------------------------------------------ */ | |
71 | private LazyList() | |
72 | 0 | {} |
73 | ||
74 | /* ------------------------------------------------------------ */ | |
75 | /** Add an item to a LazyList | |
76 | * @param list The list to add to or null if none yet created. | |
77 | * @param item The item to add. | |
78 | * @return The lazylist created or added to. | |
79 | */ | |
80 | @SuppressWarnings("unchecked") | |
81 | public static Object add(Object list, Object item) | |
82 | { | |
83 | 0 | if (list==null) |
84 | { | |
85 | 0 | if (item instanceof List || item==null) |
86 | { | |
87 | 0 | List<Object> l = new ArrayList<Object>(); |
88 | 0 | l.add(item); |
89 | 0 | return l; |
90 | } | |
91 | ||
92 | 0 | return item; |
93 | } | |
94 | ||
95 | 0 | if (list instanceof List) |
96 | { | |
97 | 0 | ((List<Object>)list).add(item); |
98 | 0 | return list; |
99 | } | |
100 | ||
101 | 0 | List<Object> l=new ArrayList<Object>(); |
102 | 0 | l.add(list); |
103 | 0 | l.add(item); |
104 | 0 | return l; |
105 | } | |
106 | ||
107 | /* ------------------------------------------------------------ */ | |
108 | /** Add an item to a LazyList | |
109 | * @param list The list to add to or null if none yet created. | |
110 | * @param index The index to add the item at. | |
111 | * @param item The item to add. | |
112 | * @return The lazylist created or added to. | |
113 | */ | |
114 | @SuppressWarnings("unchecked") | |
115 | public static Object add(Object list, int index, Object item) | |
116 | { | |
117 | 0 | if (list==null) |
118 | { | |
119 | 0 | if (index>0 || item instanceof List || item==null) |
120 | { | |
121 | 0 | List<Object> l = new ArrayList<Object>(); |
122 | 0 | l.add(index,item); |
123 | 0 | return l; |
124 | } | |
125 | 0 | return item; |
126 | } | |
127 | ||
128 | 0 | if (list instanceof List) |
129 | { | |
130 | 0 | ((List<Object>)list).add(index,item); |
131 | 0 | return list; |
132 | } | |
133 | ||
134 | 0 | List<Object> l=new ArrayList<Object>(); |
135 | 0 | l.add(list); |
136 | 0 | l.add(index,item); |
137 | 0 | return l; |
138 | } | |
139 | ||
140 | /* ------------------------------------------------------------ */ | |
141 | /** Add the contents of a Collection to a LazyList | |
142 | * @param list The list to add to or null if none yet created. | |
143 | * @param collection The Collection whose contents should be added. | |
144 | * @return The lazylist created or added to. | |
145 | */ | |
146 | public static Object addCollection(Object list, Collection<?> collection) | |
147 | { | |
148 | 0 | Iterator<?> i=collection.iterator(); |
149 | 0 | while(i.hasNext()) |
150 | 0 | list=LazyList.add(list,i.next()); |
151 | 0 | return list; |
152 | } | |
153 | ||
154 | /* ------------------------------------------------------------ */ | |
155 | /** Add the contents of an array to a LazyList | |
156 | * @param list The list to add to or null if none yet created. | |
157 | * @param array The array whose contents should be added. | |
158 | * @return The lazylist created or added to. | |
159 | */ | |
160 | public static Object addArray(Object list, Object[] array) | |
161 | { | |
162 | 0 | for(int i=0;array!=null && i<array.length;i++) |
163 | 0 | list=LazyList.add(list,array[i]); |
164 | 0 | return list; |
165 | } | |
166 | ||
167 | /* ------------------------------------------------------------ */ | |
168 | /** Ensure the capcity of the underlying list. | |
169 | * | |
170 | */ | |
171 | public static Object ensureSize(Object list, int initialSize) | |
172 | { | |
173 | 0 | if (list==null) |
174 | 0 | return new ArrayList<Object>(initialSize); |
175 | 0 | if (list instanceof ArrayList) |
176 | { | |
177 | 0 | ArrayList<?> ol=(ArrayList<?>)list; |
178 | 0 | if (ol.size()>initialSize) |
179 | 0 | return ol; |
180 | 0 | ArrayList<Object> nl = new ArrayList<Object>(initialSize); |
181 | 0 | nl.addAll(ol); |
182 | 0 | return nl; |
183 | } | |
184 | 0 | List<Object> l= new ArrayList<Object>(initialSize); |
185 | 0 | l.add(list); |
186 | 0 | return l; |
187 | } | |
188 | ||
189 | /* ------------------------------------------------------------ */ | |
190 | public static Object remove(Object list, Object o) | |
191 | { | |
192 | 0 | if (list==null) |
193 | 0 | return null; |
194 | ||
195 | 0 | if (list instanceof List) |
196 | { | |
197 | 0 | List<?> l = (List<?>)list; |
198 | 0 | l.remove(o); |
199 | 0 | if (l.size()==0) |
200 | 0 | return null; |
201 | 0 | return list; |
202 | } | |
203 | ||
204 | 0 | if (list.equals(o)) |
205 | 0 | return null; |
206 | 0 | return list; |
207 | } | |
208 | ||
209 | /* ------------------------------------------------------------ */ | |
210 | public static Object remove(Object list, int i) | |
211 | { | |
212 | 0 | if (list==null) |
213 | 0 | return null; |
214 | ||
215 | 0 | if (list instanceof List) |
216 | { | |
217 | 0 | List<?> l = (List<?>)list; |
218 | 0 | l.remove(i); |
219 | 0 | if (l.size()==0) |
220 | 0 | return null; |
221 | 0 | return list; |
222 | } | |
223 | ||
224 | 0 | if (i==0) |
225 | 0 | return null; |
226 | 0 | return list; |
227 | } | |
228 | ||
229 | ||
230 | ||
231 | /* ------------------------------------------------------------ */ | |
232 | /** Get the real List from a LazyList. | |
233 | * | |
234 | * @param list A LazyList returned from LazyList.add(Object) | |
235 | * @return The List of added items, which may be an EMPTY_LIST | |
236 | * or a SingletonList. | |
237 | */ | |
238 | public static<E> List<E> getList(Object list) | |
239 | { | |
240 | 0 | return getList(list,false); |
241 | } | |
242 | ||
243 | ||
244 | /* ------------------------------------------------------------ */ | |
245 | /** Get the real List from a LazyList. | |
246 | * | |
247 | * @param list A LazyList returned from LazyList.add(Object) or null | |
248 | * @param nullForEmpty If true, null is returned instead of an | |
249 | * empty list. | |
250 | * @return The List of added items, which may be null, an EMPTY_LIST | |
251 | * or a SingletonList. | |
252 | */ | |
253 | @SuppressWarnings("unchecked") | |
254 | public static<E> List<E> getList(Object list, boolean nullForEmpty) | |
255 | { | |
256 | 0 | if (list==null) |
257 | { | |
258 | 0 | if (nullForEmpty) |
259 | 0 | return null; |
260 | 0 | return Collections.emptyList(); |
261 | } | |
262 | 0 | if (list instanceof List) |
263 | 0 | return (List<E>)list; |
264 | ||
265 | 0 | return (List<E>)Collections.singletonList(list); |
266 | } | |
267 | ||
268 | ||
269 | /* ------------------------------------------------------------ */ | |
270 | public static String[] toStringArray(Object list) | |
271 | { | |
272 | 0 | if (list==null) |
273 | 0 | return __EMTPY_STRING_ARRAY; |
274 | ||
275 | 0 | if (list instanceof List) |
276 | { | |
277 | 0 | List<?> l = (List<?>)list; |
278 | 0 | String[] a = new String[l.size()]; |
279 | 0 | for (int i=l.size();i-->0;) |
280 | { | |
281 | 0 | Object o=l.get(i); |
282 | 0 | if (o!=null) |
283 | 0 | a[i]=o.toString(); |
284 | 0 | } |
285 | 0 | return a; |
286 | } | |
287 | ||
288 | 0 | return new String[] {list.toString()}; |
289 | } | |
290 | ||
291 | /* ------------------------------------------------------------ */ | |
292 | /** Convert a lazylist to an array | |
293 | * @param list The list to convert | |
294 | * @param clazz The class of the array, which may be a primitive type | |
295 | * @return array of the lazylist entries passed in | |
296 | */ | |
297 | @SuppressWarnings("unchecked") | |
298 | public static Object toArray(Object list,Class<?> clazz) | |
299 | { | |
300 | 0 | if (list==null) |
301 | 0 | return Array.newInstance(clazz,0); |
302 | ||
303 | 0 | if (list instanceof List) |
304 | { | |
305 | 0 | List<?> l = (List<?>)list; |
306 | 0 | if (clazz.isPrimitive()) |
307 | { | |
308 | 0 | Object a = Array.newInstance(clazz,l.size()); |
309 | 0 | for (int i=0;i<l.size();i++) |
310 | 0 | Array.set(a,i,l.get(i)); |
311 | 0 | return a; |
312 | } | |
313 | 0 | return l.toArray((Object[])Array.newInstance(clazz,l.size())); |
314 | ||
315 | } | |
316 | ||
317 | 0 | Object a = Array.newInstance(clazz,1); |
318 | 0 | Array.set(a,0,list); |
319 | 0 | return a; |
320 | } | |
321 | ||
322 | /* ------------------------------------------------------------ */ | |
323 | /** The size of a lazy List | |
324 | * @param list A LazyList returned from LazyList.add(Object) or null | |
325 | * @return the size of the list. | |
326 | */ | |
327 | public static int size(Object list) | |
328 | { | |
329 | 0 | if (list==null) |
330 | 0 | return 0; |
331 | 0 | if (list instanceof List) |
332 | 0 | return ((List<?>)list).size(); |
333 | 0 | return 1; |
334 | } | |
335 | ||
336 | /* ------------------------------------------------------------ */ | |
337 | /** Get item from the list | |
338 | * @param list A LazyList returned from LazyList.add(Object) or null | |
339 | * @param i int index | |
340 | * @return the item from the list. | |
341 | */ | |
342 | @SuppressWarnings("unchecked") | |
343 | public static <E> E get(Object list, int i) | |
344 | { | |
345 | 0 | if (list==null) |
346 | 0 | throw new IndexOutOfBoundsException(); |
347 | ||
348 | 0 | if (list instanceof List) |
349 | 0 | return (E)((List<?>)list).get(i); |
350 | ||
351 | 0 | if (i==0) |
352 | 0 | return (E)list; |
353 | ||
354 | 0 | throw new IndexOutOfBoundsException(); |
355 | } | |
356 | ||
357 | /* ------------------------------------------------------------ */ | |
358 | public static boolean contains(Object list,Object item) | |
359 | { | |
360 | 0 | if (list==null) |
361 | 0 | return false; |
362 | ||
363 | 0 | if (list instanceof List) |
364 | 0 | return ((List<?>)list).contains(item); |
365 | ||
366 | 0 | return list.equals(item); |
367 | } | |
368 | ||
369 | ||
370 | /* ------------------------------------------------------------ */ | |
371 | public static Object clone(Object list) | |
372 | { | |
373 | 0 | if (list==null) |
374 | 0 | return null; |
375 | 0 | if (list instanceof List) |
376 | 0 | return new ArrayList<Object>((List<?>)list); |
377 | 0 | return list; |
378 | } | |
379 | ||
380 | /* ------------------------------------------------------------ */ | |
381 | public static String toString(Object list) | |
382 | { | |
383 | 0 | if (list==null) |
384 | 0 | return "[]"; |
385 | 0 | if (list instanceof List) |
386 | 0 | return list.toString(); |
387 | 0 | return "["+list+"]"; |
388 | } | |
389 | ||
390 | /* ------------------------------------------------------------ */ | |
391 | @SuppressWarnings("unchecked") | |
392 | public static<E> Iterator<E> iterator(Object list) | |
393 | { | |
394 | 0 | if (list==null) |
395 | { | |
396 | 0 | List<E> empty=Collections.emptyList(); |
397 | 0 | return empty.iterator(); |
398 | } | |
399 | 0 | if (list instanceof List) |
400 | { | |
401 | 0 | return ((List<E>)list).iterator(); |
402 | } | |
403 | 0 | List<E> l=getList(list); |
404 | 0 | return l.iterator(); |
405 | } | |
406 | ||
407 | /* ------------------------------------------------------------ */ | |
408 | @SuppressWarnings("unchecked") | |
409 | public static<E> ListIterator<E> listIterator(Object list) | |
410 | { | |
411 | 0 | if (list==null) |
412 | { | |
413 | 0 | List<E> empty=Collections.emptyList(); |
414 | 0 | return empty.listIterator(); |
415 | } | |
416 | 0 | if (list instanceof List) |
417 | 0 | return ((List<E>)list).listIterator(); |
418 | ||
419 | 0 | List<E> l=getList(list); |
420 | 0 | return l.listIterator(); |
421 | } | |
422 | ||
423 | /* ------------------------------------------------------------ */ | |
424 | /** | |
425 | * @param array Any array of object | |
426 | * @return A new <i>modifiable</i> list initialised with the elements from <code>array</code>. | |
427 | */ | |
428 | public static<E> List<E> array2List(E[] array) | |
429 | { | |
430 | 0 | if (array==null || array.length==0) |
431 | 0 | return new ArrayList<E>(); |
432 | 0 | return new ArrayList<E>(Arrays.asList(array)); |
433 | } | |
434 | ||
435 | /* ------------------------------------------------------------ */ | |
436 | /** Add element to an array | |
437 | * @param array The array to add to (or null) | |
438 | * @param item The item to add | |
439 | * @param type The type of the array (in case of null array) | |
440 | * @return new array with contents of array plus item | |
441 | */ | |
442 | @SuppressWarnings("unchecked") | |
443 | public static Object[] addToArray(Object[] array, Object item, Class<?> type) | |
444 | { | |
445 | 0 | if (array==null) |
446 | { | |
447 | 0 | if (type==null && item!=null) |
448 | 0 | type= item.getClass(); |
449 | 0 | Object[] na = (Object[])Array.newInstance(type, 1); |
450 | 0 | na[0]=item; |
451 | 0 | return na; |
452 | } | |
453 | else | |
454 | { | |
455 | 0 | Class<?> c = array.getClass().getComponentType(); |
456 | 0 | Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)+1); |
457 | 0 | System.arraycopy(array, 0, na, 0, array.length); |
458 | 0 | na[array.length]=item; |
459 | 0 | return na; |
460 | } | |
461 | } | |
462 | ||
463 | /* ------------------------------------------------------------ */ | |
464 | @SuppressWarnings("unchecked") | |
465 | public static Object removeFromArray(Object[] array, Object item) | |
466 | { | |
467 | 0 | if (item==null || array==null) |
468 | 0 | return array; |
469 | 0 | for (int i=array.length;i-->0;) |
470 | { | |
471 | 0 | if (item.equals(array[i])) |
472 | { | |
473 | 0 | Class<?> c = array==null?item.getClass():array.getClass().getComponentType(); |
474 | 0 | Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)-1); |
475 | 0 | if (i>0) |
476 | 0 | System.arraycopy(array, 0, na, 0, i); |
477 | 0 | if (i+1<array.length) |
478 | 0 | System.arraycopy(array, i+1, na, i, array.length-(i+1)); |
479 | 0 | return na; |
480 | } | |
481 | } | |
482 | 0 | return array; |
483 | } | |
484 | ||
485 | } | |
486 |