1 16 17 package org.apache.velocity.tools.generic; 18 19 import java.util.Collection ; 20 import java.util.Enumeration ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Vector ; 24 25 import org.apache.velocity.util.ArrayIterator; 26 import org.apache.velocity.util.EnumerationIterator; 27 28 77 78 public class IteratorTool implements Iterator { 79 80 81 private Object wrapped; 82 private Iterator iterator; 83 private boolean wantMore; 84 private boolean cachedNext; 85 protected Object next; 86 87 88 94 public IteratorTool() 95 { 96 this(null); 97 } 98 99 100 105 public IteratorTool(Object wrapped) 106 { 107 internalWrap(wrapped); 108 } 109 110 111 123 public IteratorTool wrap(Object list) 124 { 125 if (this.wrapped == null) 126 { 127 return new IteratorTool(list); 128 } 129 else if (list != null) 130 { 131 internalWrap(list); 132 return this; 133 } 134 else 135 { 136 throw new IllegalArgumentException ("Need a valid list to wrap"); 137 } 138 } 139 140 141 153 private void internalWrap(Object wrapped) 154 { 155 if (wrapped != null) 156 { 157 158 if (wrapped.getClass().isArray()) 159 { 160 this.iterator = new ArrayIterator((Object [])wrapped); 161 } 162 else if (wrapped instanceof Collection ) 163 { 164 this.iterator = ((Collection )wrapped).iterator(); 165 } 166 else if (wrapped instanceof Map ) 167 { 168 this.iterator = ((Map )wrapped).values().iterator(); 169 } 170 else if (wrapped instanceof Iterator ) 171 { 172 this.iterator = (Iterator )wrapped; 173 } 174 else if (wrapped instanceof Enumeration ) 175 { 176 this.iterator = new EnumerationIterator((Enumeration )wrapped); 177 } 178 else 179 { 180 182 throw new IllegalArgumentException ("Don't know how to wrap this list"); 183 } 184 185 this.wrapped = wrapped; 186 this.wantMore = true; 187 this.cachedNext = false; 188 } 189 else 190 { 191 this.iterator = null; 192 this.wrapped = null; 193 this.wantMore = false; 194 this.cachedNext = false; 195 } 196 } 197 198 199 207 public void reset() 208 { 209 if (this.wrapped != null) 210 { 211 internalWrap(this.wrapped); 212 } 213 } 214 215 216 234 public Object next() 235 { 236 if (this.wrapped == null) 237 { 238 throw new IllegalStateException ("Use wrap() before calling next()"); 239 } 240 241 if (!this.cachedNext) 242 { 243 this.cachedNext = true; 244 this.next = this.iterator.next(); 245 return this.next; 246 } 247 else 248 { 249 return this.next; 250 } 251 } 252 253 264 public boolean hasNext() 265 { 266 if (this.wantMore) 267 { 268 269 this.wantMore = false; 270 return hasMore(); 271 } 272 else 273 { 274 275 this.wantMore = true; 276 return false; 277 } 278 } 279 280 288 public void remove() throws UnsupportedOperationException 289 { 290 if (this.wrapped == null) 291 { 292 throw new IllegalStateException ("Use wrap() before calling remove()"); 293 } 294 295 296 this.iterator.remove(); 297 } 298 299 300 315 public Object more() 316 { 317 this.wantMore = true; 318 if (hasMore()) 319 { 320 Object next = next(); 321 this.cachedNext = false; 322 return next; 323 } 324 else 325 { 326 return null; 327 } 328 } 329 330 336 public boolean hasMore() 337 { 338 if (this.wrapped == null) 339 { 340 return false; 341 } 342 return cachedNext || this.iterator.hasNext(); 343 } 344 345 346 351 public void stop() 352 { 353 this.wantMore = false; 354 } 355 356 357 364 public String toString() 365 { 366 StringBuffer out = new StringBuffer (this.getClass().getName()); 367 if (this.wrapped != null) 368 { 369 out.append('('); 370 out.append(this.wrapped); 371 out.append(')'); 372 } 373 return out.toString(); 374 } 375 376 377 } 378 | Popular Tags |