1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.util;
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class LazyList
53 implements Cloneable, Serializable
54 {
55 private static final String[] __EMTPY_STRING_ARRAY = new String[0];
56
57
58 private LazyList()
59 {}
60
61
62
63
64
65
66
67 public static Object add(Object list, Object item)
68 {
69 if (list==null)
70 {
71 if (item instanceof List || item==null)
72 {
73 List l = new ArrayList();
74 l.add(item);
75 return l;
76 }
77
78 return item;
79 }
80
81 if (list instanceof List)
82 {
83 ((List)list).add(item);
84 return list;
85 }
86
87 List l=new ArrayList();
88 l.add(list);
89 l.add(item);
90 return l;
91 }
92
93
94
95
96
97
98
99
100 public static Object add(Object list, int index, Object item)
101 {
102 if (list==null)
103 {
104 if (index>0 || item instanceof List || item==null)
105 {
106 List l = new ArrayList();
107 l.add(index,item);
108 return l;
109 }
110 return item;
111 }
112
113 if (list instanceof List)
114 {
115 ((List)list).add(index,item);
116 return list;
117 }
118
119 List l=new ArrayList();
120 l.add(list);
121 l.add(index,item);
122 return l;
123 }
124
125
126
127
128
129
130
131 public static Object addCollection(Object list, Collection collection)
132 {
133 Iterator i=collection.iterator();
134 while(i.hasNext())
135 list=LazyList.add(list,i.next());
136 return list;
137 }
138
139
140
141
142
143
144
145 public static Object addArray(Object list, Object[] array)
146 {
147 for(int i=0;array!=null && i<array.length;i++)
148 list=LazyList.add(list,array[i]);
149 return list;
150 }
151
152
153
154
155
156 public static Object ensureSize(Object list, int initialSize)
157 {
158 if (list==null)
159 return new ArrayList(initialSize);
160 if (list instanceof ArrayList)
161 {
162 ArrayList ol=(ArrayList)list;
163 if (ol.size()>initialSize)
164 return ol;
165 ArrayList nl = new ArrayList(initialSize);
166 nl.addAll(ol);
167 return nl;
168 }
169 List l= new ArrayList(initialSize);
170 l.add(list);
171 return l;
172 }
173
174
175 public static Object remove(Object list, Object o)
176 {
177 if (list==null)
178 return null;
179
180 if (list instanceof List)
181 {
182 List l = (List)list;
183 l.remove(o);
184 if (l.size()==0)
185 return null;
186 return list;
187 }
188
189 if (list.equals(o))
190 return null;
191 return list;
192 }
193
194
195 public static Object remove(Object list, int i)
196 {
197 if (list==null)
198 return null;
199
200 if (list instanceof List)
201 {
202 List l = (List)list;
203 l.remove(i);
204 if (l.size()==0)
205 return null;
206 return list;
207 }
208
209 if (i==0)
210 return null;
211 return list;
212 }
213
214
215
216
217
218
219
220
221
222
223 public static List getList(Object list)
224 {
225 return getList(list,false);
226 }
227
228
229
230
231
232
233
234
235
236
237
238 public static List getList(Object list, boolean nullForEmpty)
239 {
240 if (list==null)
241 return nullForEmpty?null:Collections.EMPTY_LIST;
242 if (list instanceof List)
243 return (List)list;
244
245 List l = new ArrayList(1);
246 l.add(list);
247 return l;
248 }
249
250
251
252 public static String[] toStringArray(Object list)
253 {
254 if (list==null)
255 return __EMTPY_STRING_ARRAY;
256
257 if (list instanceof List)
258 {
259 List l = (List)list;
260 String[] a = new String[l.size()];
261 for (int i=l.size();i-->0;)
262 {
263 Object o=l.get(i);
264 if (o!=null)
265 a[i]=o.toString();
266 }
267 return a;
268 }
269
270 return new String[] {list.toString()};
271 }
272
273
274 public static Object toArray(Object list,Class aClass)
275 {
276 if (list==null)
277 return (Object[])Array.newInstance(aClass,0);
278
279 if (list instanceof List)
280 {
281 List l = (List)list;
282 if (aClass.isPrimitive())
283 {
284 Object a = Array.newInstance(aClass,l.size());
285 for (int i=0;i<l.size();i++)
286 Array.set(a,i,l.get(i));
287 return a;
288 }
289 return l.toArray((Object[])Array.newInstance(aClass,l.size()));
290
291 }
292
293 Object a = Array.newInstance(aClass,1);
294 Array.set(a,0,list);
295 return a;
296 }
297
298
299
300
301
302
303 public static int size(Object list)
304 {
305 if (list==null)
306 return 0;
307 if (list instanceof List)
308 return ((List)list).size();
309 return 1;
310 }
311
312
313
314
315
316
317
318 public static Object get(Object list, int i)
319 {
320 if (list==null)
321 throw new IndexOutOfBoundsException();
322
323 if (list instanceof List)
324 return ((List)list).get(i);
325
326 if (i==0)
327 return list;
328
329 throw new IndexOutOfBoundsException();
330 }
331
332
333 public static boolean contains(Object list,Object item)
334 {
335 if (list==null)
336 return false;
337
338 if (list instanceof List)
339 return ((List)list).contains(item);
340
341 return list.equals(item);
342 }
343
344
345
346 public static Object clone(Object list)
347 {
348 if (list==null)
349 return null;
350 if (list instanceof List)
351 return new ArrayList((List)list);
352 return list;
353 }
354
355
356 public static String toString(Object list)
357 {
358 if (list==null)
359 return "[]";
360 if (list instanceof List)
361 return ((List)list).toString();
362 return "["+list+"]";
363 }
364
365
366 public static Iterator iterator(Object list)
367 {
368 if (list==null)
369 return Collections.EMPTY_LIST.iterator();
370 if (list instanceof List)
371 return ((List)list).iterator();
372 return getList(list).iterator();
373 }
374
375
376 public static ListIterator listIterator(Object list)
377 {
378 if (list==null)
379 return Collections.EMPTY_LIST.listIterator();
380 if (list instanceof List)
381 return ((List)list).listIterator();
382 return getList(list).listIterator();
383 }
384
385
386
387
388
389
390 public static List array2List(Object[] array)
391 {
392 if (array==null || array.length==0)
393 return new ArrayList();
394 return new ArrayList(Arrays.asList(array));
395 }
396
397
398
399
400
401
402
403
404 public static Object[] addToArray(Object[] array, Object item, Class type)
405 {
406 if (array==null)
407 {
408 if (type==null && item!=null)
409 type= item.getClass();
410 Object[] na = (Object[])Array.newInstance(type, 1);
411 na[0]=item;
412 return na;
413 }
414 else
415 {
416 Class c = array.getClass().getComponentType();
417 Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)+1);
418 System.arraycopy(array, 0, na, 0, array.length);
419 na[array.length]=item;
420 return na;
421 }
422 }
423
424
425 public static Object[] removeFromArray(Object[] array, Object item)
426 {
427 if (item==null || array==null)
428 return array;
429 for (int i=array.length;i-->0;)
430 {
431 if (item.equals(array[i]))
432 {
433 Class c = array==null?item.getClass():array.getClass().getComponentType();
434 Object[] na = (Object[])Array.newInstance(c, Array.getLength(array)-1);
435 if (i>0)
436 System.arraycopy(array, 0, na, 0, i);
437 if (i+1<array.length)
438 System.arraycopy(array, i+1, na, i, array.length-(i+1));
439 return na;
440 }
441 }
442 return array;
443 }
444
445 }
446