1 18 package org.apache.beehive.netui.util.iterator; 19 20 import java.lang.reflect.Array ; 21 import java.util.Iterator ; 22 import java.util.NoSuchElementException ; 23 24 import org.apache.beehive.netui.util.Bundle; 25 import org.apache.beehive.netui.util.logging.Logger; 26 27 36 public class ArrayIterator 37 implements Iterator { 38 39 42 private Object _array; 43 44 47 private int _totalElements; 48 49 52 private int _currentElement; 53 54 public ArrayIterator(Object array) { 55 if(array == null) 56 return; 57 58 if(!array.getClass().isArray()) 59 throw new IllegalStateException (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 next() { 76 if(_currentElement >= _totalElements) 77 throw new NoSuchElementException (Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement")); 78 79 Object nextElement = null; 80 try { 81 nextElement = Array.get(_array, _currentElement); 82 } catch(Exception e) { 83 throw new NoSuchElementException (Bundle.getErrorString("ArrayIterator_arrayError", new Object []{_array.getClass().getName()})); 84 } 85 86 _currentElement++; 87 88 return nextElement; 89 } 90 91 public void remove() { 92 throw new UnsupportedOperationException (Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported", new Object []{this.getClass().getName()})); 93 } 94 } 95 | Popular Tags |