diff options
Diffstat (limited to 'guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableList.java')
-rw-r--r-- | guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableList.java | 102 |
1 files changed, 55 insertions, 47 deletions
diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableList.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableList.java index ca5a95a..db67daa 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableList.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableList.java @@ -26,20 +26,30 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.RandomAccess; import javax.annotation.Nullable; /** * GWT emulated version of {@link ImmutableList}. - * TODO(cpovirk): more doc * * @author Hayward Chan */ @SuppressWarnings("serial") // we're overriding default serialization -public abstract class ImmutableList<E> extends ImmutableCollection<E> +public abstract class ImmutableList<E> extends ForwardingImmutableCollection<E> implements List<E>, RandomAccess { - ImmutableList() {} + + private transient final List<E> delegate; + + ImmutableList(List<E> delegate) { + super(delegate); + this.delegate = Collections.unmodifiableList(delegate); + } + + ImmutableList() { + this(Collections.<E>emptyList()); + } // Casting to any type is safe because the list will never hold any elements. @SuppressWarnings("unchecked") @@ -116,6 +126,19 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E> return new RegularImmutableList<E>(ImmutableList.<E>nullCheckedList(array)); } + public static <E> ImmutableList<E> of(E[] elements) { + checkNotNull(elements); // for GWT + switch (elements.length) { + case 0: + return ImmutableList.of(); + case 1: + return new SingletonImmutableList<E>(elements[0]); + default: + return new RegularImmutableList<E>( + ImmutableList.<E>nullCheckedList(elements)); + } + } + private static void arrayCopy(Object[] dest, int pos, Object... source) { System.arraycopy(source, 0, dest, pos, source.length); } @@ -165,7 +188,7 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E> } // Factory method that skips the null checks. Used only when the elements - // are guaranteed to be non-null. + // are guaranteed to be null. static <E> ImmutableList<E> unsafeDelegateList(List<? extends E> list) { switch (list.size()) { case 0: @@ -179,14 +202,8 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E> } } - /** - * Views the array as an immutable list. The array must have only {@code E} elements. - * - * <p>The array must be internally created. - */ - @SuppressWarnings("unchecked") // caller is reponsible for getting this right - static <E> ImmutableList<E> asImmutableList(Object[] elements) { - return unsafeDelegateList((List) Arrays.asList(elements)); + static <E> ImmutableList<E> backedBy(E[] elements) { + return unsafeDelegateList(Arrays.asList(elements)); } private static <E> List<E> nullCheckedList(Object... array) { @@ -200,14 +217,12 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E> return Arrays.asList(castedArray); } - @Override public int indexOf(@Nullable Object object) { - return (object == null) ? -1 : Lists.indexOfImpl(this, object); + return delegate.indexOf(object); } - @Override public int lastIndexOf(@Nullable Object object) { - return (object == null) ? -1 : Lists.lastIndexOfImpl(this, object); + return delegate.lastIndexOf(object); } public final boolean addAll(int index, Collection<? extends E> newElements) { @@ -226,45 +241,44 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E> throw new UnsupportedOperationException(); } - @Override public UnmodifiableIterator<E> iterator() { - return listIterator(); + public E get(int index) { + return delegate.get(index); } - @Override public ImmutableList<E> subList(int fromIndex, int toIndex) { - return unsafeDelegateList(Lists.subListImpl(this, fromIndex, toIndex)); + public ImmutableList<E> subList(int fromIndex, int toIndex) { + return unsafeDelegateList(delegate.subList(fromIndex, toIndex)); } - @Override public UnmodifiableListIterator<E> listIterator() { - return listIterator(0); + public ListIterator<E> listIterator() { + return delegate.listIterator(); } - @Override public UnmodifiableListIterator<E> listIterator(int index) { - return new AbstractIndexedListIterator<E>(size(), index) { - @Override - protected E get(int index) { - return ImmutableList.this.get(index); - } - }; + public ListIterator<E> listIterator(int index) { + return delegate.listIterator(index); } @Override public ImmutableList<E> asList() { return this; } + + public ImmutableList<E> reverse(){ + List<E> list = Lists.newArrayList(this); + Collections.reverse(list); + return unsafeDelegateList(list); + } - @Override - public boolean equals(@Nullable Object obj) { - return Lists.equalsImpl(this, obj); + @Override public Object[] toArray() { + // Note that ArrayList.toArray() doesn't work here because it returns E[] + // instead of Object[]. + return delegate.toArray(new Object[size()]); } - @Override - public int hashCode() { - return Lists.hashCodeImpl(this); + @Override public boolean equals(Object obj) { + return delegate.equals(obj); } - public ImmutableList<E> reverse() { - List<E> list = Lists.newArrayList(this); - Collections.reverse(list); - return unsafeDelegateList(list); + @Override public int hashCode() { + return delegate.hashCode(); } public static <E> Builder<E> builder() { @@ -272,15 +286,9 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E> } public static final class Builder<E> extends ImmutableCollection.Builder<E> { - private final ArrayList<E> contents; + private final ArrayList<E> contents = Lists.newArrayList(); - public Builder() { - contents = Lists.newArrayList(); - } - - Builder(int capacity) { - contents = Lists.newArrayListWithCapacity(capacity); - } + public Builder() {} @Override public Builder<E> add(E element) { contents.add(checkNotNull(element)); |