1 30 31 package rcm.util; 32 33 import java.util.Enumeration ; 34 35 public class History { 36 public static rcm.util.Debug debug = rcm.util.Debug.QUIET; 37 38 protected Object [] history; protected int start; protected int end; protected int curr; 46 50 public History (int max) { 51 history = new Object [max+1]; 52 start = end = curr = 0; 53 } 54 55 59 public History (History h) { 60 this.history = new Object [h.history.length]; 61 System.arraycopy (h.history, 0, this.history, 0, h.history.length); 62 this.start = h.start; 63 this.end = h.end; 64 this.curr = h.curr; 65 } 66 67 70 public void clear () { 71 for (int i = 0; i < history.length; ++i) 72 history[i] = null; 73 74 int s = start; 75 int e = end; 76 start = end = curr = 0; 77 78 if (s != e) 79 fireRemoved (s, (e > 0) ? e-1 : history.length-1); 80 } 81 82 85 public void expand () { 86 Object [] newHistory = new Object [(history.length * 2) - 1]; 87 int i = 0; 88 int newCurr = 0; 89 for (int j = start; j != end; j = (j+1) % history.length, ++i) { 90 newHistory[i] = history[j]; 91 if (j == curr) 92 newCurr = i; 93 } 94 history = newHistory; 95 start = 0; 96 end = i; 97 curr = newCurr; 98 } 99 100 106 public void put (Object obj) { 107 if (!isEmpty ()) { 108 int newEnd = (curr+1) % history.length; 110 if (newEnd != end) { 111 int e = end; 112 end = newEnd; 113 fireRemoved (newEnd, (e > 0) ? e-1 : history.length-1); 114 } 115 } 116 117 add (obj); 118 } 119 120 125 public void add (Object obj) { 126 if (isFull ()) { 127 start = (start+1) % history.length; 129 fireRemoved (start, start); 130 } 131 132 history[end] = obj; 134 curr = end; 135 end = (end+1) % history.length; 136 fireAdded (curr, curr); 137 138 debug.println ("after put: start=" + start + ", end=" + end + ", curr=" + curr); 139 } 140 141 145 public Object get () { 146 return !isEmpty () ? history[curr] : null; 147 } 148 149 155 public Object peekBack () { 156 if (curr == start) 157 return null; 158 else { 159 int bk = (curr > 0) ? curr-1 : history.length-1; 160 return history[bk]; 161 } 162 } 163 164 170 public Object peekForward () { 171 if (start == end) 172 return null; 173 174 int fw = (curr+1) % history.length; 175 if (fw == end) 176 return null; 177 else 178 return history[fw]; 179 } 180 181 188 public void replace (Object obj) { 189 if (isEmpty ()) 190 put (obj); 191 else { 192 history[curr] = obj; 193 fireChanged (curr, curr); 194 } 195 } 196 197 201 public Object back () { 202 if (curr == start) 203 return null; 204 else { 205 curr = (curr > 0) ? curr-1 : history.length-1; 206 fireChanged (curr, curr); 207 return history[curr]; 208 } 209 } 210 211 215 public Object forward () { 216 if (start == end) 217 return null; 218 219 int newCurr = (curr+1) % history.length; 220 if (newCurr == end) 221 return null; 222 else { 223 curr = newCurr; 224 fireChanged (curr, curr); 225 return history[curr]; 226 } 227 } 228 229 233 public Object toStart () { 234 if (curr != start) { 235 curr = start; 236 fireChanged (curr, curr); 237 } 238 return history[curr]; 239 } 240 241 245 public Object toEnd () { 246 if (start == end) 247 return null; 248 int newCurr = (end > 0) ? end-1 : history.length-1; 249 if (curr != newCurr) { 250 curr = newCurr; 251 fireChanged (curr, curr); 252 } 253 return history[curr]; 254 } 255 256 260 public boolean canBack () { 261 return (curr != start); 262 } 263 264 268 public boolean canForward () { 269 return ((curr+1) % history.length != end); 270 } 271 272 276 public boolean isEmpty () { 277 return start == end; 278 } 279 280 284 public boolean isFull () { 285 return start == (end+1) % history.length; 286 } 287 288 293 public boolean contains (Object obj) { 294 for (int i = start; i != end; i = (i+1) % history.length) 295 if (history[i].equals (obj)) 296 return true; 297 return false; 298 } 299 300 305 public Enumeration elements () { 306 return new HistoryEnumeration (start, end); 307 } 308 309 314 public Enumeration forwardElements () { 315 return 316 (curr == end) 317 ? new HistoryEnumeration (curr, end) 318 : new HistoryEnumeration ((curr + 1) % history.length, end); 319 } 320 321 326 public Enumeration backElements () { 327 return new HistoryEnumeration (start, curr); 328 } 329 330 class HistoryEnumeration implements Enumeration { 331 int p; 332 int e; 333 334 public HistoryEnumeration (int start, int end) { 335 p = start; 336 e = end; 337 } 338 339 public boolean hasMoreElements () { 340 return p != e; 341 } 342 343 public Object nextElement () { 344 Object obj = history[p]; 345 p = (p+1) % history.length; 346 return obj; 347 } 348 } 349 350 protected void fireRemoved (int i, int j) { 351 } 352 353 protected void fireAdded (int i, int j) { 354 } 355 356 protected void fireChanged (int i, int j) { 357 } 358 } 359 | Popular Tags |