ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/PriorityQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/PriorityQueue.java (file contents):
Revision 1.103 by jsr166, Wed Dec 31 09:37:20 2014 UTC vs.
Revision 1.108 by jsr166, Sun Oct 11 00:50:06 2015 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5   * This code is free software; you can redistribute it and/or modify it
# Line 99 | Line 99 | public class PriorityQueue<E> extends Ab
99      /**
100       * The number of elements in the priority queue.
101       */
102 <    private int size;
102 >    int size;
103  
104      /**
105       * The comparator, or null if priority queue uses elements'
# Line 111 | Line 111 | public class PriorityQueue<E> extends Ab
111       * The number of times this priority queue has been
112       * <i>structurally modified</i>.  See AbstractList for gory details.
113       */
114 <    transient int modCount = 0; // non-private to simplify nested class access
114 >    transient int modCount;     // non-private to simplify nested class access
115  
116      /**
117       * Creates a {@code PriorityQueue} with the default initial
# Line 136 | Line 136 | public class PriorityQueue<E> extends Ab
136      }
137  
138      /**
139 +     * Creates a {@code PriorityQueue} with the default initial capacity and
140 +     * whose elements are ordered according to the specified comparator.
141 +     *
142 +     * @param  comparator the comparator that will be used to order this
143 +     *         priority queue.  If {@code null}, the {@linkplain Comparable
144 +     *         natural ordering} of the elements will be used.
145 +     * @since 1.8
146 +     */
147 +    public PriorityQueue(Comparator<? super E> comparator) {
148 +        this(DEFAULT_INITIAL_CAPACITY, comparator);
149 +    }
150 +
151 +    /**
152       * Creates a {@code PriorityQueue} with the specified initial capacity
153       * that orders its elements according to the specified comparator.
154       *
# Line 245 | Line 258 | public class PriorityQueue<E> extends Ab
258              a = Arrays.copyOf(a, a.length, Object[].class);
259          int len = a.length;
260          if (len == 1 || this.comparator != null)
261 <            for (int i = 0; i < len; i++)
262 <                if (a[i] == null)
261 >            for (Object e : a)
262 >                if (e == null)
263                      throw new NullPointerException();
264          this.queue = a;
265          this.size = a.length;
# Line 435 | Line 448 | public class PriorityQueue<E> extends Ab
448       * The following code can be used to dump the queue into a newly
449       * allocated array of {@code String}:
450       *
451 <     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
451 >     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
452       *
453       * Note that {@code toArray(new Object[0])} is identical in function to
454       * {@code toArray()}.
# Line 596 | Line 609 | public class PriorityQueue<E> extends Ab
609       * avoid missing traversing elements.
610       */
611      @SuppressWarnings("unchecked")
612 <    private E removeAt(int i) {
612 >    E removeAt(int i) {
613          // assert i >= 0 && i < size;
614          modCount++;
615          int s = --size;
# Line 786 | Line 799 | public class PriorityQueue<E> extends Ab
799          heapify();
800      }
801  
789    public Spliterator<E> spliterator() {
790        return new PriorityQueueSpliterator<E>(this, 0, -1, 0);
791    }
792
802      /**
803 <     * This is very similar to ArrayList Spliterator, except for extra
804 <     * null checks.
803 >     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
804 >     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
805 >     * queue.
806 >     *
807 >     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
808 >     * {@link Spliterator#SUBSIZED}, and {@link Spliterator#NONNULL}.
809 >     * Overriding implementations should document the reporting of additional
810 >     * characteristic values.
811 >     *
812 >     * @return a {@code Spliterator} over the elements in this queue
813 >     * @since 1.8
814       */
815 +    public final Spliterator<E> spliterator() {
816 +        return new PriorityQueueSpliterator<>(this, 0, -1, 0);
817 +    }
818 +
819      static final class PriorityQueueSpliterator<E> implements Spliterator<E> {
820 +        /*
821 +         * This is very similar to ArrayList Spliterator, except for
822 +         * extra null checks.
823 +         */
824          private final PriorityQueue<E> pq;
825          private int index;            // current index, modified on advance/split
826          private int fence;            // -1 until first use
827          private int expectedModCount; // initialized when fence set
828  
829 <        /** Creates new spliterator covering the given range */
829 >        /** Creates new spliterator covering the given range. */
830          PriorityQueueSpliterator(PriorityQueue<E> pq, int origin, int fence,
831 <                             int expectedModCount) {
831 >                                 int expectedModCount) {
832              this.pq = pq;
833              this.index = origin;
834              this.fence = fence;
# Line 818 | Line 844 | public class PriorityQueue<E> extends Ab
844              return hi;
845          }
846  
847 <        public Spliterator<E> trySplit() {
847 >        public PriorityQueueSpliterator<E> trySplit() {
848              int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
849              return (lo >= mid) ? null :
850 <                new PriorityQueueSpliterator<E>(pq, lo, index = mid,
851 <                                                expectedModCount);
850 >                new PriorityQueueSpliterator<>(pq, lo, index = mid,
851 >                                               expectedModCount);
852          }
853  
854          @SuppressWarnings("unchecked")
# Line 856 | Line 882 | public class PriorityQueue<E> extends Ab
882          }
883  
884          public boolean tryAdvance(Consumer<? super E> action) {
885 +            if (action == null)
886 +                throw new NullPointerException();
887              int hi = getFence(), lo = index;
888              if (lo >= 0 && lo < hi) {
889                  index = lo + 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines