--- jsr166/src/main/java/util/PriorityQueue.java 2013/01/16 15:06:10 1.81 +++ jsr166/src/main/java/util/PriorityQueue.java 2013/01/22 19:28:05 1.86 @@ -60,7 +60,7 @@ import java.util.function.Block; * the priority queue in any particular order. If you need ordered * traversal, consider using {@code Arrays.sort(pq.toArray())}. * - *

Note that this implementation is not synchronized. + *

Note that this implementation is not synchronized. * Multiple threads should not access a {@code PriorityQueue} * instance concurrently if any of the threads modifies the queue. * Instead, use the thread-safe {@link @@ -451,7 +451,9 @@ public class PriorityQueue extends Ab * this queue * @throws NullPointerException if the specified array is null */ + @SuppressWarnings("unchecked") public T[] toArray(T[] a) { + final int size = this.size; if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(queue, size, a.getClass()); @@ -783,10 +785,10 @@ public class PriorityQueue extends Ab } // wrapping constructor in method avoids transient javac problems - final PriorityQueueSpliterator spliterator(int origin, int fence, + final PriorityQueueSpliterator spliterator(int origin, int fence, int expectedModCount) { - return new PriorityQueueSpliterator(this, origin, fence, - expectedModCount); + return new PriorityQueueSpliterator(this, origin, fence, + expectedModCount); } public Stream stream() { @@ -801,8 +803,7 @@ public class PriorityQueue extends Ab } /** Index-based split-by-two Spliterator */ - static final class PriorityQueueSpliterator - implements Spliterator, Iterator { + static final class PriorityQueueSpliterator implements Spliterator { private final PriorityQueue pq; private int index; // current index, modified on advance/split private final int fence; // one past last index @@ -819,7 +820,7 @@ public class PriorityQueue extends Ab int lo = index, mid = (lo + fence) >>> 1; return (lo >= mid) ? null : new PriorityQueueSpliterator(pq, lo, index = mid, - expectedModCount); + expectedModCount); } public void forEach(Block block) { @@ -840,11 +841,11 @@ public class PriorityQueue extends Ab public boolean tryAdvance(Block block) { if (index >= 0 && index < fence) { - if (pq.modCount != expectedModCount) - throw new ConcurrentModificationException(); @SuppressWarnings("unchecked") E e = (E)pq.queue[index++]; block.accept(e); + if (pq.modCount != expectedModCount) + throw new ConcurrentModificationException(); return true; } return false; @@ -853,20 +854,5 @@ public class PriorityQueue extends Ab public long estimateSize() { return (long)(fence - index); } public boolean hasExactSize() { return true; } public boolean hasExactSplits() { return true; } - - // Iterator support - public Iterator iterator() { return this; } - public void remove() { throw new UnsupportedOperationException(); } - public boolean hasNext() { return index >= 0 && index < fence; } - - public E next() { - if (index < 0 || index >= fence) - throw new NoSuchElementException(); - if (pq.modCount != expectedModCount) - throw new ConcurrentModificationException(); - @SuppressWarnings("unchecked") E e = - (E) pq.queue[index++]; - return e; - } } }