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.Predicate; 22 23 36 public class FilterListIterator implements ListIterator { 37 38 39 private ListIterator iterator; 40 41 42 private Predicate predicate; 43 44 48 private Object nextObject; 49 50 54 private boolean nextObjectSet = false; 55 56 60 private Object previousObject; 61 62 66 private boolean previousObjectSet = false; 67 68 71 private int nextIndex = 0; 72 73 80 public FilterListIterator() { 81 super(); 82 } 83 84 90 public FilterListIterator(ListIterator iterator ) { 91 super(); 92 this.iterator = iterator; 93 } 94 95 101 public FilterListIterator(ListIterator iterator, Predicate predicate) { 102 super(); 103 this.iterator = iterator; 104 this.predicate = predicate; 105 } 106 107 115 public FilterListIterator(Predicate predicate) { 116 super(); 117 this.predicate = predicate; 118 } 119 120 122 public void add(Object o) { 123 throw new UnsupportedOperationException ("FilterListIterator.add(Object) is not supported."); 124 } 125 126 public boolean hasNext() { 127 if(nextObjectSet) { 128 return true; 129 } else { 130 return setNextObject(); 131 } 132 } 133 134 public boolean hasPrevious() { 135 if(previousObjectSet) { 136 return true; 137 } else { 138 return setPreviousObject(); 139 } 140 } 141 142 public Object next() { 143 if(!nextObjectSet) { 144 if(!setNextObject()) { 145 throw new NoSuchElementException (); 146 } 147 } 148 nextIndex++; 149 Object temp = nextObject; 150 clearNextObject(); 151 return temp; 152 } 153 154 public int nextIndex() { 155 return nextIndex; 156 } 157 158 public Object previous() { 159 if(!previousObjectSet) { 160 if(!setPreviousObject()) { 161 throw new NoSuchElementException (); 162 } 163 } 164 nextIndex--; 165 Object temp = previousObject; 166 clearPreviousObject(); 167 return temp; 168 } 169 170 public int previousIndex() { 171 return (nextIndex-1); 172 } 173 174 175 public void remove() { 176 throw new UnsupportedOperationException ("FilterListIterator.remove() is not supported."); 177 } 178 179 180 public void set(Object o) { 181 throw new UnsupportedOperationException ("FilterListIterator.set(Object) is not supported."); 182 } 183 184 190 public ListIterator getListIterator() { 191 return iterator; 192 } 193 194 200 public void setListIterator(ListIterator iterator) { 201 this.iterator = iterator; 202 } 203 204 210 public Predicate getPredicate() { 211 return predicate; 212 } 213 214 219 public void setPredicate(Predicate predicate) { 220 this.predicate = predicate; 221 } 222 223 private void clearNextObject() { 225 nextObject = null; 226 nextObjectSet = false; 227 } 228 229 private boolean setNextObject() { 230 if(previousObjectSet) { 235 clearPreviousObject(); 236 if(!setNextObject()) { 237 return false; 238 } else { 239 clearNextObject(); 240 } 241 } 242 243 while(iterator.hasNext()) { 244 Object object = iterator.next(); 245 if(predicate.evaluate(object)) { 246 nextObject = object; 247 nextObjectSet = true; 248 return true; 249 } 250 } 251 return false; 252 } 253 254 private void clearPreviousObject() { 255 previousObject = null; 256 previousObjectSet = false; 257 } 258 259 private boolean setPreviousObject() { 260 if(nextObjectSet) { 265 clearNextObject(); 266 if(!setPreviousObject()) { 267 return false; 268 } else { 269 clearPreviousObject(); 270 } 271 } 272 273 while(iterator.hasPrevious()) { 274 Object object = iterator.previous(); 275 if(predicate.evaluate(object)) { 276 previousObject = object; 277 previousObjectSet = true; 278 return true; 279 } 280 } 281 return false; 282 } 283 284 } 285 | Popular Tags |