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