All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.ImmutableList Maven / Gradle / Ivy

Go to download

Google Collections Library is a suite of new collections and collection-related goodness for Java 5.0

The newest version!
/*
 * 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.common.collect;

import com.google.common.base.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;

import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
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. */ public static ImmutableList of(E element) { // TODO: evaluate a specialized SingletonImmutableList return new RegularImmutableList(new Object[] { checkNotNull(element) }); } /** * 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. 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} are null */ public static ImmutableList copyOf(Iterable elements) { if (elements instanceof ImmutableList) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableList list = (ImmutableList) elements; return list; } Collection collection = (elements instanceof Collection) ? (Collection) elements : Lists.newArrayList(elements); int size = collection.size(); return (size == 0) ? ImmutableList.of() : new RegularImmutableList(copyIntoArray(collection, size)); } private ImmutableList() {} // 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 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 { public int size() { return 0; } public boolean isEmpty() { return true; } @Override public boolean contains(Object target) { return false; } public Iterator iterator() { return Iterators.emptyIterator(); } @Override public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; } @Override public T[] toArray(T[] a) { if (a.length > 0) { a[0] = null; } return a; } public Object get(int index) { throw new IndexOutOfBoundsException( "Invalid index: " + index + ", list size is 0"); } @Override public int indexOf(Object target) { return -1; } @Override public int lastIndexOf(Object target) { return -1; } @Override public ImmutableList subList(int fromIndex, int toIndex) { if (fromIndex != 0 || toIndex != 0) { throw new IndexOutOfBoundsException("Invalid range: " + fromIndex + ".." + toIndex + ", list size is 0"); } return this; } public ListIterator listIterator() { return Iterators.emptyListIterator(); } public ListIterator listIterator(int start) { if (start != 0) { throw new IndexOutOfBoundsException( "Invalid index: " + start + ", list size is 0"); } return Iterators.emptyListIterator(); } @Override public boolean containsAll(Collection targets) { return targets.isEmpty(); } @Override public boolean equals(Object object) { return object == this || (object instanceof List && ((List) object).isEmpty()); } @Override public int hashCode() { return 1; } @Override public String toString() { return "[]"; } private Object readResolve() { return EMPTY_IMMUTABLE_LIST; } } private static final class RegularImmutableList extends ImmutableList { private final int offset; private final int size; private final Object[] array; private RegularImmutableList(Object[] array, int offset, int size) { this.offset = offset; this.size = size; this.array = array; } private RegularImmutableList(Object[] array) { this(array, 0, array.length); } public int size() { return size; } public boolean isEmpty() { return false; } @Override public boolean contains(Object target) { return indexOf(target) != -1; } // The fake cast to E is safe because the creation methods only allow E's @SuppressWarnings("unchecked") public Iterator iterator() { return (Iterator) Iterators.forArray(array, offset, size); } @Override public Object[] toArray() { Object[] newArray = new Object[size()]; System.arraycopy(array, offset, newArray, 0, size); return newArray; } @Override public T[] toArray(T[] other) { if (other.length < size) { other = ObjectArrays.newArray(other, size); } else if (other.length > size) { other[size] = null; } System.arraycopy(array, offset, other, 0, size); return other; } // The fake cast to E is safe because the creation methods only allow E's @SuppressWarnings("unchecked") public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException( "Invalid index: " + index + ", list size is " + size); } return (E) array[index + offset]; } @Override public int indexOf(Object target) { if (target != null) { for (int i = offset; i < offset + size; i++) { if (array[i].equals(target)) { return i - offset; } } } return -1; } @Override public int lastIndexOf(Object target) { if (target != null) { for (int i = offset + size - 1; i >= offset; i--) { if (array[i].equals(target)) { return i - offset; } } } return -1; } @Override public ImmutableList subList(int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) { throw new IndexOutOfBoundsException("Invalid range: " + fromIndex + ".." + toIndex + ", list size is " + size); } return (fromIndex == toIndex) ? ImmutableList.of() : new RegularImmutableList( array, offset + fromIndex, toIndex - fromIndex); } public ListIterator listIterator() { return listIterator(0); } public ListIterator listIterator(final int start) { if ((start < 0) || (start > size)) { throw new IndexOutOfBoundsException( "Invalid index: " + start + ", list size is " + size); } return new ListIterator() { int index = start; public boolean hasNext() { return index < size; } public boolean hasPrevious() { return index > 0; } public int nextIndex() { return index; } public int previousIndex() { return index - 1; } public E next() { E result; try { result = get(index); } catch (IndexOutOfBoundsException rethrown) { throw new NoSuchElementException(); } index++; return result; } public E previous() { E result; try { result = get(index - 1); } catch (IndexOutOfBoundsException rethrown) { throw new NoSuchElementException(); } index--; return result; } public void set(E o) { throw new UnsupportedOperationException(); } public void add(E o) { throw new UnsupportedOperationException(); } public void remove() { throw new UnsupportedOperationException(); } }; } @Override public boolean equals(Object object) { if (object == this) { return true; } if (!(object instanceof List)) { return false; } List that = (List) object; if (that.size() != size()) { return false; } int index = offset; if (object instanceof RegularImmutableList) { RegularImmutableList other = (RegularImmutableList) object; for (int i = other.offset; i < other.offset + other.size; i++) { if (!array[index++].equals(other.array[i])) { return false; } } } else { for (Object element : that) { if (!array[index++].equals(element)) { return false; } } } return true; } @Override public int hashCode() { int hashCode = 1; for (int i = offset; i < offset + size; i++) { hashCode = 31 * hashCode + array[i].hashCode(); } return hashCode; } @Override public String toString() { StringBuilder sb = new StringBuilder(size() * 16); sb.append('[').append(array[offset]); for (int i = offset + 1; i < offset + size; i++) { sb.append(", ").append(array[i]); } return sb.append(']').toString(); } } private static Object[] copyIntoArray(Object[] source) { Object[] array = new Object[source.length]; int index = 0; for (Object element : source) { if (element == null) { throw new NullPointerException("at index " + index); } array[index++] = element; } return array; } private static Object[] copyIntoArray(Iterable source, int size) { Object[] array = new Object[size]; int index = 0; for (Object element : source) { if (element == null) { throw new NullPointerException("at index " + index); } array[index++] = element; } return array; } /* * Serializes ImmutableLists as their logical contents. This ensures that * implementation types do not leak into the serialized representation. */ private static class SerializedForm implements Serializable { final Object[] elements; SerializedForm(Object[] elements) { this.elements = elements; } Object readResolve() { return of(elements); } private static final long serialVersionUID = 0; } private void readObject(ObjectInputStream stream) throws InvalidObjectException { throw new InvalidObjectException("Use SerializedForm"); } @Override Object writeReplace() { return new SerializedForm(toArray()); } }