/** * Views the array as an immutable list. Does not check for nulls; does not copy. * * <p>The array must be internally created. */ static <E> ImmutableList<E> asImmutableList(Object[] elements) { return asImmutableList(elements, elements.length); }
/** * Returns a newly-created {@code ImmutableList} based on the contents of * the {@code Builder}. */ @Override public ImmutableList<E> build() { return asImmutableList(contents, size); } }
RegularImmutableAsList(ImmutableCollection<E> delegate, Object[] array) { this(delegate, ImmutableList.<E>asImmutableList(array)); }
/** * Views the array as an immutable list. Checks for nulls; does not copy. */ private static <E> ImmutableList<E> construct(Object... elements) { return asImmutableList(checkElementsNotNull(elements)); }
/** * Returns an immutable list containing the given elements, sorted according to their natural * order. The sorting algorithm used is stable, so elements that compare as equal will stay in the * order in which they appear in the input. * * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code * ImmutableSortedSet.copyOf(elements)}; if you want a {@code List} you can use its {@code * asList()} view. * * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted * {@code ImmutableList}, use {@code stream.sorted().collect(toImmutableList())}. * * @throws NullPointerException if any element in the input is null * @since 21.0 */ public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf( Iterable<? extends E> elements) { Comparable<?>[] array = Iterables.toArray(elements, new Comparable<?>[0]); checkElementsNotNull((Object[]) array); Arrays.sort(array); return asImmutableList(array); }
ImmutableList.<E>asImmutableList(contents, uniques), comparator);
/** * Returns an immutable list containing the given elements, in sorted order relative to the * specified comparator. The sorting algorithm used is stable, so elements that compare as equal * will stay in the order in which they appear in the input. * * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code * ImmutableSortedSet.copyOf(comparator, elements)}; if you want a {@code List} you can use its * {@code asList()} view. * * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted * {@code ImmutableList}, use {@code stream.sorted(comparator).collect(toImmutableList())}. * * @throws NullPointerException if any element in the input is null * @since 21.0 */ public static <E> ImmutableList<E> sortedCopyOf( Comparator<? super E> comparator, Iterable<? extends E> elements) { checkNotNull(comparator); @SuppressWarnings("unchecked") // all supported methods are covariant E[] array = (E[]) Iterables.toArray(elements); checkElementsNotNull(array); Arrays.sort(array, comparator); return asImmutableList(array); }
/** * Returns an immutable list containing the given elements, in order. * * <p>Despite the method name, this method attempts to avoid actually copying * the data when it is safe to do so. The exact circumstances under which a * copy will or will not be performed are undocumented and subject to change. * * <p>Note that if {@code list} is a {@code List<String>}, then {@code * ImmutableList.copyOf(list)} returns an {@code ImmutableList<String>} * containing each of the strings in {@code list}, while * ImmutableList.of(list)} returns an {@code ImmutableList<List<String>>} * containing one element (the given list itself). * * <p>This method is safe to use even when {@code elements} is a synchronized * or concurrent collection that is currently being modified by another * thread. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableList<E> copyOf(Collection<? extends E> elements) { if (elements instanceof ImmutableCollection) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableList<E> list = ((ImmutableCollection<E>) elements).asList(); return list.isPartialView() ? ImmutableList.<E>asImmutableList(list.toArray()) : list; } return construct(elements.toArray()); }