rev 17661 : 8177290: add copy factory methods for unmodifiable List, Set, Map
8184690: add Collectors for collecting into unmodifiable List, Set, and Map
Reviewed-by: XXX

   1 /*
   2  * Copyright (c) 1997, 2017, 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
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.UnaryOperator;
  29 
  30 /**
  31  * An ordered collection (also known as a <i>sequence</i>).  The user of this
  32  * interface has precise control over where in the list each element is
  33  * inserted.  The user can access elements by their integer index (position in
  34  * the list), and search for elements in the list.<p>
  35  *
  36  * Unlike sets, lists typically allow duplicate elements.  More formally,
  37  * lists typically allow pairs of elements {@code e1} and {@code e2}
  38  * such that {@code e1.equals(e2)}, and they typically allow multiple
  39  * null elements if they allow null elements at all.  It is not inconceivable
  40  * that someone might wish to implement a list that prohibits duplicates, by
  41  * throwing runtime exceptions when the user attempts to insert them, but we
  42  * expect this usage to be rare.<p>
  43  *
  44  * The {@code List} interface places additional stipulations, beyond those
  45  * specified in the {@code Collection} interface, on the contracts of the
  46  * {@code iterator}, {@code add}, {@code remove}, {@code equals}, and
  47  * {@code hashCode} methods.  Declarations for other inherited methods are
  48  * also included here for convenience.<p>
  49  *
  50  * The {@code List} interface provides four methods for positional (indexed)
  51  * access to list elements.  Lists (like Java arrays) are zero based.  Note
  52  * that these operations may execute in time proportional to the index value
  53  * for some implementations (the {@code LinkedList} class, for
  54  * example). Thus, iterating over the elements in a list is typically
  55  * preferable to indexing through it if the caller does not know the
  56  * implementation.<p>
  57  *
  58  * The {@code List} interface provides a special iterator, called a
  59  * {@code ListIterator}, that allows element insertion and replacement, and
  60  * bidirectional access in addition to the normal operations that the
  61  * {@code Iterator} interface provides.  A method is provided to obtain a
  62  * list iterator that starts at a specified position in the list.<p>
  63  *
  64  * The {@code List} interface provides two methods to search for a specified
  65  * object.  From a performance standpoint, these methods should be used with
  66  * caution.  In many implementations they will perform costly linear
  67  * searches.<p>
  68  *
  69  * The {@code List} interface provides two methods to efficiently insert and
  70  * remove multiple elements at an arbitrary point in the list.<p>
  71  *
  72  * Note: While it is permissible for lists to contain themselves as elements,
  73  * extreme caution is advised: the {@code equals} and {@code hashCode}
  74  * methods are no longer well defined on such a list.
  75  *
  76  * <p>Some list implementations have restrictions on the elements that
  77  * they may contain.  For example, some implementations prohibit null elements,
  78  * and some have restrictions on the types of their elements.  Attempting to
  79  * add an ineligible element throws an unchecked exception, typically
  80  * {@code NullPointerException} or {@code ClassCastException}.  Attempting
  81  * to query the presence of an ineligible element may throw an exception,
  82  * or it may simply return false; some implementations will exhibit the former
  83  * behavior and some will exhibit the latter.  More generally, attempting an
  84  * operation on an ineligible element whose completion would not result in
  85  * the insertion of an ineligible element into the list may throw an
  86  * exception or it may succeed, at the option of the implementation.
  87  * Such exceptions are marked as "optional" in the specification for this
  88  * interface.
  89  *
  90  * <h2><a id="immutable">Immutable List Static Factory Methods</a></h2>
  91  * <p>The {@link List#of(Object...) List.of()} static factory methods
  92  * provide a convenient way to create immutable lists. The {@code List}

  93  * instances created by these methods have the following characteristics:
  94  *
  95  * <ul>
  96  * <li>They are <em>structurally immutable</em>. Elements cannot be added, removed,
  97  * or replaced. Calling any mutator method will always cause
  98  * {@code UnsupportedOperationException} to be thrown.
  99  * However, if the contained elements are themselves mutable,
 100  * this may cause the List's contents to appear to change.
 101  * <li>They disallow {@code null} elements. Attempts to create them with
 102  * {@code null} elements result in {@code NullPointerException}.
 103  * <li>They are serializable if all elements are serializable.
 104  * <li>The order of elements in the list is the same as the order of the
 105  * provided arguments, or of the elements in the provided array.
 106  * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
 107  * Callers should make no assumptions about the identity of the returned instances.
 108  * Factories are free to create new instances or reuse existing ones. Therefore,
 109  * identity-sensitive operations on these instances (reference equality ({@code ==}),
 110  * identity hash code, and synchronization) are unreliable and should be avoided.
 111  * <li>They are serialized as specified on the
 112  * <a href="{@docRoot}/serialized-form.html#java.util.CollSer">Serialized Form</a>
 113  * page.
 114  * </ul>
 115  *
 116  * <p>This interface is a member of the
 117  * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
 118  * Java Collections Framework</a>.
 119  *
 120  * @param <E> the type of elements in this list
 121  *
 122  * @author  Josh Bloch
 123  * @author  Neal Gafter
 124  * @see Collection
 125  * @see Set
 126  * @see ArrayList
 127  * @see LinkedList
 128  * @see Vector
 129  * @see Arrays#asList(Object[])
 130  * @see Collections#nCopies(int, Object)
 131  * @see Collections#EMPTY_LIST
 132  * @see AbstractList
 133  * @see AbstractSequentialList
 134  * @since 1.2
 135  */
 136 
 137 public interface List<E> extends Collection<E> {
 138     // Query Operations
 139 
 140     /**
 141      * Returns the number of elements in this list.  If this list contains
 142      * more than {@code Integer.MAX_VALUE} elements, returns
 143      * {@code Integer.MAX_VALUE}.
 144      *
 145      * @return the number of elements in this list
 146      */
 147     int size();
 148 
 149     /**
 150      * Returns {@code true} if this list contains no elements.
 151      *
 152      * @return {@code true} if this list contains no elements
 153      */
 154     boolean isEmpty();
 155 
 156     /**
 157      * Returns {@code true} if this list contains the specified element.
 158      * More formally, returns {@code true} if and only if this list contains
 159      * at least one element {@code e} such that
 160      * {@code Objects.equals(o, e)}.
 161      *
 162      * @param o element whose presence in this list is to be tested
 163      * @return {@code true} if this list contains the specified element
 164      * @throws ClassCastException if the type of the specified element
 165      *         is incompatible with this list
 166      * (<a href="Collection.html#optional-restrictions">optional</a>)
 167      * @throws NullPointerException if the specified element is null and this
 168      *         list does not permit null elements
 169      * (<a href="Collection.html#optional-restrictions">optional</a>)
 170      */
 171     boolean contains(Object o);
 172 
 173     /**
 174      * Returns an iterator over the elements in this list in proper sequence.
 175      *
 176      * @return an iterator over the elements in this list in proper sequence
 177      */
 178     Iterator<E> iterator();
 179 
 180     /**
 181      * Returns an array containing all of the elements in this list in proper
 182      * sequence (from first to last element).
 183      *
 184      * <p>The returned array will be "safe" in that no references to it are
 185      * maintained by this list.  (In other words, this method must
 186      * allocate a new array even if this list is backed by an array).
 187      * The caller is thus free to modify the returned array.
 188      *
 189      * <p>This method acts as bridge between array-based and collection-based
 190      * APIs.
 191      *
 192      * @return an array containing all of the elements in this list in proper
 193      *         sequence
 194      * @see Arrays#asList(Object[])
 195      */
 196     Object[] toArray();
 197 
 198     /**
 199      * Returns an array containing all of the elements in this list in
 200      * proper sequence (from first to last element); the runtime type of
 201      * the returned array is that of the specified array.  If the list fits
 202      * in the specified array, it is returned therein.  Otherwise, a new
 203      * array is allocated with the runtime type of the specified array and
 204      * the size of this list.
 205      *
 206      * <p>If the list fits in the specified array with room to spare (i.e.,
 207      * the array has more elements than the list), the element in the array
 208      * immediately following the end of the list is set to {@code null}.
 209      * (This is useful in determining the length of the list <i>only</i> if
 210      * the caller knows that the list does not contain any null elements.)
 211      *
 212      * <p>Like the {@link #toArray()} method, this method acts as bridge between
 213      * array-based and collection-based APIs.  Further, this method allows
 214      * precise control over the runtime type of the output array, and may,
 215      * under certain circumstances, be used to save allocation costs.
 216      *
 217      * <p>Suppose {@code x} is a list known to contain only strings.
 218      * The following code can be used to dump the list into a newly
 219      * allocated array of {@code String}:
 220      *
 221      * <pre>{@code
 222      *     String[] y = x.toArray(new String[0]);
 223      * }</pre>
 224      *
 225      * Note that {@code toArray(new Object[0])} is identical in function to
 226      * {@code toArray()}.
 227      *
 228      * @param a the array into which the elements of this list are to
 229      *          be stored, if it is big enough; otherwise, a new array of the
 230      *          same runtime type is allocated for this purpose.
 231      * @return an array containing the elements of this list
 232      * @throws ArrayStoreException if the runtime type of the specified array
 233      *         is not a supertype of the runtime type of every element in
 234      *         this list
 235      * @throws NullPointerException if the specified array is null
 236      */
 237     <T> T[] toArray(T[] a);
 238 
 239 
 240     // Modification Operations
 241 
 242     /**
 243      * Appends the specified element to the end of this list (optional
 244      * operation).
 245      *
 246      * <p>Lists that support this operation may place limitations on what
 247      * elements may be added to this list.  In particular, some
 248      * lists will refuse to add null elements, and others will impose
 249      * restrictions on the type of elements that may be added.  List
 250      * classes should clearly specify in their documentation any restrictions
 251      * on what elements may be added.
 252      *
 253      * @param e element to be appended to this list
 254      * @return {@code true} (as specified by {@link Collection#add})
 255      * @throws UnsupportedOperationException if the {@code add} operation
 256      *         is not supported by this list
 257      * @throws ClassCastException if the class of the specified element
 258      *         prevents it from being added to this list
 259      * @throws NullPointerException if the specified element is null and this
 260      *         list does not permit null elements
 261      * @throws IllegalArgumentException if some property of this element
 262      *         prevents it from being added to this list
 263      */
 264     boolean add(E e);
 265 
 266     /**
 267      * Removes the first occurrence of the specified element from this list,
 268      * if it is present (optional operation).  If this list does not contain
 269      * the element, it is unchanged.  More formally, removes the element with
 270      * the lowest index {@code i} such that
 271      * {@code Objects.equals(o, get(i))}
 272      * (if such an element exists).  Returns {@code true} if this list
 273      * contained the specified element (or equivalently, if this list changed
 274      * as a result of the call).
 275      *
 276      * @param o element to be removed from this list, if present
 277      * @return {@code true} if this list contained the specified element
 278      * @throws ClassCastException if the type of the specified element
 279      *         is incompatible with this list
 280      * (<a href="Collection.html#optional-restrictions">optional</a>)
 281      * @throws NullPointerException if the specified element is null and this
 282      *         list does not permit null elements
 283      * (<a href="Collection.html#optional-restrictions">optional</a>)
 284      * @throws UnsupportedOperationException if the {@code remove} operation
 285      *         is not supported by this list
 286      */
 287     boolean remove(Object o);
 288 
 289 
 290     // Bulk Modification Operations
 291 
 292     /**
 293      * Returns {@code true} if this list contains all of the elements of the
 294      * specified collection.
 295      *
 296      * @param  c collection to be checked for containment in this list
 297      * @return {@code true} if this list contains all of the elements of the
 298      *         specified collection
 299      * @throws ClassCastException if the types of one or more elements
 300      *         in the specified collection are incompatible with this
 301      *         list
 302      * (<a href="Collection.html#optional-restrictions">optional</a>)
 303      * @throws NullPointerException if the specified collection contains one
 304      *         or more null elements and this list does not permit null
 305      *         elements
 306      *         (<a href="Collection.html#optional-restrictions">optional</a>),
 307      *         or if the specified collection is null
 308      * @see #contains(Object)
 309      */
 310     boolean containsAll(Collection<?> c);
 311 
 312     /**
 313      * Appends all of the elements in the specified collection to the end of
 314      * this list, in the order that they are returned by the specified
 315      * collection's iterator (optional operation).  The behavior of this
 316      * operation is undefined if the specified collection is modified while
 317      * the operation is in progress.  (Note that this will occur if the
 318      * specified collection is this list, and it's nonempty.)
 319      *
 320      * @param c collection containing elements to be added to this list
 321      * @return {@code true} if this list changed as a result of the call
 322      * @throws UnsupportedOperationException if the {@code addAll} operation
 323      *         is not supported by this list
 324      * @throws ClassCastException if the class of an element of the specified
 325      *         collection prevents it from being added to this list
 326      * @throws NullPointerException if the specified collection contains one
 327      *         or more null elements and this list does not permit null
 328      *         elements, or if the specified collection is null
 329      * @throws IllegalArgumentException if some property of an element of the
 330      *         specified collection prevents it from being added to this list
 331      * @see #add(Object)
 332      */
 333     boolean addAll(Collection<? extends E> c);
 334 
 335     /**
 336      * Inserts all of the elements in the specified collection into this
 337      * list at the specified position (optional operation).  Shifts the
 338      * element currently at that position (if any) and any subsequent
 339      * elements to the right (increases their indices).  The new elements
 340      * will appear in this list in the order that they are returned by the
 341      * specified collection's iterator.  The behavior of this operation is
 342      * undefined if the specified collection is modified while the
 343      * operation is in progress.  (Note that this will occur if the specified
 344      * collection is this list, and it's nonempty.)
 345      *
 346      * @param index index at which to insert the first element from the
 347      *              specified collection
 348      * @param c collection containing elements to be added to this list
 349      * @return {@code true} if this list changed as a result of the call
 350      * @throws UnsupportedOperationException if the {@code addAll} operation
 351      *         is not supported by this list
 352      * @throws ClassCastException if the class of an element of the specified
 353      *         collection prevents it from being added to this list
 354      * @throws NullPointerException if the specified collection contains one
 355      *         or more null elements and this list does not permit null
 356      *         elements, or if the specified collection is null
 357      * @throws IllegalArgumentException if some property of an element of the
 358      *         specified collection prevents it from being added to this list
 359      * @throws IndexOutOfBoundsException if the index is out of range
 360      *         ({@code index < 0 || index > size()})
 361      */
 362     boolean addAll(int index, Collection<? extends E> c);
 363 
 364     /**
 365      * Removes from this list all of its elements that are contained in the
 366      * specified collection (optional operation).
 367      *
 368      * @param c collection containing elements to be removed from this list
 369      * @return {@code true} if this list changed as a result of the call
 370      * @throws UnsupportedOperationException if the {@code removeAll} operation
 371      *         is not supported by this list
 372      * @throws ClassCastException if the class of an element of this list
 373      *         is incompatible with the specified collection
 374      * (<a href="Collection.html#optional-restrictions">optional</a>)
 375      * @throws NullPointerException if this list contains a null element and the
 376      *         specified collection does not permit null elements
 377      *         (<a href="Collection.html#optional-restrictions">optional</a>),
 378      *         or if the specified collection is null
 379      * @see #remove(Object)
 380      * @see #contains(Object)
 381      */
 382     boolean removeAll(Collection<?> c);
 383 
 384     /**
 385      * Retains only the elements in this list that are contained in the
 386      * specified collection (optional operation).  In other words, removes
 387      * from this list all of its elements that are not contained in the
 388      * specified collection.
 389      *
 390      * @param c collection containing elements to be retained in this list
 391      * @return {@code true} if this list changed as a result of the call
 392      * @throws UnsupportedOperationException if the {@code retainAll} operation
 393      *         is not supported by this list
 394      * @throws ClassCastException if the class of an element of this list
 395      *         is incompatible with the specified collection
 396      * (<a href="Collection.html#optional-restrictions">optional</a>)
 397      * @throws NullPointerException if this list contains a null element and the
 398      *         specified collection does not permit null elements
 399      *         (<a href="Collection.html#optional-restrictions">optional</a>),
 400      *         or if the specified collection is null
 401      * @see #remove(Object)
 402      * @see #contains(Object)
 403      */
 404     boolean retainAll(Collection<?> c);
 405 
 406     /**
 407      * Replaces each element of this list with the result of applying the
 408      * operator to that element.  Errors or runtime exceptions thrown by
 409      * the operator are relayed to the caller.
 410      *
 411      * @implSpec
 412      * The default implementation is equivalent to, for this {@code list}:
 413      * <pre>{@code
 414      *     final ListIterator<E> li = list.listIterator();
 415      *     while (li.hasNext()) {
 416      *         li.set(operator.apply(li.next()));
 417      *     }
 418      * }</pre>
 419      *
 420      * If the list's list-iterator does not support the {@code set} operation
 421      * then an {@code UnsupportedOperationException} will be thrown when
 422      * replacing the first element.
 423      *
 424      * @param operator the operator to apply to each element
 425      * @throws UnsupportedOperationException if this list is unmodifiable.
 426      *         Implementations may throw this exception if an element
 427      *         cannot be replaced or if, in general, modification is not
 428      *         supported
 429      * @throws NullPointerException if the specified operator is null or
 430      *         if the operator result is a null value and this list does
 431      *         not permit null elements
 432      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 433      * @since 1.8
 434      */
 435     default void replaceAll(UnaryOperator<E> operator) {
 436         Objects.requireNonNull(operator);
 437         final ListIterator<E> li = this.listIterator();
 438         while (li.hasNext()) {
 439             li.set(operator.apply(li.next()));
 440         }
 441     }
 442 
 443     /**
 444      * Sorts this list according to the order induced by the specified
 445      * {@link Comparator}.
 446      *
 447      * <p>All elements in this list must be <i>mutually comparable</i> using the
 448      * specified comparator (that is, {@code c.compare(e1, e2)} must not throw
 449      * a {@code ClassCastException} for any elements {@code e1} and {@code e2}
 450      * in the list).
 451      *
 452      * <p>If the specified comparator is {@code null} then all elements in this
 453      * list must implement the {@link Comparable} interface and the elements'
 454      * {@linkplain Comparable natural ordering} should be used.
 455      *
 456      * <p>This list must be modifiable, but need not be resizable.
 457      *
 458      * @implSpec
 459      * The default implementation obtains an array containing all elements in
 460      * this list, sorts the array, and iterates over this list resetting each
 461      * element from the corresponding position in the array. (This avoids the
 462      * n<sup>2</sup> log(n) performance that would result from attempting
 463      * to sort a linked list in place.)
 464      *
 465      * @implNote
 466      * This implementation is a stable, adaptive, iterative mergesort that
 467      * requires far fewer than n lg(n) comparisons when the input array is
 468      * partially sorted, while offering the performance of a traditional
 469      * mergesort when the input array is randomly ordered.  If the input array
 470      * is nearly sorted, the implementation requires approximately n
 471      * comparisons.  Temporary storage requirements vary from a small constant
 472      * for nearly sorted input arrays to n/2 object references for randomly
 473      * ordered input arrays.
 474      *
 475      * <p>The implementation takes equal advantage of ascending and
 476      * descending order in its input array, and can take advantage of
 477      * ascending and descending order in different parts of the same
 478      * input array.  It is well-suited to merging two or more sorted arrays:
 479      * simply concatenate the arrays and sort the resulting array.
 480      *
 481      * <p>The implementation was adapted from Tim Peters's list sort for Python
 482      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
 483      * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
 484      * Sorting and Information Theoretic Complexity", in Proceedings of the
 485      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
 486      * January 1993.
 487      *
 488      * @param c the {@code Comparator} used to compare list elements.
 489      *          A {@code null} value indicates that the elements'
 490      *          {@linkplain Comparable natural ordering} should be used
 491      * @throws ClassCastException if the list contains elements that are not
 492      *         <i>mutually comparable</i> using the specified comparator
 493      * @throws UnsupportedOperationException if the list's list-iterator does
 494      *         not support the {@code set} operation
 495      * @throws IllegalArgumentException
 496      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 497      *         if the comparator is found to violate the {@link Comparator}
 498      *         contract
 499      * @since 1.8
 500      */
 501     @SuppressWarnings({"unchecked", "rawtypes"})
 502     default void sort(Comparator<? super E> c) {
 503         Object[] a = this.toArray();
 504         Arrays.sort(a, (Comparator) c);
 505         ListIterator<E> i = this.listIterator();
 506         for (Object e : a) {
 507             i.next();
 508             i.set((E) e);
 509         }
 510     }
 511 
 512     /**
 513      * Removes all of the elements from this list (optional operation).
 514      * The list will be empty after this call returns.
 515      *
 516      * @throws UnsupportedOperationException if the {@code clear} operation
 517      *         is not supported by this list
 518      */
 519     void clear();
 520 
 521 
 522     // Comparison and hashing
 523 
 524     /**
 525      * Compares the specified object with this list for equality.  Returns
 526      * {@code true} if and only if the specified object is also a list, both
 527      * lists have the same size, and all corresponding pairs of elements in
 528      * the two lists are <i>equal</i>.  (Two elements {@code e1} and
 529      * {@code e2} are <i>equal</i> if {@code Objects.equals(e1, e2)}.)
 530      * In other words, two lists are defined to be
 531      * equal if they contain the same elements in the same order.  This
 532      * definition ensures that the equals method works properly across
 533      * different implementations of the {@code List} interface.
 534      *
 535      * @param o the object to be compared for equality with this list
 536      * @return {@code true} if the specified object is equal to this list
 537      */
 538     boolean equals(Object o);
 539 
 540     /**
 541      * Returns the hash code value for this list.  The hash code of a list
 542      * is defined to be the result of the following calculation:
 543      * <pre>{@code
 544      *     int hashCode = 1;
 545      *     for (E e : list)
 546      *         hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
 547      * }</pre>
 548      * This ensures that {@code list1.equals(list2)} implies that
 549      * {@code list1.hashCode()==list2.hashCode()} for any two lists,
 550      * {@code list1} and {@code list2}, as required by the general
 551      * contract of {@link Object#hashCode}.
 552      *
 553      * @return the hash code value for this list
 554      * @see Object#equals(Object)
 555      * @see #equals(Object)
 556      */
 557     int hashCode();
 558 
 559 
 560     // Positional Access Operations
 561 
 562     /**
 563      * Returns the element at the specified position in this list.
 564      *
 565      * @param index index of the element to return
 566      * @return the element at the specified position in this list
 567      * @throws IndexOutOfBoundsException if the index is out of range
 568      *         ({@code index < 0 || index >= size()})
 569      */
 570     E get(int index);
 571 
 572     /**
 573      * Replaces the element at the specified position in this list with the
 574      * specified element (optional operation).
 575      *
 576      * @param index index of the element to replace
 577      * @param element element to be stored at the specified position
 578      * @return the element previously at the specified position
 579      * @throws UnsupportedOperationException if the {@code set} operation
 580      *         is not supported by this list
 581      * @throws ClassCastException if the class of the specified element
 582      *         prevents it from being added to this list
 583      * @throws NullPointerException if the specified element is null and
 584      *         this list does not permit null elements
 585      * @throws IllegalArgumentException if some property of the specified
 586      *         element prevents it from being added to this list
 587      * @throws IndexOutOfBoundsException if the index is out of range
 588      *         ({@code index < 0 || index >= size()})
 589      */
 590     E set(int index, E element);
 591 
 592     /**
 593      * Inserts the specified element at the specified position in this list
 594      * (optional operation).  Shifts the element currently at that position
 595      * (if any) and any subsequent elements to the right (adds one to their
 596      * indices).
 597      *
 598      * @param index index at which the specified element is to be inserted
 599      * @param element element to be inserted
 600      * @throws UnsupportedOperationException if the {@code add} operation
 601      *         is not supported by this list
 602      * @throws ClassCastException if the class of the specified element
 603      *         prevents it from being added to this list
 604      * @throws NullPointerException if the specified element is null and
 605      *         this list does not permit null elements
 606      * @throws IllegalArgumentException if some property of the specified
 607      *         element prevents it from being added to this list
 608      * @throws IndexOutOfBoundsException if the index is out of range
 609      *         ({@code index < 0 || index > size()})
 610      */
 611     void add(int index, E element);
 612 
 613     /**
 614      * Removes the element at the specified position in this list (optional
 615      * operation).  Shifts any subsequent elements to the left (subtracts one
 616      * from their indices).  Returns the element that was removed from the
 617      * list.
 618      *
 619      * @param index the index of the element to be removed
 620      * @return the element previously at the specified position
 621      * @throws UnsupportedOperationException if the {@code remove} operation
 622      *         is not supported by this list
 623      * @throws IndexOutOfBoundsException if the index is out of range
 624      *         ({@code index < 0 || index >= size()})
 625      */
 626     E remove(int index);
 627 
 628 
 629     // Search Operations
 630 
 631     /**
 632      * Returns the index of the first occurrence of the specified element
 633      * in this list, or -1 if this list does not contain the element.
 634      * More formally, returns the lowest index {@code i} such that
 635      * {@code Objects.equals(o, get(i))},
 636      * or -1 if there is no such index.
 637      *
 638      * @param o element to search for
 639      * @return the index of the first occurrence of the specified element in
 640      *         this list, or -1 if this list does not contain the element
 641      * @throws ClassCastException if the type of the specified element
 642      *         is incompatible with this list
 643      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 644      * @throws NullPointerException if the specified element is null and this
 645      *         list does not permit null elements
 646      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 647      */
 648     int indexOf(Object o);
 649 
 650     /**
 651      * Returns the index of the last occurrence of the specified element
 652      * in this list, or -1 if this list does not contain the element.
 653      * More formally, returns the highest index {@code i} such that
 654      * {@code Objects.equals(o, get(i))},
 655      * or -1 if there is no such index.
 656      *
 657      * @param o element to search for
 658      * @return the index of the last occurrence of the specified element in
 659      *         this list, or -1 if this list does not contain the element
 660      * @throws ClassCastException if the type of the specified element
 661      *         is incompatible with this list
 662      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 663      * @throws NullPointerException if the specified element is null and this
 664      *         list does not permit null elements
 665      *         (<a href="Collection.html#optional-restrictions">optional</a>)
 666      */
 667     int lastIndexOf(Object o);
 668 
 669 
 670     // List Iterators
 671 
 672     /**
 673      * Returns a list iterator over the elements in this list (in proper
 674      * sequence).
 675      *
 676      * @return a list iterator over the elements in this list (in proper
 677      *         sequence)
 678      */
 679     ListIterator<E> listIterator();
 680 
 681     /**
 682      * Returns a list iterator over the elements in this list (in proper
 683      * sequence), starting at the specified position in the list.
 684      * The specified index indicates the first element that would be
 685      * returned by an initial call to {@link ListIterator#next next}.
 686      * An initial call to {@link ListIterator#previous previous} would
 687      * return the element with the specified index minus one.
 688      *
 689      * @param index index of the first element to be returned from the
 690      *        list iterator (by a call to {@link ListIterator#next next})
 691      * @return a list iterator over the elements in this list (in proper
 692      *         sequence), starting at the specified position in the list
 693      * @throws IndexOutOfBoundsException if the index is out of range
 694      *         ({@code index < 0 || index > size()})
 695      */
 696     ListIterator<E> listIterator(int index);
 697 
 698     // View
 699 
 700     /**
 701      * Returns a view of the portion of this list between the specified
 702      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.  (If
 703      * {@code fromIndex} and {@code toIndex} are equal, the returned list is
 704      * empty.)  The returned list is backed by this list, so non-structural
 705      * changes in the returned list are reflected in this list, and vice-versa.
 706      * The returned list supports all of the optional list operations supported
 707      * by this list.<p>
 708      *
 709      * This method eliminates the need for explicit range operations (of
 710      * the sort that commonly exist for arrays).  Any operation that expects
 711      * a list can be used as a range operation by passing a subList view
 712      * instead of a whole list.  For example, the following idiom
 713      * removes a range of elements from a list:
 714      * <pre>{@code
 715      *      list.subList(from, to).clear();
 716      * }</pre>
 717      * Similar idioms may be constructed for {@code indexOf} and
 718      * {@code lastIndexOf}, and all of the algorithms in the
 719      * {@code Collections} class can be applied to a subList.<p>
 720      *
 721      * The semantics of the list returned by this method become undefined if
 722      * the backing list (i.e., this list) is <i>structurally modified</i> in
 723      * any way other than via the returned list.  (Structural modifications are
 724      * those that change the size of this list, or otherwise perturb it in such
 725      * a fashion that iterations in progress may yield incorrect results.)
 726      *
 727      * @param fromIndex low endpoint (inclusive) of the subList
 728      * @param toIndex high endpoint (exclusive) of the subList
 729      * @return a view of the specified range within this list
 730      * @throws IndexOutOfBoundsException for an illegal endpoint index value
 731      *         ({@code fromIndex < 0 || toIndex > size ||
 732      *         fromIndex > toIndex})
 733      */
 734     List<E> subList(int fromIndex, int toIndex);
 735 
 736     /**
 737      * Creates a {@link Spliterator} over the elements in this list.
 738      *
 739      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
 740      * {@link Spliterator#ORDERED}.  Implementations should document the
 741      * reporting of additional characteristic values.
 742      *
 743      * @implSpec
 744      * The default implementation creates a
 745      * <em><a href="Spliterator.html#binding">late-binding</a></em>
 746      * spliterator as follows:
 747      * <ul>
 748      * <li>If the list is an instance of {@link RandomAccess} then the default
 749      *     implementation creates a spliterator that traverses elements by
 750      *     invoking the method {@link List#get}.  If such invocation results or
 751      *     would result in an {@code IndexOutOfBoundsException} then the
 752      *     spliterator will <em>fail-fast</em> and throw a
 753      *     {@code ConcurrentModificationException}.
 754      *     If the list is also an instance of {@link AbstractList} then the
 755      *     spliterator will use the list's {@link AbstractList#modCount modCount}
 756      *     field to provide additional <em>fail-fast</em> behavior.
 757      * <li>Otherwise, the default implementation creates a spliterator from the
 758      *     list's {@code Iterator}.  The spliterator inherits the
 759      *     <em>fail-fast</em> of the list's iterator.
 760      * </ul>
 761      *
 762      * @implNote
 763      * The created {@code Spliterator} additionally reports
 764      * {@link Spliterator#SUBSIZED}.
 765      *
 766      * @return a {@code Spliterator} over the elements in this list
 767      * @since 1.8
 768      */
 769     @Override
 770     default Spliterator<E> spliterator() {
 771         if (this instanceof RandomAccess) {
 772             return new AbstractList.RandomAccessSpliterator<>(this);
 773         } else {
 774             return Spliterators.spliterator(this, Spliterator.ORDERED);
 775         }
 776     }
 777 
 778     /**
 779      * Returns an immutable list containing zero elements.
 780      *
 781      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 782      *
 783      * @param <E> the {@code List}'s element type
 784      * @return an empty {@code List}
 785      *
 786      * @since 9
 787      */
 788     static <E> List<E> of() {
 789         return ImmutableCollections.List0.instance();
 790     }
 791 
 792     /**
 793      * Returns an immutable list containing one element.
 794      *
 795      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 796      *
 797      * @param <E> the {@code List}'s element type
 798      * @param e1 the single element
 799      * @return a {@code List} containing the specified element
 800      * @throws NullPointerException if the element is {@code null}
 801      *
 802      * @since 9
 803      */
 804     static <E> List<E> of(E e1) {
 805         return new ImmutableCollections.List1<>(e1);
 806     }
 807 
 808     /**
 809      * Returns an immutable list containing two elements.
 810      *
 811      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 812      *
 813      * @param <E> the {@code List}'s element type
 814      * @param e1 the first element
 815      * @param e2 the second element
 816      * @return a {@code List} containing the specified elements
 817      * @throws NullPointerException if an element is {@code null}
 818      *
 819      * @since 9
 820      */
 821     static <E> List<E> of(E e1, E e2) {
 822         return new ImmutableCollections.List2<>(e1, e2);
 823     }
 824 
 825     /**
 826      * Returns an immutable list containing three elements.
 827      *
 828      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 829      *
 830      * @param <E> the {@code List}'s element type
 831      * @param e1 the first element
 832      * @param e2 the second element
 833      * @param e3 the third element
 834      * @return a {@code List} containing the specified elements
 835      * @throws NullPointerException if an element is {@code null}
 836      *
 837      * @since 9
 838      */
 839     static <E> List<E> of(E e1, E e2, E e3) {
 840         return new ImmutableCollections.ListN<>(e1, e2, e3);
 841     }
 842 
 843     /**
 844      * Returns an immutable list containing four elements.
 845      *
 846      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 847      *
 848      * @param <E> the {@code List}'s element type
 849      * @param e1 the first element
 850      * @param e2 the second element
 851      * @param e3 the third element
 852      * @param e4 the fourth element
 853      * @return a {@code List} containing the specified elements
 854      * @throws NullPointerException if an element is {@code null}
 855      *
 856      * @since 9
 857      */
 858     static <E> List<E> of(E e1, E e2, E e3, E e4) {
 859         return new ImmutableCollections.ListN<>(e1, e2, e3, e4);
 860     }
 861 
 862     /**
 863      * Returns an immutable list containing five elements.
 864      *
 865      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 866      *
 867      * @param <E> the {@code List}'s element type
 868      * @param e1 the first element
 869      * @param e2 the second element
 870      * @param e3 the third element
 871      * @param e4 the fourth element
 872      * @param e5 the fifth element
 873      * @return a {@code List} containing the specified elements
 874      * @throws NullPointerException if an element is {@code null}
 875      *
 876      * @since 9
 877      */
 878     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
 879         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5);
 880     }
 881 
 882     /**
 883      * Returns an immutable list containing six elements.
 884      *
 885      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 886      *
 887      * @param <E> the {@code List}'s element type
 888      * @param e1 the first element
 889      * @param e2 the second element
 890      * @param e3 the third element
 891      * @param e4 the fourth element
 892      * @param e5 the fifth element
 893      * @param e6 the sixth element
 894      * @return a {@code List} containing the specified elements
 895      * @throws NullPointerException if an element is {@code null}
 896      *
 897      * @since 9
 898      */
 899     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
 900         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 901                                                 e6);
 902     }
 903 
 904     /**
 905      * Returns an immutable list containing seven elements.
 906      *
 907      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 908      *
 909      * @param <E> the {@code List}'s element type
 910      * @param e1 the first element
 911      * @param e2 the second element
 912      * @param e3 the third element
 913      * @param e4 the fourth element
 914      * @param e5 the fifth element
 915      * @param e6 the sixth element
 916      * @param e7 the seventh element
 917      * @return a {@code List} containing the specified elements
 918      * @throws NullPointerException if an element is {@code null}
 919      *
 920      * @since 9
 921      */
 922     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
 923         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 924                                                 e6, e7);
 925     }
 926 
 927     /**
 928      * Returns an immutable list containing eight elements.
 929      *
 930      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 931      *
 932      * @param <E> the {@code List}'s element type
 933      * @param e1 the first element
 934      * @param e2 the second element
 935      * @param e3 the third element
 936      * @param e4 the fourth element
 937      * @param e5 the fifth element
 938      * @param e6 the sixth element
 939      * @param e7 the seventh element
 940      * @param e8 the eighth element
 941      * @return a {@code List} containing the specified elements
 942      * @throws NullPointerException if an element is {@code null}
 943      *
 944      * @since 9
 945      */
 946     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
 947         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 948                                                 e6, e7, e8);
 949     }
 950 
 951     /**
 952      * Returns an immutable list containing nine elements.
 953      *
 954      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 955      *
 956      * @param <E> the {@code List}'s element type
 957      * @param e1 the first element
 958      * @param e2 the second element
 959      * @param e3 the third element
 960      * @param e4 the fourth element
 961      * @param e5 the fifth element
 962      * @param e6 the sixth element
 963      * @param e7 the seventh element
 964      * @param e8 the eighth element
 965      * @param e9 the ninth element
 966      * @return a {@code List} containing the specified elements
 967      * @throws NullPointerException if an element is {@code null}
 968      *
 969      * @since 9
 970      */
 971     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
 972         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 973                                                 e6, e7, e8, e9);
 974     }
 975 
 976     /**
 977      * Returns an immutable list containing ten elements.
 978      *
 979      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
 980      *
 981      * @param <E> the {@code List}'s element type
 982      * @param e1 the first element
 983      * @param e2 the second element
 984      * @param e3 the third element
 985      * @param e4 the fourth element
 986      * @param e5 the fifth element
 987      * @param e6 the sixth element
 988      * @param e7 the seventh element
 989      * @param e8 the eighth element
 990      * @param e9 the ninth element
 991      * @param e10 the tenth element
 992      * @return a {@code List} containing the specified elements
 993      * @throws NullPointerException if an element is {@code null}
 994      *
 995      * @since 9
 996      */
 997     static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
 998         return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,
 999                                                 e6, e7, e8, e9, e10);
