KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > LazyList


1 // ========================================================================
2
// $Id: LazyList.java,v 1.18 2005/05/12 08:48:20 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ListIterator JavaDoc;
23
24 /* ------------------------------------------------------------ */
25 /** Lazy List creation.
26  * A List helper class that attempts to avoid unneccessary List
27  * creation. If a method needs to create a List to return, but it is
28  * expected that this will either be empty or frequently contain a
29  * single item, then using LazyList will avoid additional object
30  * creations by using Collections.EMPTY_LIST or
31  * Collections.singletonList where possible.
32  *
33  * <p><h4>Usage</h4>
34  * <pre>
35  * Object lazylist =null;
36  * while(loopCondition)
37  * {
38  * Object item = getItem();
39  * if (item.isToBeAdded())
40  * lazylist = LazyList.add(lazylist,item);
41  * }
42  * return LazyList.getList(lazylist);
43  * </pre>
44  *
45  * An ArrayList of default size is used as the initial LazyList.
46  *
47  * @see java.util.List
48  * @version $Revision: 1.18 $
49  * @author Greg Wilkins (gregw)
50  */

51 public class LazyList
52 {
53     private static final String JavaDoc[] __EMTPY_STRING_ARRAY = new String JavaDoc[0];
54     
55     /* ------------------------------------------------------------ */
56     private LazyList()
57     {}
58     
59     /* ------------------------------------------------------------ */
60     /** Add an item to a LazyList
61      * @param list The list to add to or null if none yet created.
62      * @param item The item to add.
63      * @return The lazylist created or added to.
64      */

65     public static Object JavaDoc add(Object JavaDoc list, Object JavaDoc item)
66     {
67         if (list==null)
68         {
69             if (item instanceof List JavaDoc || item==null)
70             {
71                 List JavaDoc l = new ArrayList JavaDoc();
72                 l.add(item);
73                 return l;
74             }
75
76             return item;
77         }
78
79         if (list instanceof List JavaDoc)
80         {
81             ((List JavaDoc)list).add(item);
82             return list;
83         }
84
85         List JavaDoc l=new ArrayList JavaDoc();
86         l.add(list);
87         l.add(item);
88         return l;
89     }
90
91     /* ------------------------------------------------------------ */
92     /** Add an item to a LazyList
93      * @param list The list to add to or null if none yet created.
94      * @param index The index to add the item at.
95      * @param item The item to add.
96      * @return The lazylist created or added to.
97      */

98     public static Object JavaDoc add(Object JavaDoc list, int index, Object JavaDoc item)
99     {
100         if (list==null)
101         {
102             if (index>0 || item instanceof List JavaDoc || item==null)
103             {
104                 List JavaDoc l = new ArrayList JavaDoc();
105                 l.add(index,item);
106                 return l;
107             }
108             return item;
109         }
110
111         if (list instanceof List JavaDoc)
112         {
113             ((List JavaDoc)list).add(index,item);
114             return list;
115         }
116
117         List JavaDoc l=new ArrayList JavaDoc();
118         l.add(list);
119         l.add(index,item);
120         return l;
121     }
122
123     
124     /* ------------------------------------------------------------ */
125     /** Add the contents of a Collection to a LazyList
126      * @param list The list to add to or null if none yet created.
127      * @param collection The Collection whose contents should be added.
128      * @return The lazylist created or added to.
129      * @deprecated Use addCollection
130      */

131     protected Object JavaDoc add(Object JavaDoc list, Collection JavaDoc collection)
132     {
133         Iterator JavaDoc i=collection.iterator();
134         while(i.hasNext())
135             list=LazyList.add(list,i.next());
136         return list;
137     }
138     
139     /* ------------------------------------------------------------ */
140     /** Add the contents of a Collection to a LazyList
141      * @param list The list to add to or null if none yet created.
142      * @param collection The Collection whose contents should be added.
143      * @return The lazylist created or added to.
144      */

145     public static Object JavaDoc addCollection(Object JavaDoc list, Collection JavaDoc collection)
146     {
147         Iterator JavaDoc i=collection.iterator();
148         while(i.hasNext())
149             list=LazyList.add(list,i.next());
150         return list;
151     }
152
153     /* ------------------------------------------------------------ */
154     public static Object JavaDoc ensureSize(Object JavaDoc list, int initialSize)
155     {
156         if (list==null)
157             return new ArrayList JavaDoc(initialSize);
158         if (list instanceof ArrayList JavaDoc)
159             return list;
160         List JavaDoc l= new ArrayList JavaDoc(initialSize);
161         l.add(list);
162         return l;
163     }
164
165     /* ------------------------------------------------------------ */
166     public static Object JavaDoc remove(Object JavaDoc list, Object JavaDoc o)
167     {
168         if (list==null)
169             return null;
170
171         if (list instanceof List JavaDoc)
172         {
173             List JavaDoc l = (List JavaDoc)list;
174             l.remove(o);
175             if (l.size()==0)
176                 return null;
177             return list;
178         }
179
180         if (list.equals(o))
181             return null;
182         return list;
183     }
184     
185     /* ------------------------------------------------------------ */
186     public static Object JavaDoc remove(Object JavaDoc list, int i)
187     {
188         if (list==null)
189             return null;
190
191         if (list instanceof List JavaDoc)
192         {
193             List JavaDoc l = (List JavaDoc)list;
194             l.remove(i);
195             if (l.size()==0)
196                 return null;
197             return list;
198         }
199
200         if (i==0)
201             return null;
202         return list;
203     }
204     
205     
206     
207     /* ------------------------------------------------------------ */
208     /** Get the real List from a LazyList.
209      *
210      * @param list A LazyList returned from LazyList.add(Object)
211      * @return The List of added items, which may be an EMPTY_LIST
212      * or a SingletonList.
213      */

214     public static List JavaDoc getList(Object JavaDoc list)
215     {
216         return getList(list,false);
217     }
218     
219
220     /* ------------------------------------------------------------ */
221     /** Get the real List from a LazyList.
222      *
223      * @param list A LazyList returned from LazyList.add(Object) or null
224      * @param nullForEmpty If true, null is returned instead of an
225      * empty list.
226      * @return The List of added items, which may be null, an EMPTY_LIST
227      * or a SingletonList.
228      */

229     public static List JavaDoc getList(Object JavaDoc list, boolean nullForEmpty)
230     {
231         if (list==null)
232             return nullForEmpty?null:Collections.EMPTY_LIST;
233         if (list instanceof List JavaDoc)
234             return (List JavaDoc)list;
235         
236         List JavaDoc l = new ArrayList JavaDoc(1);
237         l.add(list);
238         return l;
239     }
240
241     
242     /* ------------------------------------------------------------ */
243     public static String JavaDoc[] toStringArray(Object JavaDoc list)
244     {
245         if (list==null)
246             return __EMTPY_STRING_ARRAY;
247         
248         if (list instanceof List JavaDoc)
249         {
250             List JavaDoc l = (List JavaDoc)list;
251             
252             String JavaDoc[] a = new String JavaDoc[l.size()];
253             for (int i=l.size();i-->0;)
254             {
255                 Object JavaDoc o=l.get(i);
256                 if (o!=null)
257                     a[i]=o.toString();
258             }
259             return a;
260         }
261         
262         return new String JavaDoc[] {list.toString()};
263     }
264
265
266     /* ------------------------------------------------------------ */
267     /** The size of a lazy List
268      * @param list A LazyList returned from LazyList.add(Object) or null
269      * @return the size of the list.
270      */

271     public static int size(Object JavaDoc list)
272     {
273         if (list==null)
274             return 0;
275         if (list instanceof List JavaDoc)
276             return ((List JavaDoc)list).size();
277         return 1;
278     }
279     
280     /* ------------------------------------------------------------ */
281     /** Get item from the list
282      * @param list A LazyList returned from LazyList.add(Object) or null
283      * @param i int index
284      * @return the item from the list.
285      */

286     public static Object JavaDoc get(Object JavaDoc list, int i)
287     {
288         if (list==null)
289             throw new IndexOutOfBoundsException JavaDoc();
290         
291         if (list instanceof List JavaDoc)
292             return ((List JavaDoc)list).get(i);
293
294         if (i==0)
295             return list;
296         
297         throw new IndexOutOfBoundsException JavaDoc();
298     }
299     
300     /* ------------------------------------------------------------ */
301     public static boolean contains(Object JavaDoc list,Object JavaDoc item)
302     {
303         if (list==null)
304             return false;
305         
306         if (list instanceof List JavaDoc)
307             return ((List JavaDoc)list).contains(item);
308
309         return list.equals(item);
310     }
311     
312
313     /* ------------------------------------------------------------ */
314     public static Object JavaDoc clone(Object JavaDoc list)
315     {
316         if (list==null)
317             return null;
318         if (list instanceof List JavaDoc)
319             return new ArrayList JavaDoc((List JavaDoc)list);
320         return list;
321     }
322     
323     /* ------------------------------------------------------------ */
324     public static String JavaDoc toString(Object JavaDoc list)
325     {
326         if (list==null)
327             return "[]";
328         if (list instanceof List JavaDoc)
329             return ((List JavaDoc)list).toString();
330         return "["+list+"]";
331     }
332
333     /* ------------------------------------------------------------ */
334     public static Iterator JavaDoc iterator(Object JavaDoc list)
335     {
336         if (list==null)
337             return Collections.EMPTY_LIST.iterator();
338         if (list instanceof List JavaDoc)
339             return ((List JavaDoc)list).iterator();
340         return getList(list).iterator();
341     }
342     
343     /* ------------------------------------------------------------ */
344     public static ListIterator JavaDoc listIterator(Object JavaDoc list)
345     {
346         if (list==null)
347             return Collections.EMPTY_LIST.listIterator();
348         if (list instanceof List JavaDoc)
349             return ((List JavaDoc)list).listIterator();
350         return getList(list).listIterator();
351     }
352     
353 }
354
355
Popular Tags