1 47 48 package org.ozoneDB.collections; 49 50 import java.util.ConcurrentModificationException ; 51 import java.util.NoSuchElementException ; 52 import org.ozoneDB.OzoneObject; 53 54 58 public class _BaseList_ListIteratorImpl extends OzoneObject implements OzoneListIterator { 59 60 private BaseList owner; 61 private int knownMod; 62 private int position; 63 private int lastReturned = -1; 64 private int size; 65 66 67 public _BaseList_ListIteratorImpl(BaseList owner, int index) { 68 this.owner = owner; 69 knownMod = owner._org_ozoneDB_getModCount(); 70 position = index; 71 size = owner.size(); 72 } 73 74 private void checkMod() { 76 if (knownMod != owner._org_ozoneDB_getModCount()) { 77 throw new ConcurrentModificationException (); 78 } 79 } 80 81 public boolean hasNext() { 82 checkMod(); 83 return position < size; 84 } 85 86 public boolean hasPrevious() { 87 checkMod(); 88 return position > 0; 89 } 90 91 public Object next() { 92 checkMod(); 93 if (position == size) { 94 throw new NoSuchElementException (); 95 } 96 lastReturned = position; 97 return owner.get(position++); 98 } 99 100 public Object previous() { 101 checkMod(); 102 if (position == 0) { 103 throw new NoSuchElementException (); 104 } 105 lastReturned = --position; 106 return owner.get(lastReturned); 107 } 108 109 public int nextIndex() { 110 checkMod(); 111 return position; 112 } 113 114 public int previousIndex() { 115 checkMod(); 116 return position - 1; 117 } 118 119 public void remove() { 120 checkMod(); 121 if (lastReturned < 0) 122 throw new IllegalStateException (); 123 owner.remove(lastReturned); 124 size--; 125 position = lastReturned; 126 lastReturned = -1; 127 knownMod = owner._org_ozoneDB_getModCount(); 128 } 129 130 public void set(Object o) { 131 checkMod(); 132 if (lastReturned < 0) { 133 throw new IllegalStateException (); 134 } 135 owner.set(lastReturned, o); 136 } 137 138 public void add(Object o) { 139 checkMod(); 140 owner.add(position++, o); 141 size++; 142 lastReturned = -1; 143 knownMod = owner._org_ozoneDB_getModCount(); 144 } 145 146 } | Popular Tags |