KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > iterator > ArrayIterator


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

18 package org.apache.beehive.netui.util.iterator;
19
20 import java.lang.reflect.Array JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import org.apache.beehive.netui.util.Bundle;
25 import org.apache.beehive.netui.util.logging.Logger;
26
27 /**
28  * <code>ArrayIterator</code> provides an <code>Iterator</code> over a Java
29  * array. <code>ArrayIterator</code> will return each element in the array
30  * in the order they are stored in the array. Multidimensional arrays are
31  * handled by returning a sequence of sub-arrays. So a three dimensional array
32  * of integers will return a sequence of two dimensional arrays of integers.
33  * This <code>Iterator</code> does not support the <code>remove()</code>
34  * method.
35  */

36 public class ArrayIterator
37     implements Iterator JavaDoc {
38
39     /**
40      * The array object supplied to the iterator
41      */

42     private Object JavaDoc _array;
43
44     /**
45      * The total number of elements in the array
46      */

47     private int _totalElements;
48
49     /**
50      * The current element being accessed
51      */

52     private int _currentElement;
53
54     public ArrayIterator(Object JavaDoc array) {
55         if(array == null)
56             return;
57
58         if(!array.getClass().isArray())
59             throw new IllegalStateException JavaDoc(Bundle.getErrorString("ArrayIterator_notAnArray"));
60
61         _array = array;
62         _totalElements = Array.getLength(_array);
63     }
64
65     public boolean hasNext() {
66         if(_array == null)
67             return false;
68         else {
69             if(_currentElement < _totalElements)
70                 return true;
71             else return false;
72         }
73     }
74
75     public Object JavaDoc next() {
76         if(_currentElement >= _totalElements)
77             throw new NoSuchElementException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
78
79         Object JavaDoc nextElement = null;
80         try {
81             nextElement = Array.get(_array, _currentElement);
82         } catch(Exception JavaDoc e) {
83             throw new NoSuchElementException JavaDoc(Bundle.getErrorString("ArrayIterator_arrayError", new Object JavaDoc[]{_array.getClass().getName()}));
84         }
85
86         _currentElement++;
87
88         return nextElement;
89     }
90
91     public void remove() {
92         throw new UnsupportedOperationException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported", new Object JavaDoc[]{this.getClass().getName()}));
93     }
94 }
95
Popular Tags