Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8270057

Use Objects.checkFromToIndex for j.u.c.CopyOnWriteArrayList

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None

      After JDK-8265518, it's possible to replace all variants of checkIndex by Objects.checkIndex/Objects.checkFromToIndex/Objects.checkFromIndexSize in the whole JDK codebase.

      As Mandy suggested, file an issue for changes involving JSR 166:

      diff --git a/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java b/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
      index ee8e7b81728f..6ae552944015 100644
      --- a/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
      +++ b/src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java
      @@ -561,8 +561,7 @@ void removeRange(int fromIndex, int toIndex) {
                   Object[] es = getArray();
                   int len = es.length;
       
      - if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
      - throw new IndexOutOfBoundsException();
      + Objects.checkFromToIndex(fromIndex, toIndex, len);
                   int newlen = len - (toIndex - fromIndex);
                   int numMoved = len - toIndex;
                   if (numMoved == 0)
      @@ -1173,8 +1172,7 @@ public void forEachRemaining(Consumer<? super E> action) {
                   Object[] es = getArray();
                   int len = es.length;
                   int size = toIndex - fromIndex;
      - if (fromIndex < 0 || toIndex > len || size < 0)
      - throw new IndexOutOfBoundsException();
      + Objects.checkFromToIndex(fromIndex, toIndex, len);
                   return new COWSubList(es, fromIndex, size);
               }
           }
      @@ -1457,8 +1455,7 @@ public boolean remove(Object o) {
               public List<E> subList(int fromIndex, int toIndex) {
                   synchronized (lock) {
                       checkForComodification();
      - if (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
      - throw new IndexOutOfBoundsException();
      + Objects.checkFromToIndex(fromIndex, toIndex, size);
                       return new COWSubList(expectedArray, fromIndex + offset, toIndex - fromIndex);
                   }
               }

            Unassigned Unassigned
            yyang Yi Yang
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: