001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.util.iterator;
020:
021: import java.lang.reflect.Array;
022: import java.util.Iterator;
023: import java.util.NoSuchElementException;
024:
025: import org.apache.beehive.netui.util.Bundle;
026: import org.apache.beehive.netui.util.logging.Logger;
027:
028: /**
029: * <code>ArrayIterator</code> provides an <code>Iterator</code> over a Java
030: * array. <code>ArrayIterator</code> will return each element in the array
031: * in the order they are stored in the array. Multidimensional arrays are
032: * handled by returning a sequence of sub-arrays. So a three dimensional array
033: * of integers will return a sequence of two dimensional arrays of integers.
034: * This <code>Iterator</code> does not support the <code>remove()</code>
035: * method.
036: */
037: public class ArrayIterator implements Iterator {
038:
039: /**
040: * The array object supplied to the iterator
041: */
042: private Object _array;
043:
044: /**
045: * The total number of elements in the array
046: */
047: private int _totalElements;
048:
049: /**
050: * The current element being accessed
051: */
052: private int _currentElement;
053:
054: public ArrayIterator(Object array) {
055: if (array == null)
056: return;
057:
058: if (!array.getClass().isArray())
059: throw new IllegalStateException(Bundle
060: .getErrorString("ArrayIterator_notAnArray"));
061:
062: _array = array;
063: _totalElements = Array.getLength(_array);
064: }
065:
066: public boolean hasNext() {
067: if (_array == null)
068: return false;
069: else {
070: if (_currentElement < _totalElements)
071: return true;
072: else
073: return false;
074: }
075: }
076:
077: public Object next() {
078: if (_currentElement >= _totalElements)
079: throw new NoSuchElementException(
080: Bundle
081: .getErrorString("IteratorFactory_Iterator_noSuchElement"));
082:
083: Object nextElement = null;
084: try {
085: nextElement = Array.get(_array, _currentElement);
086: } catch (Exception e) {
087: throw new NoSuchElementException(Bundle.getErrorString(
088: "ArrayIterator_arrayError", new Object[] { _array
089: .getClass().getName() }));
090: }
091:
092: _currentElement++;
093:
094: return nextElement;
095: }
096:
097: public void remove() {
098: throw new UnsupportedOperationException(Bundle.getErrorString(
099: "IteratorFactory_Iterator_removeUnsupported",
100: new Object[] { this.getClass().getName() }));
101: }
102: }
|