1 package net.sf.jga.util; 33 34 import java.util.ListIterator ; 35 import java.util.NoSuchElementException ; 36 import java.util.Iterator ; 37 38 45 46 public class SingletonIterator<T> implements ListIterator <T>, Iterable <T> { 47 private T _value; 49 50 private boolean _beforeValue = true; 53 54 private boolean _canSetValue = false; 56 57 60 public SingletonIterator(T value) { 61 _value = value; 62 } 63 64 68 public Iterator <T> iterator() { return this; } 69 70 74 78 public boolean hasNext() { 79 return _beforeValue; 80 } 81 82 87 public T next() { 88 if (!_beforeValue) 89 throw new NoSuchElementException (); 90 91 _beforeValue = false; 92 _canSetValue = true; 93 return _value; 94 } 95 96 100 public boolean hasPrevious() { 101 return !_beforeValue; 102 } 103 104 109 public T previous() { 110 if (_beforeValue) 111 throw new NoSuchElementException (); 112 113 _beforeValue = true; 114 _canSetValue = true; 115 return _value; 116 } 117 118 public int nextIndex() { 119 return _beforeValue ? 0 : 1; 120 } 121 122 public int previousIndex() { 123 return _beforeValue ? -1 : 0; 124 } 125 126 public void add(T arg) 127 { 128 throw new UnsupportedOperationException (); 129 } 130 131 public void set(T value) { 132 if (!_canSetValue) 133 throw new IllegalStateException (); 134 135 _value = value; 136 _canSetValue = false; 137 } 138 139 public void remove() { 140 throw new UnsupportedOperationException (); 141 } 142 } 143 | Popular Tags |