View Javadoc
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      private static final String[] __EMTPY_STRING_ARRAY = new String[0];
66  
67      /* ------------------------------------------------------------ */
68      private LazyList()
69      {}
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          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     /** 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         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     /** 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         Iterator<?> i=collection.iterator();
146         while(i.hasNext())
147             list=LazyList.add(list,i.next());
148         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         for(int i=0;array!=null && i<array.length;i++)
160             list=LazyList.add(list,array[i]);
161         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         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     /** 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         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         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     /** 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         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     /** 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         if (list==null)
327             return 0;
328         if (list instanceof List)
329             return ((List<?>)list).size();
330         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         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      * @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         if (array==null || array.length==0)
428             return new ArrayList<E>();
429         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         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