1 29 package net.metanotion.util.skiplist; 30 31 import java.util.ListIterator ; 32 import java.util.NoSuchElementException ; 33 34 39 public class SkipIterator implements ListIterator { 40 SkipSpan ss; 41 int index; 42 43 protected SkipIterator() { } 44 public SkipIterator(SkipSpan ss, int index) { 45 if(ss==null) { throw new NullPointerException (); } 46 this.ss = ss; 47 this.index = index; 48 } 49 50 public boolean hasNext() { 51 if(index < ss.nKeys) { return true; } 52 return false; 53 } 54 55 public Object next() { 56 Object o; 57 if(index < ss.nKeys) { 58 o = ss.vals[index]; 59 } else { 60 throw new NoSuchElementException (); 61 } 62 63 if(index < (ss.nKeys-1)) { 64 index++; 65 } else if(ss.next != null) { 66 ss = ss.next; 67 index = 0; 68 } else { 69 index = ss.nKeys; 70 } 71 return o; 72 } 73 74 public Comparable nextKey() { 75 Comparable c; 76 if(index < ss.nKeys) { return ss.keys[index]; } 77 throw new NoSuchElementException (); 78 } 79 80 public boolean hasPrevious() { 81 if(index > 0) { return true; } 82 if((ss.prev != null) && (ss.prev.nKeys > 0)) { return true; } 83 return false; 84 } 85 86 public Object previous() { 87 if(index > 0) { 88 index--; 89 } else if(ss.prev != null) { 90 ss = ss.prev; 91 if(ss.nKeys <= 0) { throw new NoSuchElementException (); } 92 index = (ss.nKeys - 1); 93 } 94 return ss.vals[index]; 95 } 96 97 98 public void add(Object o) { throw new UnsupportedOperationException (); } 100 public void remove() { throw new UnsupportedOperationException (); } 101 public void set(Object o) { throw new UnsupportedOperationException (); } 102 public int nextIndex() { throw new UnsupportedOperationException (); } 103 public int previousIndex() { throw new UnsupportedOperationException (); } 104 105 } 106 | Popular Tags |