--- jsr166/src/main/java/util/PriorityQueue.java 2016/12/02 07:11:36 1.115 +++ jsr166/src/main/java/util/PriorityQueue.java 2018/05/06 02:08:36 1.122 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,7 @@ package java.util; import java.util.function.Consumer; +import jdk.internal.misc.SharedSecrets; /** * An unbounded priority {@linkplain Queue queue} based on a priority heap. @@ -73,7 +74,7 @@ import java.util.function.Consumer; * ({@code peek}, {@code element}, and {@code size}). * *

This class is a member of the - * + * * Java Collections Framework. * * @since 1.5 @@ -522,6 +523,8 @@ public class PriorityQueue extends Ab */ private int expectedModCount = modCount; + Itr() {} // prevent access constructor creation + public boolean hasNext() { return cursor < size || (forgetMeNot != null && !forgetMeNot.isEmpty()); @@ -631,7 +634,7 @@ public class PriorityQueue extends Ab * promoting x up the tree until it is greater than or equal to * its parent, or is the root. * - * To simplify and speed up coercions and comparisons. the + * To simplify and speed up coercions and comparisons, the * Comparable and Comparator versions are separated into different * methods that are otherwise identical. (Similarly for siftDown.) * @@ -732,12 +735,12 @@ public class PriorityQueue extends Ab @SuppressWarnings("unchecked") private void heapify() { final Object[] es = queue; - final int half = (size >>> 1) - 1; + int i = (size >>> 1) - 1; if (comparator == null) - for (int i = half; i >= 0; i--) + for (; i >= 0; i--) siftDownComparable(i, (E) es[i]); else - for (int i = half; i >= 0; i--) + for (; i >= 0; i--) siftDownUsingComparator(i, (E) es[i]); } @@ -793,6 +796,7 @@ public class PriorityQueue extends Ab // Read in (and discard) array length s.readInt(); + SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); queue = new Object[size]; // Read in all elements. @@ -891,4 +895,18 @@ public class PriorityQueue extends Ab return Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.NONNULL; } } + + /** + * @throws NullPointerException {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public void forEach(Consumer action) { + Objects.requireNonNull(action); + final int expectedModCount = modCount; + final Object[] es = queue; + for (int i = 0, n = size; i < n; i++) + action.accept((E) es[i]); + if (expectedModCount != modCount) + throw new ConcurrentModificationException(); + } }