1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 import java.util.*; 21 22 37 public class IntrusiveList extends AbstractSequentialList implements List { 38 39 private Member head; 40 41 private int mySize; 42 private int modLevel; 43 44 45 public IntrusiveList() { 46 head = new Member(); 47 head.previous = head.next = head; 48 modLevel = 0; 49 } 50 51 52 public void addFirst(Member m) { 53 addAfter(m, head); 54 } 55 56 57 public void addLast(Member m) { 58 addAfter(m, head.previous); 59 } 60 61 62 public void remove(Member m) { 63 if (checkOwner(m)) { 64 m.previous.next = m.next; 65 m.next.previous = m.previous; 66 m.previous = m.next = null; 67 m.owner = null; 68 mySize--; 69 modLevel++; 70 } 71 } 72 73 74 public Member removeFirst() { 75 Member m = head.next; 76 if (m == head) 77 return null; 78 remove(m); 79 return m; 80 } 81 82 83 public Member removeLast() { 84 Member m = head.previous; 85 if (m == head) 86 return null; 87 remove(m); 88 return m; 89 } 90 91 92 public int size() { 93 return mySize; 94 } 95 96 97 public ListIterator listIterator(int index) { 98 return new ListItr(modLevel, index); 99 } 100 101 102 private void addAfter(Member toAdd, Member afterMe) { 103 if (checkUnowned(toAdd)) { 104 toAdd.next = afterMe.next; 105 toAdd.previous = afterMe; 106 toAdd.next.previous = toAdd; 107 toAdd.previous.next = toAdd; 108 toAdd.owner = this; 109 mySize++; 110 modLevel++; 111 } 112 } 113 114 115 private boolean checkOwner(Member m) { 116 return m.owner == this; 117 } 118 119 private boolean checkUnowned(Member m) { 120 if (m.owner != null) { 121 if (m.owner == this) return false; 122 throw new IllegalArgumentException ( 123 "The object is already a member of some list!"); 124 } 125 return true; 126 } 127 128 132 class ListItr implements ListIterator { 133 134 private Member nextMember; 135 136 137 private Member lastReturned; 138 139 140 private int nextIndex; 141 142 145 private int itrModLevel; 146 147 150 boolean forward; 151 152 ListItr(int level, int index) { 153 if (index < 0 || index > mySize) { 154 throw new IndexOutOfBoundsException (); 155 } 156 Member start = head; 157 for (int i = 0; i < index; i++) { 158 start = start.next; 159 } 160 nextMember = start.next; 161 lastReturned = null; 162 nextIndex = index; 163 itrModLevel = level; 164 } 165 166 168 public boolean hasNext() { 169 checkLevel(); 170 return nextMember != head; 171 } 172 173 175 public boolean hasPrevious() { 176 checkLevel(); 177 return nextMember.previous != head; 178 } 179 180 181 public int nextIndex() { 182 checkLevel(); 183 return nextIndex; 184 } 185 186 187 public int previousIndex() { 188 checkLevel(); 189 return nextIndex - 1; 190 } 191 192 193 public Object next() { 194 checkLevel(); 195 if (nextMember == head) { 196 throw new NoSuchElementException(); 197 } 198 lastReturned = nextMember; 199 nextMember = nextMember.next; 200 nextIndex++; 201 forward = true; 202 return lastReturned; 203 } 204 205 206 public Object previous() { 207 checkLevel(); 208 if (nextMember.previous == head) { 209 throw new NoSuchElementException(); 210 } 211 lastReturned = nextMember.previous; 212 nextMember = lastReturned; 213 nextIndex--; 214 forward = false; 215 return lastReturned; 216 } 217 218 219 public void remove() { 220 checkLevel(); 221 if (lastReturned == null) { 222 throw new IllegalStateException (); 223 } 224 nextMember = lastReturned.next; 225 IntrusiveList.this.remove(lastReturned); 226 lastReturned = null; 227 if (forward) { 228 nextIndex--; 229 } 230 itrModLevel = modLevel; 231 } 232 233 234 public void set(Object o) { 235 checkLevel(); 236 if (lastReturned == null) { 237 throw new IllegalStateException (); 238 } 239 Member m = (Member)o; 240 addAfter(m, lastReturned); 241 IntrusiveList.this.remove(lastReturned); 242 lastReturned = m; 243 nextMember = forward ? m.next : m; 244 itrModLevel = modLevel; 245 } 246 247 249 public void add(Object o) { 250 checkLevel(); 251 Member m = (Member)o; 252 addAfter(m, nextMember.previous); 253 nextIndex++; 254 lastReturned = null; 255 itrModLevel = modLevel; 256 } 257 258 259 private void checkLevel() { 260 if (itrModLevel != modLevel) { 261 throw new ConcurrentModificationException(); 262 } 263 } 264 } 265 266 267 public static class Member { 268 IntrusiveList owner; 269 Member previous; 270 Member next; 271 272 } 273 } 274 | Popular Tags |