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