1000     }
1001 
1002     /**
1003      * Returns an immutable list containing an arbitrary number of elements.
1004      * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
1005      *
1006      * @apiNote
1007      * This method also accepts a single array as an argument. The element type of
1008      * the resulting list will be the component type of the array, and the size of
1009      * the list will be equal to the length of the array. To create a list with
1010      * a single element that is an array, do the following:
1011      *
1012      * <pre>{@code
1013      *     String[] array = ... ;
1014      *     List<String[]> list = List.<String[]>of(array);
1015      * }</pre>
1016      *
1017      * This will cause the {@link List#of(Object) List.of(E)} method
1018      * to be invoked instead.
1019      *
1020      * @param <E> the {@code List}'s element type
1021      * @param elements the elements to be contained in the list
1022      * @return a {@code List} containing the specified elements
1023      * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1024      *
1025      * @since 9
1026      */
1027     @SafeVarargs
1028     @SuppressWarnings("varargs")
1029     static <E> List<E> of(E... elements) {
1030         switch (elements.length) { // implicit null check of elements
1031             case 0:
1032                 return ImmutableCollections.List0.instance();
1033             case 1:
1034                 return new ImmutableCollections.List1<>(elements[0]);
1035             case 2:
1036                 return new ImmutableCollections.List2<>(elements[0], elements[1]);
1037             default:
1038                 return new ImmutableCollections.ListN<>(elements);
1039         }
1040     }



















1041 }
--- EOF ---