1 package org.apache.velocity.util; 2 3 18 19 import java.util.Iterator ; 20 import java.util.NoSuchElementException ; 21 import java.lang.reflect.Array ; 22 23 24 42 public class ArrayIterator implements Iterator 43 { 44 47 private Object array; 48 49 52 private int pos; 53 private int size; 54 55 60 public ArrayIterator(Object array) 61 { 62 67 68 if ( !array.getClass().isArray() ) 69 { 70 throw new IllegalArgumentException ( 71 "Programmer error : internal ArrayIterator invoked w/o array"); 72 } 73 74 this.array = array; 75 pos = 0; 76 size = Array.getLength( this.array ); 77 } 78 79 84 public Object next() 85 { 86 if (pos < size ) 87 return Array.get( array, pos++); 88 89 92 93 throw new NoSuchElementException ("No more elements: " + pos + 94 " / " + size); 95 } 96 97 102 public boolean hasNext() 103 { 104 return (pos < size ); 105 } 106 107 110 public void remove() 111 { 112 throw new UnsupportedOperationException (); 113 } 114 } 115 | Popular Tags |