| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BaseIterator |
|
| 0.0;0 |
| 1 | package org.webslinger.collections; | |
| 2 | ||
| 3 | import java.util.Iterator; | |
| 4 | import java.util.NoSuchElementException; | |
| 5 | ||
| 6 | /** | |
| 7 | * <p>A class that makes writing {@link java.util.Iterator Iterators} | |
| 8 | * easier. | |
| 9 | * <p>This class handles the interation logic of {@link #hasNext() | |
| 10 | * hasNext()} and {@link #next() next()}, and calls single methods to | |
| 11 | * actually get at the data. | |
| 12 | * | |
| 13 | * @author <a href="mailto:doogie@brainfood.com">Adam Heath</a> | |
| 14 | */ | |
| 15 | 0 | public abstract class BaseIterator<T> implements Iterator<T> { |
| 16 | 0 | private boolean iterate = true; |
| 17 | 0 | private boolean beenRemoved = false; |
| 18 | 0 | private boolean hasNextObject = false; |
| 19 | private T item; | |
| 20 | ||
| 21 | /** | |
| 22 | * <p>Returns the next element in the interation. | |
| 23 | * | |
| 24 | * @return the next element in the iteration. | |
| 25 | * @throws <code>NoSuchElementException</code> - iteration has no | |
| 26 | * more elements. | |
| 27 | */ | |
| 28 | public T next() { | |
| 29 | 0 | if (!hasNext()) throw new NoSuchElementException("No such element"); |
| 30 | 0 | iterate = true; |
| 31 | 0 | return item; |
| 32 | } | |
| 33 | ||
| 34 | /** | |
| 35 | * <p>Returns true if the iteration has more elements. (In other | |
| 36 | * words, returns true if next would return an element rather than | |
| 37 | * throwing an exception.) | |
| 38 | * | |
| 39 | * @return <code>true</code> if the iterator has more elements. | |
| 40 | */ | |
| 41 | public boolean hasNext() { | |
| 42 | 0 | if (!iterate) return hasNextObject; |
| 43 | 0 | iterate = false; |
| 44 | 0 | beenRemoved = false; |
| 45 | 0 | hasNextObject = getNext(); |
| 46 | 0 | return hasNextObject; |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * <p>This is called to fetch next object. | |
| 51 | * | |
| 52 | * @return true if a new object was fetched. | |
| 53 | */ | |
| 54 | protected abstract boolean getNext(); | |
| 55 | ||
| 56 | /** | |
| 57 | * <p>A convenience method that allows sub-classes to see the current | |
| 58 | * item value. | |
| 59 | * | |
| 60 | * @return The current item. | |
| 61 | */ | |
| 62 | protected T getItem() { | |
| 63 | 0 | return item; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * <p>This is called from {@link #getNext() getNext()} to set the | |
| 68 | * object to be returned by {@link #next() next()}. | |
| 69 | * | |
| 70 | * @param item The object to be returned by {@link #next() next()}. | |
| 71 | */ | |
| 72 | protected void setItem(T item) { | |
| 73 | 0 | this.item = item; |
| 74 | 0 | } |
| 75 | ||
| 76 | /** | |
| 77 | * <p>This is called from {@link #remove() remove()} to remove the | |
| 78 | * current object from the {@link java.util.Collection Collection} | |
| 79 | * that this iterator is being backed by. | |
| 80 | */ | |
| 81 | protected abstract void removeItem(); | |
| 82 | ||
| 83 | /** | |
| 84 | * <p>Removes from the underlying collection the last element | |
| 85 | * returned by the iterator(optional operation). This method can be | |
| 86 | * called only once per call to next. The behavior of an iterator | |
| 87 | * is unspecified if the underlying collection is modified while the | |
| 88 | * iteration is in progress in any way other than by calling this | |
| 89 | * method. | |
| 90 | * | |
| 91 | * @throws <code>UnsupportedOperationException</code> - if the remove | |
| 92 | * operation is not supported by this Iterator. | |
| 93 | * @throws <code>IllegalStateException</code> - if the next method | |
| 94 | * has not yet been called, or the remove method has already been | |
| 95 | * called after the last call to the next method. | |
| 96 | */ | |
| 97 | public void remove() { | |
| 98 | 0 | if (beenRemoved) throw new IllegalStateException("item has already been removed"); |
| 99 | 0 | beenRemoved = true; |
| 100 | 0 | removeItem(); |
| 101 | 0 | } |
| 102 | } |