1 16 package org.apache.commons.collections.iterators; 17 18 import java.util.ListIterator ; 19 import java.util.NoSuchElementException ; 20 21 import org.apache.commons.collections.ResettableListIterator; 22 23 33 public class SingletonListIterator implements ListIterator , ResettableListIterator { 34 35 private boolean beforeFirst = true; 36 private boolean nextCalled = false; 37 private boolean removed = false; 38 private Object object; 39 40 45 public SingletonListIterator(Object object) { 46 super(); 47 this.object = object; 48 } 49 50 57 public boolean hasNext() { 58 return beforeFirst && !removed; 59 } 60 61 68 public boolean hasPrevious() { 69 return !beforeFirst && !removed; 70 } 71 72 78 public int nextIndex() { 79 return (beforeFirst ? 0 : 1); 80 } 81 82 89 public int previousIndex() { 90 return (beforeFirst ? -1 : 0); 91 } 92 93 102 public Object next() { 103 if (!beforeFirst || removed) { 104 throw new NoSuchElementException (); 105 } 106 beforeFirst = false; 107 nextCalled = true; 108 return object; 109 } 110 111 120 public Object previous() { 121 if (beforeFirst || removed) { 122 throw new NoSuchElementException (); 123 } 124 beforeFirst = true; 125 return object; 126 } 127 128 135 public void remove() { 136 if(!nextCalled || removed) { 137 throw new IllegalStateException (); 138 } else { 139 object = null; 140 removed = true; 141 } 142 } 143 144 149 public void add(Object obj) { 150 throw new UnsupportedOperationException ("add() is not supported by this iterator"); 151 } 152 153 160 public void set(Object obj) { 161 if (!nextCalled || removed) { 162 throw new IllegalStateException (); 163 } 164 this.object = obj; 165 } 166 167 170 public void reset() { 171 beforeFirst = true; 172 nextCalled = false; 173 } 174 175 } 176 | Popular Tags |