KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > helpers > ArrayIterator


1 /**
2  * $Id: ArrayIterator.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2000-2003 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your option) any later
9  * version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.helpers;
30
31 import java.lang.reflect.Array JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.NoSuchElementException JavaDoc;
35
36 /**
37  * Iterator and Enumeration implementation for a Java array.
38  *
39  * @since JWare/core 0.5
40  * @author ssmc, &copy;2000-2003 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
41  * @version 0.5
42  * @.safety single
43  * @.group impl,helper
44  * @.pattern GoF.Adapter
45  **/

46
47 public class ArrayIterator extends NoRemoveIteratorSkeleton implements Iterator JavaDoc, Enumeration JavaDoc
48 {
49     private final Object JavaDoc m_array;
50     private final int m_len;
51     private int m_i;
52
53     /**
54      * Creates new iterator for given array.
55      * @param array array to iterate (non-null array)
56      **/

57     public ArrayIterator(Object JavaDoc array)
58     {
59         if (array==null || !array.getClass().isArray()) {
60             throw new IllegalArgumentException JavaDoc("ctor- nonzro ary reqd");
61         }
62         m_array = array;
63         m_len = Array.getLength(array);
64     }
65
66
67     /**
68      * Returns <i>true</i> if more elements in array to iterate.
69      **/

70     public boolean hasNext()
71     {
72         return m_i < m_len;
73     }
74
75
76     /**
77      * Returns <i>true</i> if more elements in array to iterate.
78      **/

79     public final boolean hasMoreElements()
80     {
81         return hasNext();
82     }
83
84
85     /**
86      * Returns next object in array and advances iteration by one.
87      * @throws NoSuchElementException if iteration finished
88      **/

89     public Object JavaDoc next()
90     {
91         if (m_i>=m_len) {
92             throw new NoSuchElementException JavaDoc();
93         }
94         return Array.get(m_array,m_i++);
95     }
96
97
98     /**
99      * Same as {@linkplain #next}.
100      **/

101     public final Object JavaDoc nextElement()
102     {
103         return next();
104     }
105
106
107     /**
108      * Returns index of last element returned returned by either
109      * {@linkplain #next} or {@linkplain #nextElement}.
110      **/

111     public int nextIndex()
112     {
113         return (m_i>=m_len) ? m_len : m_i;
114     }
115
116
117     /**
118      * Unsupported removal operation (arrays are immutable).
119      * @throws java.lang.UnsupportedOperationException always
120      **/

121     public void remove()
122     {
123         throw new UnsupportedOperationException JavaDoc("Cannot remove slot from Java array");
124     }
125 }
126
127 /* end-of-ArrayIterator.java */
128
Popular Tags