1 16 package org.mortbay.util; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.ListIterator ; 23 24 25 51 public class LazyList 52 { 53 private static final String [] __EMTPY_STRING_ARRAY = new String [0]; 54 55 56 private LazyList() 57 {} 58 59 60 65 public static Object add(Object list, Object item) 66 { 67 if (list==null) 68 { 69 if (item instanceof List || item==null) 70 { 71 List l = new ArrayList (); 72 l.add(item); 73 return l; 74 } 75 76 return item; 77 } 78 79 if (list instanceof List ) 80 { 81 ((List )list).add(item); 82 return list; 83 } 84 85 List l=new ArrayList (); 86 l.add(list); 87 l.add(item); 88 return l; 89 } 90 91 92 98 public static Object add(Object list, int index, Object item) 99 { 100 if (list==null) 101 { 102 if (index>0 || item instanceof List || item==null) 103 { 104 List l = new ArrayList (); 105 l.add(index,item); 106 return l; 107 } 108 return item; 109 } 110 111 if (list instanceof List ) 112 { 113 ((List )list).add(index,item); 114 return list; 115 } 116 117 List l=new ArrayList (); 118 l.add(list); 119 l.add(index,item); 120 return l; 121 } 122 123 124 125 131 protected Object add(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 145 public static Object addCollection(Object list, Collection collection) 146 { 147 Iterator i=collection.iterator(); 148 while(i.hasNext()) 149 list=LazyList.add(list,i.next()); 150 return list; 151 } 152 153 154 public static Object ensureSize(Object list, int initialSize) 155 { 156 if (list==null) 157 return new ArrayList (initialSize); 158 if (list instanceof ArrayList ) 159 return list; 160 List l= new ArrayList (initialSize); 161 l.add(list); 162 return l; 163 } 164 165 166 public static Object remove(Object list, Object o) 167 { 168 if (list==null) 169 return null; 170 171 if (list instanceof List ) 172 { 173 List l = (List )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 remove(Object list, int i) 187 { 188 if (list==null) 189 return null; 190 191 if (list instanceof List ) 192 { 193 List l = (List )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 214 public static List getList(Object list) 215 { 216 return getList(list,false); 217 } 218 219 220 221 229 public static List getList(Object list, boolean nullForEmpty) 230 { 231 if (list==null) 232 return nullForEmpty?null:Collections.EMPTY_LIST; 233 if (list instanceof List ) 234 return (List )list; 235 236 List l = new ArrayList (1); 237 l.add(list); 238 return l; 239 } 240 241 242 243 public static String [] toStringArray(Object list) 244 { 245 if (list==null) 246 return __EMTPY_STRING_ARRAY; 247 248 if (list instanceof List ) 249 { 250 List l = (List )list; 251 252 String [] a = new String [l.size()]; 253 for (int i=l.size();i-->0;) 254 { 255 Object o=l.get(i); 256 if (o!=null) 257 a[i]=o.toString(); 258 } 259 return a; 260 } 261 262 return new String [] {list.toString()}; 263 } 264 265 266 267 271 public static int size(Object list) 272 { 273 if (list==null) 274 return 0; 275 if (list instanceof List ) 276 return ((List )list).size(); 277 return 1; 278 } 279 280 281 286 public static Object get(Object list, int i) 287 { 288 if (list==null) 289 throw new IndexOutOfBoundsException (); 290 291 if (list instanceof List ) 292 return ((List )list).get(i); 293 294 if (i==0) 295 return list; 296 297 throw new IndexOutOfBoundsException (); 298 } 299 300 301 public static boolean contains(Object list,Object item) 302 { 303 if (list==null) 304 return false; 305 306 if (list instanceof List ) 307 return ((List )list).contains(item); 308 309 return list.equals(item); 310 } 311 312 313 314 public static Object clone(Object list) 315 { 316 if (list==null) 317 return null; 318 if (list instanceof List ) 319 return new ArrayList ((List )list); 320 return list; 321 } 322 323 324 public static String toString(Object list) 325 { 326 if (list==null) 327 return "[]"; 328 if (list instanceof List ) 329 return ((List )list).toString(); 330 return "["+list+"]"; 331 } 332 333 334 public static Iterator iterator(Object list) 335 { 336 if (list==null) 337 return Collections.EMPTY_LIST.iterator(); 338 if (list instanceof List ) 339 return ((List )list).iterator(); 340 return getList(list).iterator(); 341 } 342 343 344 public static ListIterator listIterator(Object list) 345 { 346 if (list==null) 347 return Collections.EMPTY_LIST.listIterator(); 348 if (list instanceof List ) 349 return ((List )list).listIterator(); 350 return getList(list).listIterator(); 351 } 352 353 } 354 355 | Popular Tags |