src/share/classes/java/util/PriorityQueue.java
Print this page
rev 4788 : Fix bunch of generics warnings
@@ -447,10 +447,11 @@
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this queue
* @throws NullPointerException if the specified array is null
*/
+ @SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(queue, size, a.getClass());
System.arraycopy(queue, 0, a, 0, size);
@@ -512,10 +513,11 @@
public boolean hasNext() {
return cursor < size ||
(forgetMeNot != null && !forgetMeNot.isEmpty());
}
+ @SuppressWarnings("unchecked")
public E next() {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
if (cursor < size)
return (E) queue[lastRet = cursor++];
@@ -569,11 +571,13 @@
public E poll() {
if (size == 0)
return null;
int s = --size;
modCount++;
+ @SuppressWarnings("unchecked")
E result = (E) queue[0];
+ @SuppressWarnings("unchecked")
E x = (E) queue[s];
queue[s] = null;
if (s != 0)
siftDown(0, x);
return result;
@@ -596,10 +600,11 @@
modCount++;
int s = --size;
if (s == i) // removed last element
queue[i] = null;
else {
+ @SuppressWarnings("unchecked")
E moved = (E) queue[s];
queue[s] = null;
siftDown(i, moved);
if (queue[i] == moved) {
siftUp(i, moved);
@@ -627,10 +632,11 @@
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
+ @SuppressWarnings("unchecked")
private void siftUpComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>) x;
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
@@ -643,12 +649,13 @@
}
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
- Object e = queue[parent];
- if (comparator.compare(x, (E) e) >= 0)
+ @SuppressWarnings("unchecked")
+ E e = (E) queue[parent];
+ if (comparator.compare(x, e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = x;
@@ -667,10 +674,11 @@
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}
+ @SuppressWarnings("unchecked")
private void siftDownComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>)x;
int half = size >>> 1; // loop while a non-leaf
while (k < half) {
int child = (k << 1) + 1; // assume left child is least
@@ -685,10 +693,11 @@
k = child;
}
queue[k] = key;
}
+ @SuppressWarnings("unchecked")
private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
int child = (k << 1) + 1;
Object c = queue[child];
@@ -706,10 +715,11 @@
/**
* Establishes the heap invariant (described above) in the entire tree,
* assuming nothing about the order of the elements prior to the call.
*/
+ @SuppressWarnings("unchecked")
private void heapify() {
for (int i = (size >>> 1) - 1; i >= 0; i--)
siftDown(i, (E) queue[i]);
}