diff options
Diffstat (limited to 'android/guava/src/com/google/common/collect/Iterators.java')
-rw-r--r-- | android/guava/src/com/google/common/collect/Iterators.java | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/android/guava/src/com/google/common/collect/Iterators.java b/android/guava/src/com/google/common/collect/Iterators.java index 0699202fe..009a41cef 100644 --- a/android/guava/src/com/google/common/collect/Iterators.java +++ b/android/guava/src/com/google/common/collect/Iterators.java @@ -42,7 +42,6 @@ import java.util.Deque; import java.util.Enumeration; import java.util.Iterator; import java.util.List; -import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Queue; @@ -1056,47 +1055,40 @@ public final class Iterators { */ @SafeVarargs public static <T extends @Nullable Object> UnmodifiableIterator<T> forArray(T... array) { - return forArray(array, 0, array.length, 0); + return forArrayWithPosition(array, 0); } /** - * Returns a list iterator containing the elements in the specified range of {@code array} in - * order, starting at the specified index. + * Returns a list iterator containing the elements in the specified {@code array} in order, + * starting at the specified {@code position}. * * <p>The {@code Iterable} equivalent of this method is {@code - * Arrays.asList(array).subList(offset, offset + length).listIterator(index)}. + * Arrays.asList(array).listIterator(position)}. */ - static <T extends @Nullable Object> UnmodifiableListIterator<T> forArray( - T[] array, int offset, int length, int index) { - checkArgument(length >= 0); - int end = offset + length; - - // Technically we should give a slightly more descriptive error on overflow - Preconditions.checkPositionIndexes(offset, end, array.length); - Preconditions.checkPositionIndex(index, length); - if (length == 0) { + static <T extends @Nullable Object> UnmodifiableListIterator<T> forArrayWithPosition( + T[] array, int position) { + if (array.length == 0) { + Preconditions.checkPositionIndex(position, array.length); // otherwise checked in ArrayItr return emptyListIterator(); } - return new ArrayItr<>(array, offset, length, index); + return new ArrayItr<>(array, position); } private static final class ArrayItr<T extends @Nullable Object> extends AbstractIndexedListIterator<T> { - static final UnmodifiableListIterator<Object> EMPTY = new ArrayItr<>(new Object[0], 0, 0, 0); + static final UnmodifiableListIterator<Object> EMPTY = new ArrayItr<>(new Object[0], 0); private final T[] array; - private final int offset; - ArrayItr(T[] array, int offset, int length, int index) { - super(length, index); + ArrayItr(T[] array, int position) { + super(array.length, position); this.array = array; - this.offset = offset; } @Override @ParametricNullness protected T get(int index) { - return array[offset + index]; + return array[index]; } } @@ -1107,24 +1099,36 @@ public final class Iterators { */ public static <T extends @Nullable Object> UnmodifiableIterator<T> singletonIterator( @ParametricNullness T value) { - return new UnmodifiableIterator<T>() { - boolean done; + return new SingletonIterator<>(value); + } - @Override - public boolean hasNext() { - return !done; - } + private static final class SingletonIterator<T extends @Nullable Object> + extends UnmodifiableIterator<T> { + private static final Object SENTINEL = new Object(); - @Override - @ParametricNullness - public T next() { - if (done) { - throw new NoSuchElementException(); - } - done = true; - return value; + private Object valueOrSentinel; + + SingletonIterator(T value) { + this.valueOrSentinel = value; + } + + @Override + public boolean hasNext() { + return valueOrSentinel != SENTINEL; + } + + @Override + @ParametricNullness + public T next() { + if (valueOrSentinel == SENTINEL) { + throw new NoSuchElementException(); } - }; + // The field held either a T or SENTINEL, and it turned out not to be SENTINEL. + @SuppressWarnings("unchecked") + T t = (T) valueOrSentinel; + valueOrSentinel = SENTINEL; + return t; + } } /** @@ -1444,9 +1448,4 @@ public final class Iterators { toRemove = null; } } - - /** Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557 */ - static <T extends @Nullable Object> ListIterator<T> cast(Iterator<T> iterator) { - return (ListIterator<T>) iterator; - } } |