1 package org.tigris.scarab.util; 2 3 48 49 import java.util.Collections ; 50 import java.util.Iterator ; 51 import java.util.NoSuchElementException ; 52 53 public class WindowIterator 54 implements Iterator 55 { 56 public static final WindowIterator EMPTY = new EmptyWindowIterator(); 57 private static final Object NOT_INITIALIZED = new Object (); 58 59 private final Iterator i; 60 private final Object [] window; 61 private final int bsize; 62 private final int fsize; 63 private final int size; 64 private Boolean hasNext; 65 66 69 public WindowIterator(Iterator i, int backSize, int forwardSize) 70 { 71 this.i = i; 72 this.fsize = Math.abs(forwardSize); 73 this.bsize = Math.abs(backSize); 74 size = fsize + bsize + 1; 75 window = new Object [size]; 76 77 for (int m = 0; m < size; m++) 78 { 79 window[m] = NOT_INITIALIZED; 80 } 81 } 82 83 public boolean hasNext() 84 { 85 if (hasNext == null) 86 { 87 hasNext = (internalIterate()) 88 ? Boolean.TRUE : Boolean.FALSE; 89 } 90 return hasNext.booleanValue(); 91 } 92 93 boolean firstCall = true; 94 137 138 private boolean internalIterate() 139 { 140 if (firstCall) 141 { 142 for (int m = 0; m <= fsize && i.hasNext(); m++) 143 { 144 window[bsize + m] = i.next(); 145 } 146 firstCall = false; 147 } 148 else 149 { 150 for (int i = 1; i < size; i++) 151 { 152 window[i-1] = window[i]; 153 } 154 155 if (i.hasNext()) 156 { 157 window[size-1] = i.next(); 159 } 160 else 161 { 162 window[size-1] = NOT_INITIALIZED; 164 } 165 } 166 167 return window[bsize] != NOT_INITIALIZED; 168 } 169 170 public Object next() 171 { 172 if (hasNext()) 173 { 174 hasNext = null; 175 return window[bsize]; 176 } 177 else 178 { 179 throw new NoSuchElementException ("Iterator is exhausted"); } 181 } 182 183 184 public void remove() 185 { 186 throw new UnsupportedOperationException ("'remove' is not implemented"); } 188 189 193 public Object get(int i) 194 { 195 if (i < 0 && (-1 * i) > bsize) 196 { 197 throw new ArrayIndexOutOfBoundsException ("window was only defined " 198 + bsize + " in the negative direction. Argument was " + i); } 200 201 if (i > 0 && i > fsize) 202 { 203 throw new ArrayIndexOutOfBoundsException ("window was only defined " 204 + fsize + " in the positive direction. Argument was " + i); } 206 207 return window[bsize + i]; 208 } 209 210 public boolean hasValue(int i) 211 { 212 return get(i) != NOT_INITIALIZED; 213 } 214 } 215 216 class EmptyWindowIterator extends WindowIterator 217 { 218 EmptyWindowIterator() 219 { 220 super(Collections.EMPTY_SET.iterator(), 0, 0); 221 } 222 223 public boolean hasNext() 224 { 225 return false; 226 } 227 228 public Object next() 229 { 230 throw new NoSuchElementException ("This is an empty list."); } 232 233 public void remove() 234 { 235 throw new IllegalStateException ("next() will throw exception, it is " 236 + "not possible to call this method."); } 238 239 public Object get(int i) 240 { 241 throw new NoSuchElementException ("This is an empty list."); } 243 244 public boolean hasValue(int i) 245 { 246 return false; 247 } 248 } 249 | Popular Tags |