1 16 17 package org.apache.commons.betwixt.expression; 18 19 import java.lang.reflect.Array ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Enumeration ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.NoSuchElementException ; 26 27 28 33 public class IteratorExpression implements Expression { 34 35 36 private Expression expression; 37 38 43 public IteratorExpression(Expression expression) { 44 this.expression = expression; 45 } 46 47 51 public Object evaluate(Context context) { 52 Object value = expression.evaluate( context ); 54 55 if ( value instanceof Iterator ) { 58 return (Iterator ) value; 60 61 } else if ( value instanceof Collection ) { 62 Collection collection = (Collection ) value; 64 return collection.iterator(); 65 66 } else if ( value instanceof Map ) { 67 Map map = (Map ) value; 69 return map.entrySet().iterator(); 70 71 } else if ( value instanceof Enumeration ) { 72 return new EnumerationIterator( (Enumeration ) value ); 74 75 } else if ( value != null ) { 76 Class type = value.getClass(); 78 if ( type.isArray() ) { 79 return new ArrayIterator( value ); 80 } 81 } 82 83 return Collections.EMPTY_LIST.iterator(); 86 } 87 88 92 public void update(Context context, String newValue) { 93 } 95 96 100 public String toString() { 101 return "IteratorExpression [expression=" + expression + "]"; 102 } 103 104 105 115 private static final class ArrayIterator implements Iterator { 116 117 118 protected Object array; 119 120 121 protected int startIndex = 0; 122 123 124 protected int endIndex = 0; 125 126 127 protected int index = 0; 128 129 138 public ArrayIterator() { 139 super(); 140 } 141 142 153 public ArrayIterator(final Object array) { 154 super(); 155 setArray(array); 156 } 157 158 173 public ArrayIterator(final Object array, final int startIndex) { 174 super(); 175 setArray(array); 176 checkBound(startIndex, "start"); 177 this.startIndex = startIndex; 178 this.index = startIndex; 179 } 180 181 198 public ArrayIterator(final Object array, final int startIndex, 199 final int endIndex) { 200 super(); 201 setArray(array); 202 checkBound(startIndex, "start"); 203 checkBound(endIndex, "end"); 204 if (endIndex < startIndex) { 205 throw new IllegalArgumentException ( 206 "End index must not be less than start index."); 207 } 208 this.startIndex = startIndex; 209 this.endIndex = endIndex; 210 this.index = startIndex; 211 } 212 213 223 protected void checkBound(final int bound, final String type) { 224 if (bound > this.endIndex) { 225 throw new ArrayIndexOutOfBoundsException ( 226 "Attempt to make an ArrayIterator that " + type 227 + "s beyond the end of the array. "); 228 } 229 if (bound < 0) { 230 throw new ArrayIndexOutOfBoundsException ( 231 "Attempt to make an ArrayIterator that " + type 232 + "s before the start of the array. "); 233 } 234 } 235 236 243 public boolean hasNext() { 244 return (index < endIndex); 245 } 246 247 255 public Object next() { 256 if (hasNext() == false) { 257 throw new NoSuchElementException (); 258 } 259 return Array.get(array, index++); 260 } 261 262 268 public void remove() { 269 throw new UnsupportedOperationException ( 270 "remove() method is not supported"); 271 } 272 273 283 public Object getArray() { 284 return array; 285 } 286 287 303 public void setArray(final Object array) { 304 this.endIndex = Array.getLength(array); 313 this.startIndex = 0; 314 this.array = array; 315 this.index = 0; 316 } 317 318 321 public void reset() { 322 this.index = this.startIndex; 323 } 324 325 } 326 327 328 336 private static final class EnumerationIterator implements Iterator { 337 338 339 private Collection collection; 340 341 342 private Enumeration enumeration; 343 344 345 private Object last; 346 347 353 public EnumerationIterator() { 354 this(null, null); 355 } 356 357 363 public EnumerationIterator(final Enumeration enumeration) { 364 this(enumeration, null); 365 } 366 367 374 public EnumerationIterator(final Enumeration enumeration, 375 final Collection collection) { 376 super(); 377 this.enumeration = enumeration; 378 this.collection = collection; 379 this.last = null; 380 } 381 382 390 public boolean hasNext() { 391 return enumeration.hasMoreElements(); 392 } 393 394 400 public Object next() { 401 last = enumeration.nextElement(); 402 return last; 403 } 404 405 415 public void remove() { 416 if (collection != null) { 417 if (last != null) { 418 collection.remove(last); 419 } else { 420 throw new IllegalStateException ( 421 "next() must have been called for remove() to function"); 422 } 423 } else { 424 throw new UnsupportedOperationException ( 425 "No Collection associated with this Iterator"); 426 } 427 } 428 429 436 public Enumeration getEnumeration() { 437 return enumeration; 438 } 439 440 445 public void setEnumeration(final Enumeration enumeration) { 446 this.enumeration = enumeration; 447 } 448 } 449 450 } 451 | Popular Tags |