27 |
|
import java.util.stream.Stream; |
28 |
|
import java.util.Spliterator; |
29 |
|
import java.util.stream.Streams; |
30 |
< |
import java.util.function.Block; |
30 |
> |
import java.util.function.Consumer; |
31 |
|
|
32 |
|
/** |
33 |
|
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
60 |
|
* the priority queue in any particular order. If you need ordered |
61 |
|
* traversal, consider using {@code Arrays.sort(pq.toArray())}. |
62 |
|
* |
63 |
< |
* <p> <strong>Note that this implementation is not synchronized.</strong> |
63 |
> |
* <p><strong>Note that this implementation is not synchronized.</strong> |
64 |
|
* Multiple threads should not access a {@code PriorityQueue} |
65 |
|
* instance concurrently if any of the threads modifies the queue. |
66 |
|
* Instead, use the thread-safe {@link |
451 |
|
* this queue |
452 |
|
* @throws NullPointerException if the specified array is null |
453 |
|
*/ |
454 |
+ |
@SuppressWarnings("unchecked") |
455 |
|
public <T> T[] toArray(T[] a) { |
456 |
+ |
final int size = this.size; |
457 |
|
if (a.length < size) |
458 |
|
// Make a new array of a's runtime type, but my contents: |
459 |
|
return (T[]) Arrays.copyOf(queue, size, a.getClass()); |
785 |
|
} |
786 |
|
|
787 |
|
// wrapping constructor in method avoids transient javac problems |
788 |
< |
final PriorityQueueSpliterator<E> spliterator(int origin, int fence, |
788 |
> |
final PriorityQueueSpliterator<E> spliterator(int origin, int fence, |
789 |
|
int expectedModCount) { |
790 |
< |
return new PriorityQueueSpliterator(this, origin, fence, |
791 |
< |
expectedModCount); |
790 |
> |
return new PriorityQueueSpliterator<E>(this, origin, fence, |
791 |
> |
expectedModCount); |
792 |
|
} |
793 |
|
|
794 |
|
public Stream<E> stream() { |
803 |
|
} |
804 |
|
|
805 |
|
/** Index-based split-by-two Spliterator */ |
806 |
< |
static final class PriorityQueueSpliterator<E> |
805 |
< |
implements Spliterator<E>, Iterator<E> { |
806 |
> |
static final class PriorityQueueSpliterator<E> implements Spliterator<E> { |
807 |
|
private final PriorityQueue<E> pq; |
808 |
|
private int index; // current index, modified on advance/split |
809 |
|
private final int fence; // one past last index |
820 |
|
int lo = index, mid = (lo + fence) >>> 1; |
821 |
|
return (lo >= mid) ? null : |
822 |
|
new PriorityQueueSpliterator<E>(pq, lo, index = mid, |
823 |
< |
expectedModCount); |
823 |
> |
expectedModCount); |
824 |
|
} |
825 |
|
|
826 |
< |
public void forEach(Block<? super E> block) { |
826 |
> |
public void forEach(Consumer<? super E> block) { |
827 |
|
Object[] a; int i, hi; // hoist accesses and checks from loop |
828 |
|
if (block == null) |
829 |
|
throw new NullPointerException(); |
839 |
|
} |
840 |
|
} |
841 |
|
|
842 |
< |
public boolean tryAdvance(Block<? super E> block) { |
842 |
> |
public boolean tryAdvance(Consumer<? super E> block) { |
843 |
|
if (index >= 0 && index < fence) { |
843 |
– |
if (pq.modCount != expectedModCount) |
844 |
– |
throw new ConcurrentModificationException(); |
844 |
|
@SuppressWarnings("unchecked") E e = |
845 |
|
(E)pq.queue[index++]; |
846 |
|
block.accept(e); |
847 |
+ |
if (pq.modCount != expectedModCount) |
848 |
+ |
throw new ConcurrentModificationException(); |
849 |
|
return true; |
850 |
|
} |
851 |
|
return false; |
854 |
|
public long estimateSize() { return (long)(fence - index); } |
855 |
|
public boolean hasExactSize() { return true; } |
856 |
|
public boolean hasExactSplits() { return true; } |
856 |
– |
|
857 |
– |
// Iterator support |
858 |
– |
public Iterator<E> iterator() { return this; } |
859 |
– |
public void remove() { throw new UnsupportedOperationException(); } |
860 |
– |
public boolean hasNext() { return index >= 0 && index < fence; } |
861 |
– |
|
862 |
– |
public E next() { |
863 |
– |
if (index < 0 || index >= fence) |
864 |
– |
throw new NoSuchElementException(); |
865 |
– |
if (pq.modCount != expectedModCount) |
866 |
– |
throw new ConcurrentModificationException(); |
867 |
– |
@SuppressWarnings("unchecked") E e = |
868 |
– |
(E) pq.queue[index++]; |
869 |
– |
return e; |
870 |
– |
} |
857 |
|
} |
858 |
|
} |