View Javadoc

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      private static final String[] __EMTPY_STRING_ARRAY = new String[0];
69  
70      /* ------------------------------------------------------------ */
71      private LazyList()
72      {}
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          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     /** 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         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     /** 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         Iterator<?> i=collection.iterator();
149         while(i.hasNext())
150             list=LazyList.add(list,i.next());
151         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         for(int i=0;array!=null && i<array.length;i++)
163             list=LazyList.add(list,array[i]);
164         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         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     /** 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         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         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     /** 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         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     /** 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         if (list==null)
330             return 0;
331         if (list instanceof List)
332             return ((List<?>)list).size();
333         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         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      * @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         if (array==null || array.length==0)
431             return new ArrayList<E>();
432         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         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