< prev index next >

src/java.base/share/classes/java/util/AbstractList.java

Print this page
rev 54827 : 6394757: AbstractSet.removeAll semantics are surprisingly dependent on relative sizes
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 495      */
 496     public List<E> subList(int fromIndex, int toIndex) {
 497         subListRangeCheck(fromIndex, toIndex, size());
 498         return (this instanceof RandomAccess ?
 499                 new RandomAccessSubList<>(this, fromIndex, toIndex) :
 500                 new SubList<>(this, fromIndex, toIndex));
 501     }
 502 
 503     static void subListRangeCheck(int fromIndex, int toIndex, int size) {
 504         if (fromIndex < 0)
 505             throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
 506         if (toIndex > size)
 507             throw new IndexOutOfBoundsException("toIndex = " + toIndex);
 508         if (fromIndex > toIndex)
 509             throw new IllegalArgumentException("fromIndex(" + fromIndex +
 510                                                ") > toIndex(" + toIndex + ")");
 511     }
 512 
 513     // Comparison and hashing
 514 


 515     /**
 516      * Compares the specified object with this list for equality.  Returns
 517      * {@code true} if and only if the specified object is also a list, both
 518      * lists have the same size, and all corresponding pairs of elements in
 519      * the two lists are <i>equal</i>.  (Two elements {@code e1} and
 520      * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
 521      * e1.equals(e2))}.)  In other words, two lists are defined to be
 522      * equal if they contain the same elements in the same order.


 523      *
 524      * @implSpec
 525      * This implementation first checks if the specified object is this
 526      * list. If so, it returns {@code true}; if not, it checks if the
 527      * specified object is a list. If not, it returns {@code false}; if so,
 528      * it iterates over both lists, comparing corresponding pairs of elements.
 529      * If any comparison returns {@code false}, this method returns
 530      * {@code false}.  If either iterator runs out of elements before the
 531      * other it returns {@code false} (as the lists are of unequal length);
 532      * otherwise it returns {@code true} when the iterations complete.
 533      *
 534      * @param o the object to be compared for equality with this list
 535      * @return {@code true} if the specified object is equal to this list
 536      */
 537     public boolean equals(Object o) {
 538         if (o == this)
 539             return true;
 540         if (!(o instanceof List))
 541             return false;
 542 


   1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 495      */
 496     public List<E> subList(int fromIndex, int toIndex) {
 497         subListRangeCheck(fromIndex, toIndex, size());
 498         return (this instanceof RandomAccess ?
 499                 new RandomAccessSubList<>(this, fromIndex, toIndex) :
 500                 new SubList<>(this, fromIndex, toIndex));
 501     }
 502 
 503     static void subListRangeCheck(int fromIndex, int toIndex, int size) {
 504         if (fromIndex < 0)
 505             throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
 506         if (toIndex > size)
 507             throw new IndexOutOfBoundsException("toIndex = " + toIndex);
 508         if (fromIndex > toIndex)
 509             throw new IllegalArgumentException("fromIndex(" + fromIndex +
 510                                                ") > toIndex(" + toIndex + ")");
 511     }
 512 
 513     // Comparison and hashing
 514 
 515     // FIXME: the first paragraph below is copied from Set.equals, because
 516     // {@inheritDoc} inherits text from superclasses, not interfaces (see JDK-6376959).
 517     /**
 518      * Compares the specified object with this list for equality.  Returns
 519      * {@code true} if and only if the specified object is also a list, both
 520      * lists have the same size, and all corresponding pairs of elements in
 521      * the two lists are <i>equal</i>.  (Two elements {@code e1} and
 522      * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
 523      * e1.equals(e2))}.)  In other words, two lists are defined to be
 524      * equal if they contain the same elements in the same order.  This
 525      * definition ensures that the {@code equals} method works properly across
 526      * different implementations of the {@code List} interface.
 527      *
 528      * @implSpec
 529      * This implementation first checks if the specified object is this
 530      * list. If so, it returns {@code true}; if not, it checks if the
 531      * specified object is a list. If not, it returns {@code false}; if so,
 532      * it iterates over both lists, comparing corresponding pairs of elements.
 533      * If any comparison returns {@code false}, this method returns
 534      * {@code false}.  If either iterator runs out of elements before the
 535      * other it returns {@code false} (as the lists are of unequal length);
 536      * otherwise it returns {@code true} when the iterations complete.
 537      *
 538      * @param o the object to be compared for equality with this list
 539      * @return {@code true} if the specified object is equal to this list
 540      */
 541     public boolean equals(Object o) {
 542         if (o == this)
 543             return true;
 544         if (!(o instanceof List))
 545             return false;
 546 


< prev index next >