1 |
|
package java.util; |
2 |
|
|
3 |
– |
/* |
4 |
– |
* Todo |
5 |
– |
* |
6 |
– |
* 1) Make it serializable. |
7 |
– |
*/ |
8 |
– |
|
3 |
|
/** |
4 |
< |
* An unbounded priority queue based on a priority heap. This queue orders |
5 |
< |
* elements according to the order specified at creation time. This order is |
6 |
< |
* specified as for {@link TreeSet} and {@link TreeMap}: Elements are ordered |
4 |
> |
* A priority queue based on a priority heap. This queue orders |
5 |
> |
* elements according to an order specified at construction time, which is |
6 |
> |
* specified in the same manner as {@link java.util.TreeSet} and |
7 |
> |
* {@link java.util.TreeMap}: elements are ordered |
8 |
|
* either according to their <i>natural order</i> (see {@link Comparable}), or |
9 |
< |
* according to a {@link Comparator}, depending on which constructor is used. |
10 |
< |
* The {@link #peek}, {@link #poll}, and {@link #remove} methods return the |
11 |
< |
* minimal element with respect to the specified ordering. If multiple |
12 |
< |
* these elements are tied for least value, no guarantees are made as to |
13 |
< |
* which of elements is returned. |
9 |
> |
* according to a {@link java.util.Comparator}, depending on which |
10 |
> |
* constructor is used. |
11 |
> |
* <p>The <em>head</em> of this queue is the <em>least</em> element with |
12 |
> |
* respect to the specified ordering. |
13 |
> |
* If multiple elements are tied for least value, the |
14 |
> |
* head is one of those elements. A priority queue does not permit |
15 |
> |
* <tt>null</tt> elements. |
16 |
> |
* |
17 |
> |
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
18 |
> |
* return the head of the queue. |
19 |
> |
* |
20 |
> |
* <p>The {@link #element()} and {@link #peek()} methods return, but do |
21 |
> |
* not delete, the head of the queue. |
22 |
|
* |
23 |
< |
* <p>Each priority queue has a <i>capacity</i>. The capacity is the size of |
24 |
< |
* the array used to store the elements on the queue. It is always at least |
25 |
< |
* as large as the queue size. As elements are added to a priority list, |
26 |
< |
* its capacity grows automatically. The details of the growth policy are not |
27 |
< |
* specified. |
23 |
> |
* <p>A priority queue has a <i>capacity</i>. The capacity is the |
24 |
> |
* size of the array used internally to store the elements on the |
25 |
> |
* queue, and is limited to <tt>Integer.MAX_VALUE-1</tt>. |
26 |
> |
* It is always at least as large as the queue size. As |
27 |
> |
* elements are added to a priority queue, its capacity grows |
28 |
> |
* automatically. The details of the growth policy are not specified. |
29 |
|
* |
30 |
< |
*<p>Implementation note: this implementation provides O(log(n)) time for |
31 |
< |
* the <tt>offer</tt>, <tt>poll</tt>, <tt>remove()</tt> and <tt>add</tt> |
32 |
< |
* methods; linear time for the <tt>remove(Object)</tt> and |
33 |
< |
* <tt>contains</tt> methods; and constant time for the <tt>peek</tt>, |
34 |
< |
* <tt>element</tt>, and <tt>size</tt> methods. |
30 |
> |
* <p>Implementation note: this implementation provides O(log(n)) time |
31 |
> |
* for the insertion methods (<tt>offer</tt>, <tt>poll</tt>, |
32 |
> |
* <tt>remove()</tt> and <tt>add</tt>) methods; linear time for the |
33 |
> |
* <tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and |
34 |
> |
* constant time for the retrieval methods (<tt>peek</tt>, |
35 |
> |
* <tt>element</tt>, and <tt>size</tt>). |
36 |
|
* |
37 |
|
* <p>This class is a member of the |
38 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
39 |
|
* Java Collections Framework</a>. |
40 |
+ |
* @since 1.5 |
41 |
+ |
* @author Josh Bloch |
42 |
|
*/ |
43 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
44 |
< |
implements Queue<E> |
45 |
< |
{ |
44 |
> |
implements Sorted, Queue<E>, java.io.Serializable { |
45 |
> |
|
46 |
|
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
47 |
|
|
48 |
|
/** |
49 |
|
* Priority queue represented as a balanced binary heap: the two children |
50 |
|
* of queue[n] are queue[2*n] and queue[2*n + 1]. The priority queue is |
51 |
|
* ordered by comparator, or by the elements' natural ordering, if |
52 |
< |
* comparator is null: For each node n in the heap, and each descendant |
53 |
< |
* of n, d, n <= d. |
52 |
> |
* comparator is null: For each node n in the heap and each descendant d |
53 |
> |
* of n, n <= d. |
54 |
|
* |
55 |
< |
* The element with the lowest value is in queue[1] (assuming the queue is |
56 |
< |
* nonempty). A one-based array is used in preference to the traditional |
57 |
< |
* zero-based array to simplify parent and child calculations. |
55 |
> |
* The element with the lowest value is in queue[1], assuming the queue is |
56 |
> |
* nonempty. (A one-based array is used in preference to the traditional |
57 |
> |
* zero-based array to simplify parent and child calculations.) |
58 |
|
* |
59 |
|
* queue.length must be >= 2, even if size == 0. |
60 |
|
*/ |
61 |
< |
private E[] queue; |
61 |
> |
private transient Object[] queue; |
62 |
|
|
63 |
|
/** |
64 |
|
* The number of elements in the priority queue. |
69 |
|
* The comparator, or null if priority queue uses elements' |
70 |
|
* natural ordering. |
71 |
|
*/ |
72 |
< |
private final Comparator<E> comparator; |
72 |
> |
private final Comparator<? super E> comparator; |
73 |
|
|
74 |
|
/** |
75 |
|
* The number of times this priority queue has been |
76 |
|
* <i>structurally modified</i>. See AbstractList for gory details. |
77 |
|
*/ |
78 |
< |
private int modCount = 0; |
78 |
> |
private transient int modCount = 0; |
79 |
|
|
80 |
|
/** |
81 |
< |
* Create a new priority queue with the default initial capacity (11) |
82 |
< |
* that orders its elements according to their natural ordering. |
81 |
> |
* Create a <tt>PriorityQueue</tt> with the default initial capacity |
82 |
> |
* (11) that orders its elements according to their natural |
83 |
> |
* ordering (using <tt>Comparable</tt>.) |
84 |
|
*/ |
85 |
|
public PriorityQueue() { |
86 |
< |
this(DEFAULT_INITIAL_CAPACITY); |
86 |
> |
this(DEFAULT_INITIAL_CAPACITY, null); |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
< |
* Create a new priority queue with the specified initial capacity |
91 |
< |
* that orders its elements according to their natural ordering. |
90 |
> |
* Create a <tt>PriorityQueue</tt> with the specified initial capacity |
91 |
> |
* that orders its elements according to their natural ordering |
92 |
> |
* (using <tt>Comparable</tt>.) |
93 |
|
* |
94 |
|
* @param initialCapacity the initial capacity for this priority queue. |
95 |
|
*/ |
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
< |
* Create a new priority queue with the specified initial capacity (11) |
101 |
> |
* Create a <tt>PriorityQueue</tt> with the specified initial capacity |
102 |
|
* that orders its elements according to the specified comparator. |
103 |
|
* |
104 |
|
* @param initialCapacity the initial capacity for this priority queue. |
105 |
|
* @param comparator the comparator used to order this priority queue. |
106 |
+ |
* If <tt>null</tt> then the order depends on the elements' natural |
107 |
+ |
* ordering. |
108 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
109 |
+ |
* than 1 |
110 |
|
*/ |
111 |
< |
public PriorityQueue(int initialCapacity, Comparator<E> comparator) { |
111 |
> |
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { |
112 |
|
if (initialCapacity < 1) |
113 |
< |
initialCapacity = 1; |
114 |
< |
queue = new E[initialCapacity + 1]; |
113 |
> |
throw new IllegalArgumentException(); |
114 |
> |
this.queue = new Object[initialCapacity + 1]; |
115 |
|
this.comparator = comparator; |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
< |
* Create a new priority queue containing the elements in the specified |
119 |
> |
* Create a <tt>PriorityQueue</tt> containing the elements in the specified |
120 |
|
* collection. The priority queue has an initial capacity of 110% of the |
121 |
< |
* size of the specified collection. If the specified collection |
121 |
> |
* size of the specified collection (bounded by |
122 |
> |
* <tt>Integer.MAX_VALUE-1</tt>); or 1 if the collection is empty. |
123 |
> |
* If the specified collection |
124 |
|
* implements the {@link Sorted} interface, the priority queue will be |
125 |
|
* sorted according to the same comparator, or according to its elements' |
126 |
|
* natural order if the collection is sorted according to its elements' |
127 |
< |
* natural order. If the specified collection does not implement the |
128 |
< |
* <tt>Sorted</tt> interface, the priority queue is ordered according to |
127 |
> |
* natural order. If the specified collection does not implement |
128 |
> |
* <tt>Sorted</tt>, the priority queue is ordered according to |
129 |
|
* its elements' natural order. |
130 |
|
* |
131 |
< |
* @param initialElements the collection whose elements are to be placed |
131 |
> |
* @param c the collection whose elements are to be placed |
132 |
|
* into this priority queue. |
133 |
|
* @throws ClassCastException if elements of the specified collection |
134 |
|
* cannot be compared to one another according to the priority |
135 |
|
* queue's ordering. |
136 |
< |
* @throws NullPointerException if the specified collection or an |
137 |
< |
* element of the specified collection is <tt>null</tt>. |
136 |
> |
* @throws NullPointerException if <tt>c</tt> or any element within it |
137 |
> |
* is <tt>null</tt> |
138 |
|
*/ |
139 |
< |
public PriorityQueue(Collection<E> initialElements) { |
140 |
< |
int sz = initialElements.size(); |
139 |
> |
public PriorityQueue(Collection<? extends E> c) { |
140 |
> |
int sz = c.size(); |
141 |
|
int initialCapacity = (int)Math.min((sz * 110L) / 100, |
142 |
|
Integer.MAX_VALUE - 1); |
143 |
|
if (initialCapacity < 1) |
144 |
|
initialCapacity = 1; |
130 |
– |
queue = new E[initialCapacity + 1]; |
145 |
|
|
146 |
< |
/* Commented out to compile with generics compiler |
146 |
> |
this.queue = new Object[initialCapacity + 1]; |
147 |
|
|
148 |
< |
if (initialElements instanceof Sorted) { |
149 |
< |
comparator = ((Sorted)initialElements).comparator(); |
150 |
< |
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
151 |
< |
queue[++size] = i.next(); |
148 |
> |
// FIXME: if c is larger than Integer.MAX_VALUE we'll |
149 |
> |
// overflow the array |
150 |
> |
|
151 |
> |
if (c instanceof Sorted) { |
152 |
> |
comparator = ((Sorted)c).comparator(); |
153 |
|
} else { |
139 |
– |
*/ |
140 |
– |
{ |
154 |
|
comparator = null; |
142 |
– |
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
143 |
– |
add(i.next()); |
155 |
|
} |
156 |
+ |
|
157 |
+ |
for (Iterator<? extends E> i = c.iterator(); i.hasNext(); ) |
158 |
+ |
add(i.next()); |
159 |
|
} |
160 |
|
|
161 |
|
// Queue Methods |
162 |
|
|
163 |
|
/** |
164 |
< |
* Remove and return the minimal element from this priority queue if |
151 |
< |
* it contains one or more elements, otherwise <tt>null</tt>. The term |
152 |
< |
* <i>minimal</i> is defined according to this priority queue's order. |
164 |
> |
* Add the specified element to this priority queue. |
165 |
|
* |
166 |
< |
* @return the minimal element from this priority queue if it contains |
167 |
< |
* one or more elements, otherwise <tt>null</tt>. |
166 |
> |
* @return <tt>true</tt> |
167 |
> |
* @throws ClassCastException if the specified element cannot be compared |
168 |
> |
* with elements currently in the priority queue according |
169 |
> |
* to the priority queue's ordering. |
170 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
171 |
|
*/ |
172 |
+ |
public boolean offer(E o) { |
173 |
+ |
if (o == null) |
174 |
+ |
throw new NullPointerException(); |
175 |
+ |
modCount++; |
176 |
+ |
++size; |
177 |
+ |
|
178 |
+ |
// Grow backing store if necessary |
179 |
+ |
// FIXME: watch for overflow |
180 |
+ |
// FIXME: what if we're full? |
181 |
+ |
while (size >= queue.length) { |
182 |
+ |
Object[] newQueue = new Object[2 * queue.length]; |
183 |
+ |
System.arraycopy(queue, 0, newQueue, 0, queue.length); |
184 |
+ |
queue = newQueue; |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
queue[size] = o; |
188 |
+ |
fixUp(size); |
189 |
+ |
return true; |
190 |
+ |
} |
191 |
+ |
|
192 |
|
public E poll() { |
193 |
|
if (size == 0) |
194 |
|
return null; |
195 |
< |
return remove(1); |
195 |
> |
return (E) remove(1); |
196 |
|
} |
197 |
|
|
163 |
– |
/** |
164 |
– |
* Return, but do not remove, the minimal element from the priority queue, |
165 |
– |
* or <tt>null</tt> if the queue is empty. The term <i>minimal</i> is |
166 |
– |
* defined according to this priority queue's order. This method returns |
167 |
– |
* the same object reference that would be returned by by the |
168 |
– |
* <tt>poll</tt> method. The two methods differ in that this method |
169 |
– |
* does not remove the element from the priority queue. |
170 |
– |
* |
171 |
– |
* @return the minimal element from this priority queue if it contains |
172 |
– |
* one or more elements, otherwise <tt>null</tt>. |
173 |
– |
*/ |
198 |
|
public E peek() { |
199 |
< |
return queue[1]; |
199 |
> |
return (E) queue[1]; |
200 |
|
} |
201 |
|
|
202 |
|
// Collection Methods |
203 |
|
|
204 |
+ |
// these first two override just to get the throws docs |
205 |
+ |
|
206 |
|
/** |
207 |
< |
* Removes a single instance of the specified element from this priority |
182 |
< |
* queue, if it is present. Returns true if this collection contained the |
183 |
< |
* specified element (or equivalently, if this collection changed as a |
184 |
< |
* result of the call). |
185 |
< |
* |
186 |
< |
* @param o element to be removed from this collection, if present. |
187 |
< |
* @return <tt>true</tt> if this collection changed as a result of the |
188 |
< |
* call |
207 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
208 |
|
* @throws ClassCastException if the specified element cannot be compared |
209 |
< |
* with elements currently in the priority queue according |
210 |
< |
* to the priority queue's ordering. |
192 |
< |
* @throws NullPointerException if the specified element is null. |
209 |
> |
* with elements currently in the priority queue according |
210 |
> |
* to the priority queue's ordering. |
211 |
|
*/ |
212 |
< |
public boolean remove(Object element) { |
213 |
< |
if (element == null) |
214 |
< |
throw new NullPointerException(); |
212 |
> |
public boolean add(E o) { |
213 |
> |
return super.add(o); |
214 |
> |
} |
215 |
> |
|
216 |
> |
/** |
217 |
> |
* @throws ClassCastException if any element cannot be compared |
218 |
> |
* with elements currently in the priority queue according |
219 |
> |
* to the priority queue's ordering. |
220 |
> |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
221 |
> |
* is <tt>null</tt> |
222 |
> |
*/ |
223 |
> |
public boolean addAll(Collection<? extends E> c) { |
224 |
> |
return super.addAll(c); |
225 |
> |
} |
226 |
> |
|
227 |
> |
public boolean remove(Object o) { |
228 |
> |
if (o == null) |
229 |
> |
return false; |
230 |
|
|
231 |
|
if (comparator == null) { |
232 |
|
for (int i = 1; i <= size; i++) { |
233 |
< |
if (((Comparable)queue[i]).compareTo(element) == 0) { |
233 |
> |
if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) { |
234 |
|
remove(i); |
235 |
|
return true; |
236 |
|
} |
237 |
|
} |
238 |
|
} else { |
239 |
|
for (int i = 1; i <= size; i++) { |
240 |
< |
if (comparator.compare(queue[i], (E) element) == 0) { |
240 |
> |
if (comparator.compare((E)queue[i], (E)o) == 0) { |
241 |
|
remove(i); |
242 |
|
return true; |
243 |
|
} |
246 |
|
return false; |
247 |
|
} |
248 |
|
|
216 |
– |
/** |
217 |
– |
* Returns an iterator over the elements in this priority queue. The |
218 |
– |
* first element returned by this iterator is the same element that |
219 |
– |
* would be returned by a call to <tt>peek</tt>. |
220 |
– |
* |
221 |
– |
* @return an <tt>Iterator</tt> over the elements in this priority queue. |
222 |
– |
*/ |
249 |
|
public Iterator<E> iterator() { |
250 |
|
return new Itr(); |
251 |
|
} |
255 |
|
* Index (into queue array) of element to be returned by |
256 |
|
* subsequent call to next. |
257 |
|
*/ |
258 |
< |
int cursor = 1; |
258 |
> |
private int cursor = 1; |
259 |
|
|
260 |
|
/** |
261 |
|
* Index of element returned by most recent call to next or |
262 |
|
* previous. Reset to 0 if this element is deleted by a call |
263 |
|
* to remove. |
264 |
|
*/ |
265 |
< |
int lastRet = 0; |
265 |
> |
private int lastRet = 0; |
266 |
|
|
267 |
|
/** |
268 |
|
* The modCount value that the iterator believes that the backing |
269 |
|
* List should have. If this expectation is violated, the iterator |
270 |
|
* has detected concurrent modification. |
271 |
|
*/ |
272 |
< |
int expectedModCount = modCount; |
272 |
> |
private int expectedModCount = modCount; |
273 |
|
|
274 |
|
public boolean hasNext() { |
275 |
|
return cursor <= size; |
279 |
|
checkForComodification(); |
280 |
|
if (cursor > size) |
281 |
|
throw new NoSuchElementException(); |
282 |
< |
E result = queue[cursor]; |
282 |
> |
E result = (E) queue[cursor]; |
283 |
|
lastRet = cursor++; |
284 |
|
return result; |
285 |
|
} |
312 |
|
} |
313 |
|
|
314 |
|
/** |
289 |
– |
* Add the specified element to this priority queue. |
290 |
– |
* |
291 |
– |
* @param element the element to add. |
292 |
– |
* @return true |
293 |
– |
* @throws ClassCastException if the specified element cannot be compared |
294 |
– |
* with elements currently in the priority queue according |
295 |
– |
* to the priority queue's ordering. |
296 |
– |
* @throws NullPointerException if the specified element is null. |
297 |
– |
*/ |
298 |
– |
public boolean offer(E element) { |
299 |
– |
if (element == null) |
300 |
– |
throw new NullPointerException(); |
301 |
– |
modCount++; |
302 |
– |
|
303 |
– |
// Grow backing store if necessary |
304 |
– |
if (++size == queue.length) { |
305 |
– |
E[] newQueue = new E[2 * queue.length]; |
306 |
– |
System.arraycopy(queue, 0, newQueue, 0, size); |
307 |
– |
queue = newQueue; |
308 |
– |
} |
309 |
– |
|
310 |
– |
queue[size] = element; |
311 |
– |
fixUp(size); |
312 |
– |
return true; |
313 |
– |
} |
314 |
– |
|
315 |
– |
/** |
315 |
|
* Remove all elements from the priority queue. |
316 |
|
*/ |
317 |
|
public void clear() { |
335 |
|
assert i <= size; |
336 |
|
modCount++; |
337 |
|
|
338 |
< |
E result = queue[i]; |
338 |
> |
E result = (E) queue[i]; |
339 |
|
queue[i] = queue[size]; |
340 |
|
queue[size--] = null; // Drop extra ref to prevent memory leak |
341 |
|
if (i <= size) |
356 |
|
if (comparator == null) { |
357 |
|
while (k > 1) { |
358 |
|
int j = k >> 1; |
359 |
< |
if (((Comparable)queue[j]).compareTo(queue[k]) <= 0) |
359 |
> |
if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0) |
360 |
|
break; |
361 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
361 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
362 |
|
k = j; |
363 |
|
} |
364 |
|
} else { |
365 |
|
while (k > 1) { |
366 |
|
int j = k >> 1; |
367 |
< |
if (comparator.compare(queue[j], queue[k]) <= 0) |
367 |
> |
if (comparator.compare((E)queue[j], (E)queue[k]) <= 0) |
368 |
|
break; |
369 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
369 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
370 |
|
k = j; |
371 |
|
} |
372 |
|
} |
385 |
|
int j; |
386 |
|
if (comparator == null) { |
387 |
|
while ((j = k << 1) <= size) { |
388 |
< |
if (j<size && ((Comparable)queue[j]).compareTo(queue[j+1]) > 0) |
388 |
> |
if (j<size && ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0) |
389 |
|
j++; // j indexes smallest kid |
390 |
< |
if (((Comparable)queue[k]).compareTo(queue[j]) <= 0) |
390 |
> |
if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0) |
391 |
|
break; |
392 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
392 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
393 |
|
k = j; |
394 |
|
} |
395 |
|
} else { |
396 |
|
while ((j = k << 1) <= size) { |
397 |
< |
if (j < size && comparator.compare(queue[j], queue[j+1]) > 0) |
397 |
> |
if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0) |
398 |
|
j++; // j indexes smallest kid |
399 |
< |
if (comparator.compare(queue[k], queue[j]) <= 0) |
399 |
> |
if (comparator.compare((E)queue[k], (E)queue[j]) <= 0) |
400 |
|
break; |
401 |
< |
E tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
401 |
> |
Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp; |
402 |
|
k = j; |
403 |
|
} |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
+ |
public Comparator<? super E> comparator() { |
408 |
+ |
return comparator; |
409 |
+ |
} |
410 |
+ |
|
411 |
|
/** |
412 |
< |
* Returns the comparator associated with this priority queue, or |
413 |
< |
* <tt>null</tt> if it uses its elements' natural ordering. |
412 |
> |
* Save the state of the instance to a stream (that |
413 |
> |
* is, serialize it). |
414 |
|
* |
415 |
< |
* @return the comparator associated with this priority queue, or |
416 |
< |
* <tt>null</tt> if it uses its elements' natural ordering. |
415 |
> |
* @serialData The length of the array backing the instance is |
416 |
> |
* emitted (int), followed by all of its elements (each an |
417 |
> |
* <tt>Object</tt>) in the proper order. |
418 |
> |
* @param s the stream |
419 |
|
*/ |
420 |
< |
Comparator<E> comparator() { |
421 |
< |
return comparator; |
420 |
> |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
421 |
> |
throws java.io.IOException{ |
422 |
> |
// Write out element count, and any hidden stuff |
423 |
> |
s.defaultWriteObject(); |
424 |
> |
|
425 |
> |
// Write out array length |
426 |
> |
s.writeInt(queue.length); |
427 |
> |
|
428 |
> |
// Write out all elements in the proper order. |
429 |
> |
for (int i=0; i<size; i++) |
430 |
> |
s.writeObject(queue[i]); |
431 |
|
} |
432 |
+ |
|
433 |
+ |
/** |
434 |
+ |
* Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, |
435 |
+ |
* deserialize it). |
436 |
+ |
* @param s the stream |
437 |
+ |
*/ |
438 |
+ |
private synchronized void readObject(java.io.ObjectInputStream s) |
439 |
+ |
throws java.io.IOException, ClassNotFoundException { |
440 |
+ |
// Read in size, and any hidden stuff |
441 |
+ |
s.defaultReadObject(); |
442 |
+ |
|
443 |
+ |
// Read in array length and allocate array |
444 |
+ |
int arrayLength = s.readInt(); |
445 |
+ |
queue = new Object[arrayLength]; |
446 |
+ |
|
447 |
+ |
// Read in all elements in the proper order. |
448 |
+ |
for (int i=0; i<size; i++) |
449 |
+ |
queue[i] = s.readObject(); |
450 |
+ |
} |
451 |
+ |
|
452 |
|
} |
453 |
+ |
|