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.31 by jsr166, Tue Mar 15 19:47:02 2011 UTC vs.
Revision 1.39 by jsr166, Tue Feb 21 01:54:03 2012 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 import java.io.*;
7  
8   /**
9   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 52 | Line 51 | import java.io.*;
51   * @param <E> the type of elements held in this collection
52   */
53   public class ArrayDeque<E> extends AbstractCollection<E>
54 <                           implements Deque<E>, Cloneable, Serializable
54 >                           implements Deque<E>, Cloneable, java.io.Serializable
55   {
56      /**
57       * The array in which the elements of the deque are stored.
# Line 64 | Line 63 | public class ArrayDeque<E> extends Abstr
63       * other.  We also guarantee that all array cells not holding
64       * deque elements are always null.
65       */
66 <    private transient E[] elements;
66 >    private transient Object[] elements;
67  
68      /**
69       * The index of the element at the head of the deque (which is the
# Line 88 | Line 87 | public class ArrayDeque<E> extends Abstr
87      // ******  Array allocation and resizing utilities ******
88  
89      /**
90 <     * Allocate empty array to hold the given number of elements.
90 >     * Allocates empty array to hold the given number of elements.
91       *
92       * @param numElements  the number of elements to hold
93       */
# Line 108 | Line 107 | public class ArrayDeque<E> extends Abstr
107              if (initialCapacity < 0)   // Too many elements, must back off
108                  initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
109          }
110 <        elements = (E[]) new Object[initialCapacity];
110 >        elements = new Object[initialCapacity];
111      }
112  
113      /**
114 <     * Double the capacity of this deque.  Call only when full, i.e.,
114 >     * Doubles the capacity of this deque.  Call only when full, i.e.,
115       * when head and tail have wrapped around to become equal.
116       */
117      private void doubleCapacity() {
# Line 126 | Line 125 | public class ArrayDeque<E> extends Abstr
125          Object[] a = new Object[newCapacity];
126          System.arraycopy(elements, p, a, 0, r);
127          System.arraycopy(elements, 0, a, r, p);
128 <        elements = (E[])a;
128 >        elements = a;
129          head = 0;
130          tail = n;
131      }
# Line 154 | Line 153 | public class ArrayDeque<E> extends Abstr
153       * sufficient to hold 16 elements.
154       */
155      public ArrayDeque() {
156 <        elements = (E[]) new Object[16];
156 >        elements = new Object[16];
157      }
158  
159      /**
# Line 262 | Line 261 | public class ArrayDeque<E> extends Abstr
261  
262      public E pollFirst() {
263          int h = head;
264 <        E result = elements[h]; // Element is null if deque empty
264 >        @SuppressWarnings("unchecked")
265 >        E result = (E) elements[h];
266 >        // Element is null if deque empty
267          if (result == null)
268              return null;
269          elements[h] = null;     // Must null out slot
# Line 272 | Line 273 | public class ArrayDeque<E> extends Abstr
273  
274      public E pollLast() {
275          int t = (tail - 1) & (elements.length - 1);
276 <        E result = elements[t];
276 >        @SuppressWarnings("unchecked")
277 >        E result = (E) elements[t];
278          if (result == null)
279              return null;
280          elements[t] = null;
# Line 284 | Line 286 | public class ArrayDeque<E> extends Abstr
286       * @throws NoSuchElementException {@inheritDoc}
287       */
288      public E getFirst() {
289 <        E x = elements[head];
290 <        if (x == null)
289 >        @SuppressWarnings("unchecked")
290 >        E result = (E) elements[head];
291 >        if (result == null)
292              throw new NoSuchElementException();
293 <        return x;
293 >        return result;
294      }
295  
296      /**
297       * @throws NoSuchElementException {@inheritDoc}
298       */
299      public E getLast() {
300 <        E x = elements[(tail - 1) & (elements.length - 1)];
301 <        if (x == null)
300 >        @SuppressWarnings("unchecked")
301 >        E result = (E) elements[(tail - 1) & (elements.length - 1)];
302 >        if (result == null)
303              throw new NoSuchElementException();
304 <        return x;
304 >        return result;
305      }
306  
307 +    @SuppressWarnings("unchecked")
308      public E peekFirst() {
309 <        return elements[head]; // elements[head] is null if deque empty
309 >        // elements[head] is null if deque empty
310 >        return (E) elements[head];
311      }
312  
313 +    @SuppressWarnings("unchecked")
314      public E peekLast() {
315 <        return elements[(tail - 1) & (elements.length - 1)];
315 >        return (E) elements[(tail - 1) & (elements.length - 1)];
316      }
317  
318      /**
# Line 325 | Line 332 | public class ArrayDeque<E> extends Abstr
332              return false;
333          int mask = elements.length - 1;
334          int i = head;
335 <        E x;
335 >        Object x;
336          while ( (x = elements[i]) != null) {
337              if (o.equals(x)) {
338                  delete(i);
# Line 353 | Line 360 | public class ArrayDeque<E> extends Abstr
360              return false;
361          int mask = elements.length - 1;
362          int i = (tail - 1) & mask;
363 <        E x;
363 >        Object x;
364          while ( (x = elements[i]) != null) {
365              if (o.equals(x)) {
366                  delete(i);
# Line 498 | Line 505 | public class ArrayDeque<E> extends Abstr
505       */
506      private boolean delete(int i) {
507          checkInvariants();
508 <        final E[] elements = this.elements;
508 >        final Object[] elements = this.elements;
509          final int mask = elements.length - 1;
510          final int h = head;
511          final int t = tail;
# Line 596 | Line 603 | public class ArrayDeque<E> extends Abstr
603          public E next() {
604              if (cursor == fence)
605                  throw new NoSuchElementException();
606 <            E result = elements[cursor];
606 >            @SuppressWarnings("unchecked")
607 >            E result = (E) elements[cursor];
608              // This check doesn't catch all possible comodifications,
609              // but does catch the ones that corrupt traversal
610              if (tail != fence || result == null)
# Line 635 | Line 643 | public class ArrayDeque<E> extends Abstr
643              if (cursor == fence)
644                  throw new NoSuchElementException();
645              cursor = (cursor - 1) & (elements.length - 1);
646 <            E result = elements[cursor];
646 >            @SuppressWarnings("unchecked")
647 >            E result = (E) elements[cursor];
648              if (head != fence || result == null)
649                  throw new ConcurrentModificationException();
650              lastRet = cursor;
# Line 666 | Line 675 | public class ArrayDeque<E> extends Abstr
675              return false;
676          int mask = elements.length - 1;
677          int i = head;
678 <        E x;
678 >        Object x;
679          while ( (x = elements[i]) != null) {
680              if (o.equals(x))
681                  return true;
# Line 749 | Line 758 | public class ArrayDeque<E> extends Abstr
758       * The following code can be used to dump the deque into a newly
759       * allocated array of <tt>String</tt>:
760       *
761 <     * <pre>
753 <     *     String[] y = x.toArray(new String[0]);</pre>
761 >     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
762       *
763       * Note that <tt>toArray(new Object[0])</tt> is identical in function to
764       * <tt>toArray()</tt>.
# Line 764 | Line 772 | public class ArrayDeque<E> extends Abstr
772       *         this deque
773       * @throws NullPointerException if the specified array is null
774       */
775 +    @SuppressWarnings("unchecked")
776      public <T> T[] toArray(T[] a) {
777          int size = size();
778          if (a.length < size)
# Line 784 | Line 793 | public class ArrayDeque<E> extends Abstr
793       */
794      public ArrayDeque<E> clone() {
795          try {
796 +            @SuppressWarnings("unchecked")
797              ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
798              result.elements = Arrays.copyOf(elements, elements.length);
799              return result;
790
800          } catch (CloneNotSupportedException e) {
801              throw new AssertionError();
802          }
803      }
804  
796    /**
797     * Appease the serialization gods.
798     */
805      private static final long serialVersionUID = 2340985798034038923L;
806  
807      /**
808 <     * Serialize this deque.
808 >     * Saves this deque to a stream (that is, serializes it).
809       *
810       * @serialData The current size (<tt>int</tt>) of the deque,
811       * followed by all of its elements (each an object reference) in
812       * first-to-last order.
813       */
814 <    private void writeObject(ObjectOutputStream s) throws IOException {
814 >    private void writeObject(java.io.ObjectOutputStream s)
815 >            throws java.io.IOException {
816          s.defaultWriteObject();
817  
818          // Write out size
# Line 818 | Line 825 | public class ArrayDeque<E> extends Abstr
825      }
826  
827      /**
828 <     * Deserialize this deque.
828 >     * Reconstitutes this deque from a stream (that is, deserializes it).
829       */
830 <    private void readObject(ObjectInputStream s)
831 <            throws IOException, ClassNotFoundException {
830 >    private void readObject(java.io.ObjectInputStream s)
831 >            throws java.io.IOException, ClassNotFoundException {
832          s.defaultReadObject();
833  
834          // Read in size and allocate array
# Line 832 | Line 839 | public class ArrayDeque<E> extends Abstr
839  
840          // Read in all elements in the proper order.
841          for (int i = 0; i < size; i++)
842 <            elements[i] = (E)s.readObject();
842 >            elements[i] = s.readObject();
843      }
844   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines