1 package org.apache.velocity.util; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.Iterator; 23 import java.util.NoSuchElementException; 24 import java.lang.reflect.Array; 25 26 27 /** 28 * <p> 29 * An Iterator wrapper for an Object[]. This will 30 * allow us to deal with all array like structures 31 * in a consistent manner. 32 * </p> 33 * <p> 34 * WARNING : this class's operations are NOT synchronized. 35 * It is meant to be used in a single thread, newly created 36 * for each use in the #foreach() directive. 37 * If this is used or shared, synchronize in the 38 * next() method. 39 * </p> 40 * 41 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 42 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a> 43 * @version $Id: ArrayIterator.java 463298 2006-10-12 16:10:32Z henning $ 44 */ 45 public class ArrayIterator implements Iterator 46 { 47 /** 48 * The objects to iterate. 49 */ 50 private Object array; 51 52 /** 53 * The current position and size in the array. 54 */ 55 private int pos; 56 private int size; 57 58 /** 59 * Creates a new iterator instance for the specified array. 60 * 61 * @param array The array for which an iterator is desired. 62 */ 63 public ArrayIterator(Object array) 64 { 65 /* 66 * if this isn't an array, then throw. Note that this is 67 * for internal use - so this should never happen - if it does 68 * we screwed up. 69 */ 70 71 if ( !array.getClass().isArray() ) 72 { 73 throw new IllegalArgumentException( 74 "Programmer error : internal ArrayIterator invoked w/o array"); 75 } 76 77 this.array = array; 78 pos = 0; 79 size = Array.getLength( this.array ); 80 } 81 82 /** 83 * Move to next element in the array. 84 * 85 * @return The next object in the array. 86 */ 87 public Object next() 88 { 89 if (pos < size ) 90 return Array.get( array, pos++); 91 92 /* 93 * we screwed up... 94 */ 95 96 throw new NoSuchElementException("No more elements: " + pos + 97 " / " + size); 98 } 99 100 /** 101 * Check to see if there is another element in the array. 102 * 103 * @return Whether there is another element. 104 */ 105 public boolean hasNext() 106 { 107 return (pos < size ); 108 } 109 110 /** 111 * No op--merely added to satify the <code>Iterator</code> interface. 112 */ 113 public void remove() 114 { 115 throw new UnsupportedOperationException(); 116 } 117 }