com.google.common.collect.ImmutableList Maven / Gradle / Ivy
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkPositionIndex;
import static com.google.common.base.Preconditions.checkPositionIndexes;
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static com.google.common.collect.ObjectArrays.checkElementsNotNull;
import static com.google.common.collect.RegularImmutableList.EMPTY;
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.RandomAccess;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
/**
* A {@link List} whose contents will never change, with many other important properties detailed at
* {@link ImmutableCollection}.
*
* See the Guava User Guide article on immutable collections.
*
* @see ImmutableMap
* @see ImmutableSet
* @author Kevin Bourrillion
* @since 2.0
*/
@GwtCompatible(serializable = true, emulated = true)
@SuppressWarnings("serial") // we're overriding default serialization
public abstract class ImmutableList extends ImmutableCollection
implements List, RandomAccess {
/**
* Returns the empty immutable list. This list behaves and performs comparably to {@link
* Collections#emptyList}, and is preferable mainly for consistency and maintainability of your
* code.
*/
// Casting to any type is safe because the list will never hold any elements.
@SuppressWarnings("unchecked")
public static ImmutableList of() {
return (ImmutableList) EMPTY;
}
/**
* Returns an immutable list containing a single element. This list behaves and performs
* comparably to {@link Collections#singleton}, but will not accept a null element. It is
* preferable mainly for consistency and maintainability of your code.
*
* @throws NullPointerException if {@code element} is null
*/
public static ImmutableList of(E element) {
return construct(element);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2) {
return construct(e1, e2);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3) {
return construct(e1, e2, e3);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3, E e4) {
return construct(e1, e2, e3, e4);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3, E e4, E e5) {
return construct(e1, e2, e3, e4, e5);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3, E e4, E e5, E e6) {
return construct(e1, e2, e3, e4, e5, e6);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
return construct(e1, e2, e3, e4, e5, e6, e7);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
return construct(e1, e2, e3, e4, e5, e6, e7, e8);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(
E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any element is null
*/
public static ImmutableList of(
E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11) {
return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11);
}
// These go up to eleven. After that, you just get the varargs form, and
// whatever warnings might come along with it. :(
/**
* Returns an immutable list containing the given elements, in order.
*
* The array {@code others} must not be longer than {@code Integer.MAX_VALUE - 12}.
*
* @throws NullPointerException if any element is null
* @since 3.0 (source-compatible since 2.0)
*/
@SafeVarargs // For Eclipse. For internal javac we have disabled this pointless type of warning.
public static ImmutableList of(
E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11, E e12, E... others) {
checkArgument(
others.length <= Integer.MAX_VALUE - 12, "the total number of elements must fit in an int");
Object[] array = new Object[12 + others.length];
array[0] = e1;
array[1] = e2;
array[2] = e3;
array[3] = e4;
array[4] = e5;
array[5] = e6;
array[6] = e7;
array[7] = e8;
array[8] = e9;
array[9] = e10;
array[10] = e11;
array[11] = e12;
System.arraycopy(others, 0, array, 12, others.length);
return construct(array);
}
/**
* Returns an immutable list containing the given elements, in order. If {@code elements} is a
* {@link Collection}, this method behaves exactly as {@link #copyOf(Collection)}; otherwise, it
* behaves exactly as {@code copyOf(elements.iterator()}.
*
* @throws NullPointerException if {@code elements} contains a null element
*/
public static ImmutableList copyOf(Iterable extends E> elements) {
checkNotNull(elements); // TODO(kevinb): is this here only for GWT?
return (elements instanceof Collection)
? copyOf((Collection extends E>) elements)
: copyOf(elements.iterator());
}
/**
* Returns an immutable list containing the given elements, in order.
*
* 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.
*
*
Note that if {@code list} is a {@code List}, then {@code ImmutableList.copyOf(list)}
* returns an {@code ImmutableList} containing each of the strings in {@code list}, while
* ImmutableList.of(list)} returns an {@code ImmutableList>} containing one element
* (the given list itself).
*
* 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 {@code elements} contains a null element
*/
public static ImmutableList copyOf(Collection extends E> elements) {
if (elements instanceof ImmutableCollection) {
@SuppressWarnings("unchecked") // all supported methods are covariant
ImmutableList list = ((ImmutableCollection) elements).asList();
return list.isPartialView() ? ImmutableList.asImmutableList(list.toArray()) : list;
}
return construct(elements.toArray());
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if {@code elements} contains a null element
*/
public static ImmutableList copyOf(Iterator extends E> elements) {
// We special-case for 0 or 1 elements, but going further is madness.
if (!elements.hasNext()) {
return of();
}
E first = elements.next();
if (!elements.hasNext()) {
return of(first);
} else {
return new ImmutableList.Builder().add(first).addAll(elements).build();
}
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if {@code elements} contains a null element
* @since 3.0
*/
public static ImmutableList copyOf(E[] elements) {
return (elements.length == 0)
? ImmutableList.of()
: ImmutableList.construct(elements.clone());
}
/**
* 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.
*
* 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.
*
*
Java 8 users: 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 > ImmutableList sortedCopyOf(
Iterable extends E> elements) {
Comparable>[] array = Iterables.toArray(elements, new Comparable>[0]);
checkElementsNotNull((Object[]) array);
Arrays.sort(array);
return asImmutableList(array);
}
/**
* 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.
*
* 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.
*
*
Java 8 users: 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 ImmutableList 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);
}
/** Views the array as an immutable list. Checks for nulls; does not copy. */
private static ImmutableList construct(Object... elements) {
return asImmutableList(checkElementsNotNull(elements));
}
/**
* Views the array as an immutable list. Does not check for nulls; does not copy.
*
* The array must be internally created.
*/
static ImmutableList asImmutableList(Object[] elements) {
return asImmutableList(elements, elements.length);
}
/** Views the array as an immutable list. Does not check for nulls. */
static ImmutableList asImmutableList(Object[] elements, int length) {
if (length == 0) {
return of();
}
return new RegularImmutableList(elements, length);
}
ImmutableList() {}
// This declaration is needed to make List.iterator() and
// ImmutableCollection.iterator() consistent.
@Override
public UnmodifiableIterator iterator() {
return listIterator();
}
@Override
public UnmodifiableListIterator listIterator() {
return listIterator(0);
}
@SuppressWarnings("unchecked")
@Override
public UnmodifiableListIterator listIterator(int index) {
checkPositionIndex(index, size());
if (isEmpty()) {
return (UnmodifiableListIterator) EMPTY_ITR;
} else {
return new Itr(this, index);
}
}
/** A singleton implementation of iterator() for the empty ImmutableList. */
private static final UnmodifiableListIterator