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.83 by jsr166, Sun Oct 23 00:28:41 2016 UTC vs.
Revision 1.86 by jsr166, Sun Oct 23 22:30:20 2016 UTC

# Line 187 | Line 187 | public class ArrayDeque<E> extends Abstr
187          Object[] elements = c.toArray();
188          // defend against c.toArray (incorrectly) not returning Object[]
189          // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
190 +        size = elements.length;
191          if (elements.getClass() != Object[].class)
192              elements = Arrays.copyOf(elements, size, Object[].class);
193          for (Object obj : elements)
194              Objects.requireNonNull(obj);
194        size = elements.length;
195          this.elements = elements;
196      }
197  
# Line 365 | Line 365 | public class ArrayDeque<E> extends Abstr
365       */
366      public E removeFirst() {
367          // checkInvariants();
368 <        E x = pollFirst();
369 <        if (x == null)
368 >        E e = pollFirst();
369 >        if (e == null)
370              throw new NoSuchElementException();
371 <        return x;
371 >        return e;
372      }
373  
374      /**
# Line 376 | Line 376 | public class ArrayDeque<E> extends Abstr
376       */
377      public E removeLast() {
378          // checkInvariants();
379 <        E x = pollLast();
380 <        if (x == null)
379 >        E e = pollLast();
380 >        if (e == null)
381              throw new NoSuchElementException();
382 <        return x;
382 >        return e;
383      }
384  
385      public E pollFirst() {
# Line 983 | Line 983 | public class ArrayDeque<E> extends Abstr
983       */
984      public void clear() {
985          final Object[] elements = this.elements;
986 <        final int capacity = elements.length;
987 <        final int h = this.head;
988 <        final int s = size;
989 <        if (capacity - h >= s)
990 <            Arrays.fill(elements, h, h + s, null);
986 >        final int capacity = elements.length, tail = head + size;
987 >        if (capacity - tail >= 0)
988 >            Arrays.fill(elements, head, tail, null);
989          else {
990 <            Arrays.fill(elements, h, capacity, null);
991 <            Arrays.fill(elements, 0, s - capacity + h, null);
990 >            Arrays.fill(elements, head, capacity, null);
991 >            Arrays.fill(elements, 0, tail - capacity, null);
992          }
993          size = head = 0;
994          // checkInvariants();
# Line 1010 | Line 1008 | public class ArrayDeque<E> extends Abstr
1008       * @return an array containing all of the elements in this deque
1009       */
1010      public Object[] toArray() {
1011 <        final int head = this.head;
1012 <        final int firstLeg;
1013 <        Object[] a = Arrays.copyOfRange(elements, head, head + size);
1014 <        if ((firstLeg = elements.length - head) < size)
1015 <            System.arraycopy(elements, 0, a, firstLeg, size - firstLeg);
1011 >        return toArray(Object[].class);
1012 >    }
1013 >
1014 >    private <T> T[] toArray(Class<T[]> klazz) {
1015 >        final Object[] elements = this.elements;
1016 >        final int capacity = elements.length;
1017 >        final int head = this.head, tail = head + size;
1018 >        final T[] a;
1019 >        if (tail >= 0) {
1020 >            a = Arrays.copyOfRange(elements, head, tail, klazz);
1021 >        } else {
1022 >            // integer overflow!
1023 >            a = Arrays.copyOfRange(elements, 0, size, klazz);
1024 >            System.arraycopy(elements, head, a, 0, capacity - head);
1025 >        }
1026 >        if (tail - capacity > 0)
1027 >            System.arraycopy(elements, 0, a, capacity - head, tail - capacity);
1028          return a;
1029      }
1030  
# Line 1056 | Line 1066 | public class ArrayDeque<E> extends Abstr
1066       */
1067      @SuppressWarnings("unchecked")
1068      public <T> T[] toArray(T[] a) {
1069 +        final int size = this.size;
1070 +        if (size > a.length)
1071 +            return toArray((Class<T[]>) a.getClass());
1072          final Object[] elements = this.elements;
1073 <        final int head = this.head;
1074 <        final int firstLeg;
1075 <        boolean wrap = (firstLeg = elements.length - head) < size;
1076 <        if (size > a.length) {
1077 <            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
1078 <                                         a.getClass());
1079 <        } else {
1067 <            System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size);
1068 <            if (size < a.length)
1069 <                a[size] = null;
1073 >        final int capacity = elements.length;
1074 >        final int head = this.head, tail = head + size;
1075 >        if (capacity - tail >= 0)
1076 >            System.arraycopy(elements, head, a, 0, size);
1077 >        else {
1078 >            System.arraycopy(elements, head, a, 0, capacity - head);
1079 >            System.arraycopy(elements, 0, a, capacity - head, tail - capacity);
1080          }
1081 <        if (wrap)
1082 <            System.arraycopy(elements, 0, a, firstLeg, size - firstLeg);
1081 >        if (size < a.length)
1082 >            a[size] = null;
1083          return a;
1084      }
1085  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines