1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.Iterator ; 19 import java.util.NoSuchElementException ; 20 21 import org.apache.commons.collections.ResettableIterator; 22 23 34 public class SingletonIterator 35 implements Iterator , ResettableIterator { 36 37 38 private final boolean removeAllowed; 39 40 private boolean beforeFirst = true; 41 42 private boolean removed = false; 43 44 private Object object; 45 46 52 public SingletonIterator(Object object) { 53 this(object, true); 54 } 55 56 64 public SingletonIterator(Object object, boolean removeAllowed) { 65 super(); 66 this.object = object; 67 this.removeAllowed = removeAllowed; 68 } 69 70 78 public boolean hasNext() { 79 return (beforeFirst && !removed); 80 } 81 82 91 public Object next() { 92 if (!beforeFirst || removed) { 93 throw new NoSuchElementException (); 94 } 95 beforeFirst = false; 96 return object; 97 } 98 99 108 public void remove() { 109 if (removeAllowed) { 110 if (removed || beforeFirst) { 111 throw new IllegalStateException (); 112 } else { 113 object = null; 114 removed = true; 115 } 116 } else { 117 throw new UnsupportedOperationException (); 118 } 119 } 120 121 124 public void reset() { 125 beforeFirst = true; 126 } 127 128 } 129 | Popular Tags |