1 21 package oracle.toplink.essentials.internal.helper.linkedlist; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.ValidationException; 26 27 40 public class ExposedNodeLinkedList implements List { 41 private transient LinkedNode header; 42 private transient int size; 43 44 47 public ExposedNodeLinkedList() { 48 this.size = 0; 49 this.header = new LinkedNode(null, null, null); 50 header.next = header; 51 header.previous = header; 52 } 53 54 public Object [] toArray(Object [] array) { 56 throw ValidationException.operationNotSupported("toArray"); 57 } 58 59 public Object [] toArray() { 60 throw ValidationException.operationNotSupported("toArray"); 61 } 62 63 public Object set(int index, Object value) { 64 throw ValidationException.operationNotSupported("set"); 65 } 66 67 public ListIterator listIterator(int index) { 68 throw ValidationException.operationNotSupported("listIterator"); 69 } 70 71 public ListIterator listIterator() { 72 throw ValidationException.operationNotSupported("listIterator"); 73 } 74 75 public Iterator iterator() { 76 throw ValidationException.operationNotSupported("iterator"); 77 } 78 79 public List subList(int start, int end) { 80 throw ValidationException.operationNotSupported("subList"); 81 } 82 83 public boolean retainAll(Collection collection) { 84 throw ValidationException.operationNotSupported("retainAll"); 85 } 86 87 public boolean removeAll(Collection collection) { 88 throw ValidationException.operationNotSupported("removeAll"); 89 } 90 91 public boolean containsAll(Collection collection) { 92 throw ValidationException.operationNotSupported("containsAll"); 93 } 94 95 public boolean addAll(Collection collection) { 96 throw ValidationException.operationNotSupported("addAll"); 97 } 98 99 public boolean addAll(int index, Collection collection) { 100 throw ValidationException.operationNotSupported("addAll"); 101 } 102 103 public boolean remove(Object object) { 104 throw ValidationException.operationNotSupported("remove"); 105 } 106 107 public boolean add(Object object) { 108 addLast(object); 109 return true; 110 } 111 112 public int lastIndexOf(Object object) { 113 throw ValidationException.operationNotSupported("lastIndexOf"); 114 } 115 116 public void add(int index, Object object) { 117 throw ValidationException.operationNotSupported("add"); 118 } 119 120 public Object remove(int index) { 121 throw ValidationException.operationNotSupported("remove"); 122 } 123 124 public Object get(int index) { 125 throw ValidationException.operationNotSupported("get"); 126 } 127 128 public boolean isEmpty() { 129 return size() == 0; 130 } 131 132 137 public Object getFirst() { 138 if (size == 0) { 139 return null; 140 } 141 return header.next.contents; 142 } 143 144 149 public Object getLast() { 150 if (size == 0) { 151 return null; 152 } 153 return header.previous.contents; 154 } 155 156 162 public Object removeFirst() { 163 if (size != 0) { 164 Object first = header.next.contents; 165 remove(header.next); 166 return first; 167 } 168 return null; 169 } 170 171 177 public Object removeLast() { 178 if (size != 0) { 179 Object last = header.previous.contents; 180 remove(header.previous); 181 return last; 182 } 183 return null; 184 } 185 186 191 public LinkedNode addFirst(Object o) { 192 return addAfter(o, header); 193 } 194 195 201 public LinkedNode addLast(Object o) { 202 return addAfter(o, header.previous); 203 } 204 205 214 public boolean contains(Object o) { 215 return indexOf(o) != -1; 216 } 217 218 223 public int size() { 224 return size; 225 } 226 227 230 public void clear() { 231 header.next = header; 232 header.previous = header; 233 size = 0; 234 } 235 236 248 public int indexOf(Object o) { 249 int index = 0; 250 if (o == null) { 251 for (LinkedNode n = header.next; n != header; n = n.next) { 252 if (n.contents == null) { 253 return index; 254 } 255 index++; 256 } 257 } else { 258 for (LinkedNode n = header.next; n != header; n = n.next) { 259 if (o.equals(n.contents)) { 260 return index; 261 } 262 index++; 263 } 264 } 265 return -1; 266 } 267 268 private LinkedNode addAfter(Object o, LinkedNode n) { 269 LinkedNode newNode = new LinkedNode(o, n.next, n); 270 newNode.previous.next = newNode; 271 newNode.next.previous = newNode; 272 size++; 273 return newNode; 274 } 275 276 279 public void remove(LinkedNode n) { 280 if (n == header) { 281 throw new NoSuchElementException(); 282 } else if ((n.previous == null) || (n.next == null)) { 283 return; 285 } 286 n.previous.next = n.next; 287 n.next.previous = n.previous; 288 n.previous = null; 290 n.next = null; 291 n.contents = null; 292 size--; 293 } 294 295 298 public void moveFirst(LinkedNode node) { 299 if (node == header) { 300 throw new NoSuchElementException(); 301 } else if ((node.previous == null) || (node.next == null)) { 302 size++; 304 } else { 305 node.previous.next = node.next; 306 node.next.previous = node.previous; 307 } 308 node.next = header.next; 309 node.previous = header; 310 header.next = node; 311 node.next.previous = node; 312 } 313 } 314 | Popular Tags |