/** * 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()); }
public static ImmutableSortedSet copyOfSorted(SortedSet paramSortedSet) { Comparator localComparator = SortedIterables.comparator(paramSortedSet); Object[] arrayOfObject = paramSortedSet.toArray(); if (arrayOfObject.length == 0) { return emptySet(localComparator); } return new RegularImmutableSortedSet(ImmutableList.asImmutableList(arrayOfObject), localComparator); }
static ImmutableSortedSet construct(Comparator paramComparator, int paramInt, Object... paramVarArgs) { int i = sortAndUnique(paramComparator, paramInt, paramVarArgs); if (i == 0) { return emptySet(paramComparator); } if (i < paramVarArgs.length) { paramVarArgs = ObjectArrays.arraysCopyOf(paramVarArgs, i); } return new RegularImmutableSortedSet(ImmutableList.asImmutableList(paramVarArgs), paramComparator); }
RegularImmutableAsList(ImmutableCollection<E> delegate, Object[] array) { this(delegate, ImmutableList.<E>asImmutableList(array)); }
public ImmutableList build() { return ImmutableList.asImmutableList(contents, size); }