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