com.google.inject.internal.ImmutableList Maven / Gradle / Ivy
/*
* Copyright (C) 2007 Google Inc.
*
* 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.inject.internal;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
/**
* A high-performance, immutable, random-access {@code List} implementation.
* Does not permit null elements.
*
* Unlike {@link Collections#unmodifiableList}, which is a view of a
* separate collection that can still change, an instance of {@code
* ImmutableList} contains its own private data and will never change.
* {@code ImmutableList} is convenient for {@code public static final} lists
* ("constant lists") and also lets you easily make a "defensive copy" of a list
* provided to your class by a caller.
*
*
Note: Although this class is not final, it cannot be subclassed as
* it has no public or protected constructors. Thus, instances of this type are
* guaranteed to be immutable.
*
* @see ImmutableMap
* @see ImmutableSet
* @author Kevin Bourrillion
*/
@SuppressWarnings("serial") // we're overriding default serialization
public abstract class ImmutableList extends ImmutableCollection
implements List, RandomAccess {
private static final ImmutableList> EMPTY_IMMUTABLE_LIST
= new EmptyImmutableList();
/**
* Returns the empty immutable list. This set 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_IMMUTABLE_LIST;
}
/**
* 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) {
// TODO: evaluate a specialized SingletonImmutableList
return new RegularImmutableList(copyIntoArray(element));
}
// TODO: Add similar overloadings to ImmutableSet and ImmutableSortedSet
/**
* 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 new RegularImmutableList(copyIntoArray(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 new RegularImmutableList(copyIntoArray(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 new RegularImmutableList(copyIntoArray(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 new RegularImmutableList(copyIntoArray(e1, e2, e3, e4, e5));
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any of {@code elements} is null
*/
public static ImmutableList of(E... elements) {
return (elements.length == 0)
? ImmutableList.of()
: new RegularImmutableList(copyIntoArray(elements));
}
/**
* Returns an immutable list containing the given elements, in order. This
* method iterates over {@code elements} at most once. 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).
*
* Note: Despite what the method name suggests, if {@code elements}
* is an {@code ImmutableList}, no copy will actually be performed, and the
* given list itself will be returned.
*
* @throws NullPointerException if any of {@code elements} is null
*/
public static ImmutableList copyOf(Iterable extends E> elements) {
if (elements instanceof ImmutableList) {
/*
* TODO: If the given ImmutableList is a sublist, copy the referenced
* portion of the array into a new array to save space?
*/
@SuppressWarnings("unchecked") // all supported methods are covariant
ImmutableList list = (ImmutableList) elements;
return list;
} else if (elements instanceof Collection) {
@SuppressWarnings("unchecked")
Collection extends E> coll = (Collection extends E>) elements;
return copyOfInternal(coll);
} else {
return copyOfInternal(Lists.newArrayList(elements));
}
}
/**
* Returns an immutable list containing the given elements, in order.
*
* @throws NullPointerException if any of {@code elements} is null
*/
public static ImmutableList copyOf(Iterator extends E> elements) {
return copyOfInternal(Lists.newArrayList(elements));
}
private static ImmutableList copyOfInternal(
ArrayList extends E> list) {
return (list.isEmpty())
? ImmutableList.of()
: new RegularImmutableList(nullChecked(list.toArray()));
}
/**
* Checks that all the array elements are non-null.
*
* @return the argument array
* @throws NullPointerException if any element is null
*/
private static Object[] nullChecked(Object[] array) {
for (int i = 0, len = array.length; i < len; i++) {
if (array[i] == null) {
throw new NullPointerException("at index " + i);
}
}
return array;
}
private static ImmutableList copyOfInternal(
Collection extends E> collection) {
int size = collection.size();
return (size == 0)
? ImmutableList.of()
: ImmutableList.createFromIterable(collection, size);
}
private ImmutableList() {}
// This declaration is needed to make List.iterator() and
// ImmutableCollection.iterator() consistent.
@Override public abstract UnmodifiableIterator iterator();
// Mark these two methods with @Nullable
public abstract int indexOf(@Nullable Object object);
public abstract int lastIndexOf(@Nullable Object object);
// constrain the return type to ImmutableList
/**
* Returns an immutable list of the elements between the specified {@code
* fromIndex}, inclusive, and {@code toIndex}, exclusive. (If {@code
* fromIndex} and {@code toIndex} are equal, the empty immutable list is
* returned.)
*/
public abstract ImmutableList subList(int fromIndex, int toIndex);
/**
* Guaranteed to throw an exception and leave the list unmodified.
*
* @throws UnsupportedOperationException always
*/
public final boolean addAll(int index, Collection extends E> newElements) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the list unmodified.
*
* @throws UnsupportedOperationException always
*/
public final E set(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the list unmodified.
*
* @throws UnsupportedOperationException always
*/
public final void add(int index, E element) {
throw new UnsupportedOperationException();
}
/**
* Guaranteed to throw an exception and leave the list unmodified.
*
* @throws UnsupportedOperationException always
*/
public final E remove(int index) {
throw new UnsupportedOperationException();
}
private static final class EmptyImmutableList extends ImmutableList