View Javadoc

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