KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > util > ArrayIterator


1 package org.apache.velocity.util;
2
3 /*
4  * Copyright 1999-2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Iterator JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21 import java.lang.reflect.Array JavaDoc;
22
23
24 /**
25  * <p>
26  * An Iterator wrapper for an Object[]. This will
27  * allow us to deal with all array like structures
28  * in a consistent manner.
29  * </p>
30  * <p>
31  * WARNING : this class's operations are NOT synchronized.
32  * It is meant to be used in a single thread, newly created
33  * for each use in the #foreach() directive.
34  * If this is used or shared, synchronize in the
35  * next() method.
36  * </p>
37  *
38  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
39  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
40  * @version $Id: ArrayIterator.java,v 1.6.8.1 2004/03/03 23:23:07 geirm Exp $
41  */

42 public class ArrayIterator implements Iterator JavaDoc
43 {
44     /**
45      * The objects to iterate.
46      */

47     private Object JavaDoc array;
48
49     /**
50      * The current position and size in the array.
51      */

52     private int pos;
53     private int size;
54
55     /**
56      * Creates a new iterator instance for the specified array.
57      *
58      * @param array The array for which an iterator is desired.
59      */

60     public ArrayIterator(Object JavaDoc array)
61     {
62         /*
63          * if this isn't an array, then throw. Note that this is
64          * for internal use - so this should never happen - if it does
65          * we screwed up.
66          */

67          
68         if ( !array.getClass().isArray() )
69         {
70             throw new IllegalArgumentException JavaDoc(
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     /**
80      * Move to next element in the array.
81      *
82      * @return The next object in the array.
83      */

84     public Object JavaDoc next()
85     {
86         if (pos < size )
87             return Array.get( array, pos++);
88                 
89         /*
90          * we screwed up...
91          */

92          
93         throw new NoSuchElementException JavaDoc("No more elements: " + pos +
94                                          " / " + size);
95     }
96     
97     /**
98      * Check to see if there is another element in the array.
99      *
100      * @return Whether there is another element.
101      */

102     public boolean hasNext()
103     {
104         return (pos < size );
105     }
106
107     /**
108      * No op--merely added to satify the <code>Iterator</code> interface.
109      */

110     public void remove()
111     {
112         throw new UnsupportedOperationException JavaDoc();
113     }
114 }
115
Popular Tags