771 @Override
772 default Spliterator<E> spliterator() {
773 if (this instanceof RandomAccess) {
774 return new AbstractList.RandomAccessSpliterator<>(this);
775 } else {
776 return Spliterators.spliterator(this, Spliterator.ORDERED);
777 }
778 }
779
780 /**
781 * Returns an unmodifiable list containing zero elements.
782 *
783 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
784 *
785 * @param <E> the {@code List}'s element type
786 * @return an empty {@code List}
787 *
788 * @since 9
789 */
790 static <E> List<E> of() {
791 return ImmutableCollections.List0.instance();
792 }
793
794 /**
795 * Returns an unmodifiable list containing one element.
796 *
797 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
798 *
799 * @param <E> the {@code List}'s element type
800 * @param e1 the single element
801 * @return a {@code List} containing the specified element
802 * @throws NullPointerException if the element is {@code null}
803 *
804 * @since 9
805 */
806 static <E> List<E> of(E e1) {
807 return new ImmutableCollections.List1<>(e1);
808 }
809
810 /**
811 * Returns an unmodifiable list containing two elements.
812 *
813 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
814 *
815 * @param <E> the {@code List}'s element type
816 * @param e1 the first element
817 * @param e2 the second element
818 * @return a {@code List} containing the specified elements
819 * @throws NullPointerException if an element is {@code null}
820 *
821 * @since 9
822 */
823 static <E> List<E> of(E e1, E e2) {
824 return new ImmutableCollections.List2<>(e1, e2);
825 }
826
827 /**
828 * Returns an unmodifiable list containing three elements.
829 *
830 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
831 *
832 * @param <E> the {@code List}'s element type
833 * @param e1 the first element
834 * @param e2 the second element
835 * @param e3 the third element
836 * @return a {@code List} containing the specified elements
837 * @throws NullPointerException if an element is {@code null}
838 *
839 * @since 9
840 */
841 static <E> List<E> of(E e1, E e2, E e3) {
842 return new ImmutableCollections.ListN<>(e1, e2, e3);
843 }
844
1014 * <pre>{@code
1015 * String[] array = ... ;
1016 * List<String[]> list = List.<String[]>of(array);
1017 * }</pre>
1018 *
1019 * This will cause the {@link List#of(Object) List.of(E)} method
1020 * to be invoked instead.
1021 *
1022 * @param <E> the {@code List}'s element type
1023 * @param elements the elements to be contained in the list
1024 * @return a {@code List} containing the specified elements
1025 * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1026 *
1027 * @since 9
1028 */
1029 @SafeVarargs
1030 @SuppressWarnings("varargs")
1031 static <E> List<E> of(E... elements) {
1032 switch (elements.length) { // implicit null check of elements
1033 case 0:
1034 return ImmutableCollections.List0.instance();
1035 case 1:
1036 return new ImmutableCollections.List1<>(elements[0]);
1037 case 2:
1038 return new ImmutableCollections.List2<>(elements[0], elements[1]);
1039 default:
1040 return new ImmutableCollections.ListN<>(elements);
1041 }
1042 }
1043
1044 /**
1045 * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of
1046 * the given Collection, in its iteration order. The given Collection must not be null,
1047 * and it must not contain any null elements. If the given Collection is subsequently
1048 * modified, the returned List will not reflect such modifications.
1049 *
1050 * @implNote
1051 * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>,
1052 * calling copyOf will generally not create a copy.
1053 *
1054 * @param <E> the {@code List}'s element type
1055 * @param coll a {@code Collection} from which elements are drawn, must be non-null
1056 * @return a {@code List} containing the elements of the given {@code Collection}
1057 * @throws NullPointerException if coll is null, or if it contains any nulls
1058 * @since 10
|
771 @Override
772 default Spliterator<E> spliterator() {
773 if (this instanceof RandomAccess) {
774 return new AbstractList.RandomAccessSpliterator<>(this);
775 } else {
776 return Spliterators.spliterator(this, Spliterator.ORDERED);
777 }
778 }
779
780 /**
781 * Returns an unmodifiable list containing zero elements.
782 *
783 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
784 *
785 * @param <E> the {@code List}'s element type
786 * @return an empty {@code List}
787 *
788 * @since 9
789 */
790 static <E> List<E> of() {
791 return ImmutableCollections.emptyList();
792 }
793
794 /**
795 * Returns an unmodifiable list containing one element.
796 *
797 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
798 *
799 * @param <E> the {@code List}'s element type
800 * @param e1 the single element
801 * @return a {@code List} containing the specified element
802 * @throws NullPointerException if the element is {@code null}
803 *
804 * @since 9
805 */
806 static <E> List<E> of(E e1) {
807 return new ImmutableCollections.List12<>(e1);
808 }
809
810 /**
811 * Returns an unmodifiable list containing two elements.
812 *
813 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
814 *
815 * @param <E> the {@code List}'s element type
816 * @param e1 the first element
817 * @param e2 the second element
818 * @return a {@code List} containing the specified elements
819 * @throws NullPointerException if an element is {@code null}
820 *
821 * @since 9
822 */
823 static <E> List<E> of(E e1, E e2) {
824 return new ImmutableCollections.List12<>(e1, e2);
825 }
826
827 /**
828 * Returns an unmodifiable list containing three elements.
829 *
830 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
831 *
832 * @param <E> the {@code List}'s element type
833 * @param e1 the first element
834 * @param e2 the second element
835 * @param e3 the third element
836 * @return a {@code List} containing the specified elements
837 * @throws NullPointerException if an element is {@code null}
838 *
839 * @since 9
840 */
841 static <E> List<E> of(E e1, E e2, E e3) {
842 return new ImmutableCollections.ListN<>(e1, e2, e3);
843 }
844
1014 * <pre>{@code
1015 * String[] array = ... ;
1016 * List<String[]> list = List.<String[]>of(array);
1017 * }</pre>
1018 *
1019 * This will cause the {@link List#of(Object) List.of(E)} method
1020 * to be invoked instead.
1021 *
1022 * @param <E> the {@code List}'s element type
1023 * @param elements the elements to be contained in the list
1024 * @return a {@code List} containing the specified elements
1025 * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1026 *
1027 * @since 9
1028 */
1029 @SafeVarargs
1030 @SuppressWarnings("varargs")
1031 static <E> List<E> of(E... elements) {
1032 switch (elements.length) { // implicit null check of elements
1033 case 0:
1034 return ImmutableCollections.emptyList();
1035 case 1:
1036 return new ImmutableCollections.List12<>(elements[0]);
1037 case 2:
1038 return new ImmutableCollections.List12<>(elements[0], elements[1]);
1039 default:
1040 return new ImmutableCollections.ListN<>(elements);
1041 }
1042 }
1043
1044 /**
1045 * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of
1046 * the given Collection, in its iteration order. The given Collection must not be null,
1047 * and it must not contain any null elements. If the given Collection is subsequently
1048 * modified, the returned List will not reflect such modifications.
1049 *
1050 * @implNote
1051 * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>,
1052 * calling copyOf will generally not create a copy.
1053 *
1054 * @param <E> the {@code List}'s element type
1055 * @param coll a {@code Collection} from which elements are drawn, must be non-null
1056 * @return a {@code List} containing the elements of the given {@code Collection}
1057 * @throws NullPointerException if coll is null, or if it contains any nulls
1058 * @since 10
|