770 @Override
771 default Spliterator<E> spliterator() {
772 if (this instanceof RandomAccess) {
773 return new AbstractList.RandomAccessSpliterator<>(this);
774 } else {
775 return Spliterators.spliterator(this, Spliterator.ORDERED);
776 }
777 }
778
779 /**
780 * Returns an immutable list containing zero elements.
781 *
782 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
783 *
784 * @param <E> the {@code List}'s element type
785 * @return an empty {@code List}
786 *
787 * @since 9
788 */
789 static <E> List<E> of() {
790 return ImmutableCollections.List0.instance();
791 }
792
793 /**
794 * Returns an immutable list containing one element.
795 *
796 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
797 *
798 * @param <E> the {@code List}'s element type
799 * @param e1 the single element
800 * @return a {@code List} containing the specified element
801 * @throws NullPointerException if the element is {@code null}
802 *
803 * @since 9
804 */
805 static <E> List<E> of(E e1) {
806 return new ImmutableCollections.List1<>(e1);
807 }
808
809 /**
810 * Returns an immutable list containing two elements.
811 *
812 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
813 *
814 * @param <E> the {@code List}'s element type
815 * @param e1 the first element
816 * @param e2 the second element
817 * @return a {@code List} containing the specified elements
818 * @throws NullPointerException if an element is {@code null}
819 *
820 * @since 9
821 */
822 static <E> List<E> of(E e1, E e2) {
823 return new ImmutableCollections.List2<>(e1, e2);
824 }
825
826 /**
827 * Returns an immutable list containing three elements.
828 *
829 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
830 *
831 * @param <E> the {@code List}'s element type
832 * @param e1 the first element
833 * @param e2 the second element
834 * @param e3 the third element
835 * @return a {@code List} containing the specified elements
836 * @throws NullPointerException if an element is {@code null}
837 *
838 * @since 9
839 */
840 static <E> List<E> of(E e1, E e2, E e3) {
841 return new ImmutableCollections.ListN<>(e1, e2, e3);
842 }
843
1013 * <pre>{@code
1014 * String[] array = ... ;
1015 * List<String[]> list = List.<String[]>of(array);
1016 * }</pre>
1017 *
1018 * This will cause the {@link List#of(Object) List.of(E)} method
1019 * to be invoked instead.
1020 *
1021 * @param <E> the {@code List}'s element type
1022 * @param elements the elements to be contained in the list
1023 * @return a {@code List} containing the specified elements
1024 * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1025 *
1026 * @since 9
1027 */
1028 @SafeVarargs
1029 @SuppressWarnings("varargs")
1030 static <E> List<E> of(E... elements) {
1031 switch (elements.length) { // implicit null check of elements
1032 case 0:
1033 return ImmutableCollections.List0.instance();
1034 case 1:
1035 return new ImmutableCollections.List1<>(elements[0]);
1036 case 2:
1037 return new ImmutableCollections.List2<>(elements[0], elements[1]);
1038 default:
1039 return new ImmutableCollections.ListN<>(elements);
1040 }
1041 }
1042 }
|
770 @Override
771 default Spliterator<E> spliterator() {
772 if (this instanceof RandomAccess) {
773 return new AbstractList.RandomAccessSpliterator<>(this);
774 } else {
775 return Spliterators.spliterator(this, Spliterator.ORDERED);
776 }
777 }
778
779 /**
780 * Returns an immutable list containing zero elements.
781 *
782 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
783 *
784 * @param <E> the {@code List}'s element type
785 * @return an empty {@code List}
786 *
787 * @since 9
788 */
789 static <E> List<E> of() {
790 return ImmutableCollections.emptyList();
791 }
792
793 /**
794 * Returns an immutable list containing one element.
795 *
796 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
797 *
798 * @param <E> the {@code List}'s element type
799 * @param e1 the single element
800 * @return a {@code List} containing the specified element
801 * @throws NullPointerException if the element is {@code null}
802 *
803 * @since 9
804 */
805 static <E> List<E> of(E e1) {
806 return new ImmutableCollections.List1<>(e1);
807 }
808
809 /**
810 * Returns an immutable list containing two elements.
811 *
812 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
813 *
814 * @param <E> the {@code List}'s element type
815 * @param e1 the first element
816 * @param e2 the second element
817 * @return a {@code List} containing the specified elements
818 * @throws NullPointerException if an element is {@code null}
819 *
820 * @since 9
821 */
822 static <E> List<E> of(E e1, E e2) {
823 return new ImmutableCollections.ListN<>(e1, e2);
824 }
825
826 /**
827 * Returns an immutable list containing three elements.
828 *
829 * See <a href="#immutable">Immutable List Static Factory Methods</a> for details.
830 *
831 * @param <E> the {@code List}'s element type
832 * @param e1 the first element
833 * @param e2 the second element
834 * @param e3 the third element
835 * @return a {@code List} containing the specified elements
836 * @throws NullPointerException if an element is {@code null}
837 *
838 * @since 9
839 */
840 static <E> List<E> of(E e1, E e2, E e3) {
841 return new ImmutableCollections.ListN<>(e1, e2, e3);
842 }
843
1013 * <pre>{@code
1014 * String[] array = ... ;
1015 * List<String[]> list = List.<String[]>of(array);
1016 * }</pre>
1017 *
1018 * This will cause the {@link List#of(Object) List.of(E)} method
1019 * to be invoked instead.
1020 *
1021 * @param <E> the {@code List}'s element type
1022 * @param elements the elements to be contained in the list
1023 * @return a {@code List} containing the specified elements
1024 * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1025 *
1026 * @since 9
1027 */
1028 @SafeVarargs
1029 @SuppressWarnings("varargs")
1030 static <E> List<E> of(E... elements) {
1031 switch (elements.length) { // implicit null check of elements
1032 case 0:
1033 return ImmutableCollections.emptyList();
1034 case 1:
1035 return new ImmutableCollections.List1<>(elements[0]);
1036 default:
1037 return new ImmutableCollections.ListN<>(elements);
1038 }
1039 }
1040 }
|