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

Comparing jsr166/src/main/java/util/ArrayDeque.java (file contents):
Revision 1.126 by jsr166, Thu Dec 8 04:58:50 2016 UTC vs.
Revision 1.134 by jsr166, Sat Jun 30 22:35:55 2018 UTC

# Line 9 | Line 9 | import java.io.Serializable;
9   import java.util.function.Consumer;
10   import java.util.function.Predicate;
11   import java.util.function.UnaryOperator;
12 + import jdk.internal.misc.SharedSecrets;
13  
14   /**
15   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 50 | Line 51 | import java.util.function.UnaryOperator;
51   * Iterator} interfaces.
52   *
53   * <p>This class is a member of the
54 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
54 > * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
55   * Java Collections Framework</a>.
56   *
57   * @author  Josh Bloch and Doug Lea
# Line 180 | Line 181 | public class ArrayDeque<E> extends Abstr
181       * sufficient to hold 16 elements.
182       */
183      public ArrayDeque() {
184 <        elements = new Object[16];
184 >        elements = new Object[16 + 1];
185      }
186  
187      /**
# Line 208 | Line 209 | public class ArrayDeque<E> extends Abstr
209       */
210      public ArrayDeque(Collection<? extends E> c) {
211          this(c.size());
212 <        addAll(c);
212 >        copyElements(c);
213      }
214  
215      /**
216 <     * Increments i, mod modulus.
216 >     * Circularly increments i, mod modulus.
217       * Precondition and postcondition: 0 <= i < modulus.
218       */
219      static final int inc(int i, int modulus) {
# Line 221 | Line 222 | public class ArrayDeque<E> extends Abstr
222      }
223  
224      /**
225 <     * Decrements i, mod modulus.
225 >     * Circularly decrements i, mod modulus.
226       * Precondition and postcondition: 0 <= i < modulus.
227       */
228      static final int dec(int i, int modulus) {
# Line 234 | Line 235 | public class ArrayDeque<E> extends Abstr
235       * Precondition: 0 <= i < modulus, 0 <= distance <= modulus.
236       * @return index 0 <= i < modulus
237       */
238 <    static final int add(int i, int distance, int modulus) {
238 >    static final int inc(int i, int distance, int modulus) {
239          if ((i += distance) - modulus >= 0) i -= modulus;
240          return i;
241      }
# Line 244 | Line 245 | public class ArrayDeque<E> extends Abstr
245       * Index i must be logically ahead of index j.
246       * Precondition: 0 <= i < modulus, 0 <= j < modulus.
247       * @return the "circular distance" from j to i; corner case i == j
248 <     * is diambiguated to "empty", returning 0.
248 >     * is disambiguated to "empty", returning 0.
249       */
250      static final int sub(int i, int j, int modulus) {
251          if ((i -= j) < 0) i += modulus;
# Line 313 | Line 314 | public class ArrayDeque<E> extends Abstr
314      /**
315       * Adds all of the elements in the specified collection at the end
316       * of this deque, as if by calling {@link #addLast} on each one,
317 <     * in the order that they are returned by the collection's
317 <     * iterator.
317 >     * in the order that they are returned by the collection's iterator.
318       *
319       * @param c the elements to be inserted into this deque
320       * @return {@code true} if this deque changed as a result of the call
# Line 325 | Line 325 | public class ArrayDeque<E> extends Abstr
325          final int s, needed;
326          if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
327              grow(needed);
328 <        c.forEach(this::addLast);
328 >        copyElements(c);
329          // checkInvariants();
330          return size() > s;
331      }
332  
333 +    private void copyElements(Collection<? extends E> c) {
334 +        c.forEach(this::addLast);
335 +    }
336 +
337      /**
338       * Inserts the specified element at the front of this deque.
339       *
# Line 520 | Line 524 | public class ArrayDeque<E> extends Abstr
524      /**
525       * Retrieves and removes the head of the queue represented by this deque.
526       *
527 <     * This method differs from {@link #poll poll} only in that it throws an
528 <     * exception if this deque is empty.
527 >     * This method differs from {@link #poll() poll()} only in that it
528 >     * throws an exception if this deque is empty.
529       *
530       * <p>This method is equivalent to {@link #removeFirst}.
531       *
# Line 841 | Line 845 | public class ArrayDeque<E> extends Abstr
845              final int i, n;
846              return ((n = sub(getFence(), i = cursor, es.length) >> 1) <= 0)
847                  ? null
848 <                : new DeqSpliterator(i, cursor = add(i, n, es.length));
848 >                : new DeqSpliterator(i, cursor = inc(i, n, es.length));
849          }
850  
851          public void forEachRemaining(Consumer<? super E> action) {
# Line 1236 | Line 1240 | public class ArrayDeque<E> extends Abstr
1240  
1241          // Read in size and allocate array
1242          int size = s.readInt();
1243 +        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size + 1);
1244          elements = new Object[size + 1];
1245          this.tail = size;
1246  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines