1 7 package org.jboss.cache.eviction; 8 9 import java.util.Arrays ; 10 import java.util.Comparator ; 11 import java.util.ConcurrentModificationException ; 12 import java.util.Iterator ; 13 import java.util.ListIterator ; 14 import java.util.NoSuchElementException ; 15 16 20 public class EvictionQueueList 21 { 22 EvictionListEntry head; 23 EvictionListEntry tail; 24 int modCount; 25 private int size; 26 27 EvictionQueueList() 28 { 29 head = null; 30 tail = null; 31 size = 0; 32 modCount = 0; 33 } 34 35 void addToTop(EvictionListEntry entry) 36 { 37 EvictionListEntry formerHead = head; 38 head = entry; 39 if (formerHead != null) 41 { 42 formerHead.previous = head; 43 head.next = formerHead; 44 head.previous = null; 45 } 46 else 47 { 48 tail = entry; 49 } 50 size++; 51 modCount++; 52 } 53 54 void addToBottom(EvictionListEntry entry) 55 { 56 EvictionListEntry formerTail = tail; 57 tail = entry; 58 if (formerTail != null) 60 { 61 tail.previous = formerTail; 62 formerTail.next = tail; 63 tail.next = null; 64 } 65 else 66 { 67 head = entry; 68 } 69 size++; 70 modCount++; 71 } 72 73 void remove(EvictionListEntry entry) 74 { 75 if (this.isEmpty()) 76 { 77 return; 78 } 79 80 if (isSingleNode(entry)) 81 { 82 head = null; 83 tail = null; 84 } 85 else if (isTail(entry)) 86 { 87 tail = entry.previous; 88 entry.previous.next = null; 90 } 91 else if (isHead(entry)) 92 { 93 head = entry.next; 94 head.previous = null; 95 } 96 else 97 { 98 entry.next.previous = entry.previous; 100 entry.previous.next = entry.next; 101 } 102 size--; 103 modCount++; 104 } 105 106 int size() 107 { 108 return this.size; 109 } 110 111 void clear() 112 { 113 head = null; 114 tail = null; 115 size = 0; 116 modCount++; 117 } 118 119 EvictionListEntry getFirst() 120 { 121 if (head == null) 122 { 123 throw new NoSuchElementException ("List is empty"); 124 } 125 return head; 126 } 127 128 EvictionListEntry getLast() 129 { 130 if (tail == null) 131 { 132 throw new NoSuchElementException ("List is empty"); 133 } 134 return tail; 135 } 136 137 Iterator iterator() 138 { 139 return new EvictionListIterator(); 140 } 141 142 NodeEntry[] toNodeEntryArray() 143 { 144 if (isEmpty()) 145 { 146 return null; 147 } 148 NodeEntry[] ret = new NodeEntry[size]; 149 int i = 0; 150 EvictionListEntry temp = head; 151 152 do 153 { 154 ret[i] = temp.node; 155 temp = temp.next; 156 i++; 157 } 158 while (temp != null); 159 160 return ret; 161 } 162 163 EvictionListEntry[] toArray() 164 { 165 if (isEmpty()) 166 { 167 return null; 168 } 169 EvictionListEntry[] ret = new EvictionListEntry[size]; 170 int i = 0; 171 EvictionListEntry temp = head; 172 173 do 174 { 175 ret[i] = temp; 176 temp = temp.next; 177 i++; 178 } 179 while (temp != null); 180 181 return ret; 182 } 183 184 void fromArray(EvictionListEntry[] array) 185 { 186 187 for (int i = 0; i < array.length; i++) 188 { 189 this.addToBottom(array[i]); 190 } 191 } 192 193 private boolean isEmpty() 194 { 195 return head == null && tail == null; 196 } 197 198 private boolean isSingleNode(EvictionListEntry entry) 199 { 200 return isTail(entry) && isHead(entry); 201 } 202 203 private boolean isTail(EvictionListEntry entry) 204 { 205 return entry.next == null; 206 } 207 208 private boolean isHead(EvictionListEntry entry) 209 { 210 return entry.previous == null; 211 } 212 213 public String toString() 214 { 215 return Arrays.asList(toArray()).toString(); 216 } 217 218 static class EvictionListComparator implements Comparator 219 { 220 Comparator nodeEntryComparator; 221 222 EvictionListComparator(Comparator nodeEntryComparator) 223 { 224 this.nodeEntryComparator = nodeEntryComparator; 225 } 226 227 public int compare(Object o1, Object o2) 228 { 229 EvictionListEntry e1 = (EvictionListEntry) o1; 230 EvictionListEntry e2 = (EvictionListEntry) o2; 231 232 return nodeEntryComparator.compare(e1.node, e2.node); 233 } 234 } 235 236 class EvictionListIterator implements ListIterator 237 { 238 EvictionListEntry next = head; 239 EvictionListEntry previous; 240 EvictionListEntry cursor; 241 242 int initialModCount = EvictionQueueList.this.modCount; 243 244 public boolean hasNext() 245 { 246 this.doConcurrentModCheck(); 247 return next != null; 248 } 249 250 public Object next() 251 { 252 this.doConcurrentModCheck(); 253 this.forwardCursor(); 254 return cursor.node; 255 } 256 257 public boolean hasPrevious() 258 { 259 this.doConcurrentModCheck(); 260 return previous != null; 261 } 262 263 public Object previous() 264 { 265 this.doConcurrentModCheck(); 266 this.rewindCursor(); 267 return cursor.node; 268 } 269 270 public int nextIndex() 271 { 272 throw new UnsupportedOperationException (); 273 } 274 275 public int previousIndex() 276 { 277 throw new UnsupportedOperationException (); 278 } 279 280 public void remove() 281 { 282 this.doConcurrentModCheck(); 283 if (cursor == null) 284 { 285 throw new IllegalStateException ("Cannot remove from iterator when there is nothing at the current iteration point"); 286 } 287 EvictionQueueList.this.remove(cursor); 288 cursor = null; 289 initialModCount++; 290 } 291 292 public void set(Object o) 293 { 294 this.doConcurrentModCheck(); 295 NodeEntry e = (NodeEntry) o; 296 cursor.node = e; 297 } 298 299 public void add(Object o) 300 { 301 this.doConcurrentModCheck(); 302 initialModCount++; 303 } 304 305 private void doConcurrentModCheck() 306 { 307 if (EvictionQueueList.this.modCount != initialModCount) 308 { 309 throw new ConcurrentModificationException (); 310 } 311 } 312 313 private void forwardCursor() 314 { 315 if (next == null) 316 { 317 throw new NoSuchElementException ("No more objects to iterate."); 318 } 319 previous = cursor; 320 cursor = next; 321 next = cursor.next; 322 } 323 324 private void rewindCursor() 325 { 326 if (previous == null) 327 { 328 throw new NoSuchElementException (); 329 } 330 next = cursor; 331 cursor = previous; 332 previous = cursor.previous; 333 } 334 } 335 336 } 337 338 class EvictionListEntry 339 { 340 EvictionListEntry next; 341 EvictionListEntry previous; 342 343 NodeEntry node; 344 345 EvictionListEntry() 346 { 347 } 348 349 EvictionListEntry(NodeEntry node) 350 { 351 this.node = node; 352 } 353 354 public boolean equals(Object o) 355 { 356 EvictionListEntry entry = (EvictionListEntry) o; 357 return this.node.getFqn().equals(entry.node.getFqn()); 358 } 359 360 public int hashCode() 361 { 362 return this.node.getFqn().hashCode(); 363 } 364 365 public String toString() 366 { 367 return "EntryLE=" + node; 368 } 369 370 } 371 | Popular Tags |