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.88 by jsr166, Mon Oct 24 16:01:25 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 234 | Line 234 | public class ArrayDeque<E> extends Abstr
234       * Returns element at array index i.
235       */
236      @SuppressWarnings("unchecked")
237 <    final E elementAt(int i) {
237 >    private E elementAt(int i) {
238          return (E) elements[i];
239      }
240  
# Line 317 | Line 317 | public class ArrayDeque<E> extends Abstr
317       * @throws NullPointerException if the specified collection or any
318       *         of its elements are null
319       */
320    @Override
320      public boolean addAll(Collection<? extends E> c) {
321 +        final int s = size, needed = c.size() - (elements.length - s);
322 +        if (needed > 0)
323 +            grow(needed);
324 +        c.forEach((e) -> addLast(e));
325          // checkInvariants();
326 <        Object[] a, elements;
324 <        int newcomers, capacity, s = size;
325 <        if ((newcomers = (a = c.toArray()).length) == 0)
326 <            return false;
327 <        while ((capacity = (elements = this.elements).length) - s < newcomers)
328 <            grow(newcomers - (capacity - s));
329 <        int i = add(head, s, capacity);
330 <        for (Object x : a) {
331 <            Objects.requireNonNull(x);
332 <            elements[i] = x;
333 <            i = inc(i, capacity);
334 <            size++;
335 <        }
336 <        return true;
326 >        return size > s;
327      }
328  
329      /**
# Line 365 | Line 355 | public class ArrayDeque<E> extends Abstr
355       */
356      public E removeFirst() {
357          // checkInvariants();
358 <        E x = pollFirst();
359 <        if (x == null)
358 >        E e = pollFirst();
359 >        if (e == null)
360              throw new NoSuchElementException();
361 <        return x;
361 >        return e;
362      }
363  
364      /**
# Line 376 | Line 366 | public class ArrayDeque<E> extends Abstr
366       */
367      public E removeLast() {
368          // checkInvariants();
369 <        E x = pollLast();
370 <        if (x == null)
369 >        E e = pollLast();
370 >        if (e == null)
371              throw new NoSuchElementException();
372 <        return x;
372 >        return e;
373      }
374  
375      public E pollFirst() {
# Line 855 | Line 845 | public class ArrayDeque<E> extends Abstr
845          }
846      }
847  
858    @Override
848      public void forEach(Consumer<? super E> action) {
849          // checkInvariants();
850          Objects.requireNonNull(action);
# Line 885 | Line 874 | public class ArrayDeque<E> extends Abstr
874      /**
875       * @throws NullPointerException {@inheritDoc}
876       */
888    @Override
877      public boolean removeIf(Predicate<? super E> filter) {
878          Objects.requireNonNull(filter);
879          return bulkRemove(filter);
# Line 894 | Line 882 | public class ArrayDeque<E> extends Abstr
882      /**
883       * @throws NullPointerException {@inheritDoc}
884       */
897    @Override
885      public boolean removeAll(Collection<?> c) {
886          Objects.requireNonNull(c);
887          return bulkRemove(e -> c.contains(e));
# Line 903 | Line 890 | public class ArrayDeque<E> extends Abstr
890      /**
891       * @throws NullPointerException {@inheritDoc}
892       */
906    @Override
893      public boolean retainAll(Collection<?> c) {
894          Objects.requireNonNull(c);
895          return bulkRemove(e -> !c.contains(e));
# Line 983 | Line 969 | public class ArrayDeque<E> extends Abstr
969       */
970      public void clear() {
971          final Object[] elements = this.elements;
972 <        final int capacity = elements.length;
973 <        final int h = this.head;
974 <        final int s = size;
989 <        if (capacity - h >= s)
990 <            Arrays.fill(elements, h, h + s, null);
972 >        final int capacity = elements.length, tail = head + size;
973 >        if (capacity - tail >= 0)
974 >            Arrays.fill(elements, head, tail, null);
975          else {
976 <            Arrays.fill(elements, h, capacity, null);
977 <            Arrays.fill(elements, 0, s - capacity + h, null);
976 >            Arrays.fill(elements, head, capacity, null);
977 >            Arrays.fill(elements, 0, tail - capacity, null);
978          }
979          size = head = 0;
980          // checkInvariants();
# Line 1010 | Line 994 | public class ArrayDeque<E> extends Abstr
994       * @return an array containing all of the elements in this deque
995       */
996      public Object[] toArray() {
997 <        final int head = this.head;
998 <        final int firstLeg;
999 <        Object[] a = Arrays.copyOfRange(elements, head, head + size);
1000 <        if ((firstLeg = elements.length - head) < size)
1001 <            System.arraycopy(elements, 0, a, firstLeg, size - firstLeg);
997 >        return toArray(Object[].class);
998 >    }
999 >
1000 >    private <T> T[] toArray(Class<T[]> klazz) {
1001 >        final Object[] elements = this.elements;
1002 >        final int capacity = elements.length;
1003 >        final int head = this.head, tail = head + size;
1004 >        final T[] a;
1005 >        if (tail >= 0) {
1006 >            a = Arrays.copyOfRange(elements, head, tail, klazz);
1007 >        } else {
1008 >            // integer overflow!
1009 >            a = Arrays.copyOfRange(elements, 0, size, klazz);
1010 >            System.arraycopy(elements, head, a, 0, capacity - head);
1011 >        }
1012 >        if (tail - capacity > 0)
1013 >            System.arraycopy(elements, 0, a, capacity - head, tail - capacity);
1014          return a;
1015      }
1016  
# Line 1056 | Line 1052 | public class ArrayDeque<E> extends Abstr
1052       */
1053      @SuppressWarnings("unchecked")
1054      public <T> T[] toArray(T[] a) {
1055 +        final int size = this.size;
1056 +        if (size > a.length)
1057 +            return toArray((Class<T[]>) a.getClass());
1058          final Object[] elements = this.elements;
1059 <        final int head = this.head;
1060 <        final int firstLeg;
1061 <        boolean wrap = (firstLeg = elements.length - head) < size;
1062 <        if (size > a.length) {
1063 <            a = (T[]) Arrays.copyOfRange(elements, head, head + size,
1064 <                                         a.getClass());
1065 <        } else {
1067 <            System.arraycopy(elements, head, a, 0, wrap ? firstLeg : size);
1068 <            if (size < a.length)
1069 <                a[size] = null;
1059 >        final int capacity = elements.length;
1060 >        final int head = this.head, tail = head + size;
1061 >        if (capacity - tail >= 0)
1062 >            System.arraycopy(elements, head, a, 0, size);
1063 >        else {
1064 >            System.arraycopy(elements, head, a, 0, capacity - head);
1065 >            System.arraycopy(elements, 0, a, capacity - head, tail - capacity);
1066          }
1067 <        if (wrap)
1068 <            System.arraycopy(elements, 0, a, firstLeg, size - firstLeg);
1067 >        if (size < a.length)
1068 >            a[size] = null;
1069          return a;
1070      }
1071  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines