001: /*
002: * Converts an array to an iterator.
003: * Copyright (C) 2004-2006 Stephen Ostermiller
004: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * See COPYING.TXT for details.
017: */
018:
019: package com.Ostermiller.util;
020:
021: import java.util.*;
022:
023: /**
024: * Converts an array to an iterator.
025: * <p>
026: * More information about this class is available from <a target="_top" href=
027: * "http://ostermiller.org/utils/Iterator_Enumeration.html">ostermiller.org</a>.
028: *
029: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
030: * @param <ElementType> Type of array over which to iterate
031: * @since ostermillerutils 1.03.00
032: */
033: public class ArrayIterator<ElementType> implements
034: Iterator<ElementType> {
035:
036: /**
037: * Array being converted to iterator.
038: */
039: private ElementType[] array;
040:
041: /**
042: * Current index into the array.
043: */
044: private int index = 0;
045:
046: /**
047: * Whether the last element has been removed.
048: */
049: private boolean lastRemoved = false;
050:
051: /**
052: * Create an Iterator from an Array.
053: *
054: * @param array of objects on which to enumerate.
055: *
056: * @since ostermillerutils 1.03.00
057: */
058: public ArrayIterator(ElementType[] array) {
059: this .array = array;
060: }
061:
062: /**
063: * Tests if this Iterator contains more elements.
064: *
065: * @return true if and only if this Iterator object contains at least
066: * one more element to provide; false otherwise.
067: *
068: * @since ostermillerutils 1.03.00
069: */
070: public boolean hasNext() {
071: return (index < array.length);
072: }
073:
074: /**
075: * Returns the next element of this Iterator if this Iterator
076: * object has at least one more element to provide.
077: *
078: * @return the next element of this Iterator.
079: * @throws NoSuchElementException if no more elements exist.
080: *
081: * @since ostermillerutils 1.03.00
082: */
083: public ElementType next() throws NoSuchElementException {
084: if (index >= array.length)
085: throw new NoSuchElementException("Array index: " + index);
086: ElementType object = array[index];
087: index++;
088: lastRemoved = false;
089: return object;
090: }
091:
092: /**
093: * Removes the last object from the array by setting the slot in
094: * the array to null.
095: * This method can be called only once per call to next.
096: *
097: * @throws IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method.
098: *
099: * @since ostermillerutils 1.03.00
100: */
101: public void remove() {
102: if (index == 0)
103: throw new IllegalStateException();
104: if (lastRemoved)
105: throw new IllegalStateException();
106: array[index - 1] = null;
107: lastRemoved = true;
108: }
109: }
|