diff --git a/android/guava/src/com/google/common/collect/Iterators.java b/android/guava/src/com/google/common/collect/Iterators.java index 3b4a2b293a53..9f7b6f711295 100644 --- a/android/guava/src/com/google/common/collect/Iterators.java +++ b/android/guava/src/com/google/common/collect/Iterators.java @@ -1105,31 +1105,30 @@ protected T get(int index) { private static final class SingletonIterator extends UnmodifiableIterator { - private @Nullable Object valueOrThis; + private static final Object SENTINEL = new Object(); + + private @Nullable Object valueOrSentinel; SingletonIterator(T value) { - this.valueOrThis = value; + this.valueOrSentinel = value; } @Override public boolean hasNext() { - return valueOrThis != this; + return valueOrSentinel != SENTINEL; } @Override @ParametricNullness public T next() { - Object result = valueOrThis; - valueOrThis = this; - // We put the common case first, even though it's unlikely to matter if the code is run much: - // https://shipilev.net/jvm/anatomy-quarks/28-frequency-based-code-layout/ - if (result != this) { - // The field held either a `T` or `this`, and it turned out not to be `this`. - @SuppressWarnings("unchecked") - T t = (T) result; - return t; + if (valueOrSentinel == SENTINEL) { + throw new NoSuchElementException(); } - 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; } } diff --git a/guava/src/com/google/common/collect/Iterators.java b/guava/src/com/google/common/collect/Iterators.java index 3b4a2b293a53..9f7b6f711295 100644 --- a/guava/src/com/google/common/collect/Iterators.java +++ b/guava/src/com/google/common/collect/Iterators.java @@ -1105,31 +1105,30 @@ protected T get(int index) { private static final class SingletonIterator extends UnmodifiableIterator { - private @Nullable Object valueOrThis; + private static final Object SENTINEL = new Object(); + + private @Nullable Object valueOrSentinel; SingletonIterator(T value) { - this.valueOrThis = value; + this.valueOrSentinel = value; } @Override public boolean hasNext() { - return valueOrThis != this; + return valueOrSentinel != SENTINEL; } @Override @ParametricNullness public T next() { - Object result = valueOrThis; - valueOrThis = this; - // We put the common case first, even though it's unlikely to matter if the code is run much: - // https://shipilev.net/jvm/anatomy-quarks/28-frequency-based-code-layout/ - if (result != this) { - // The field held either a `T` or `this`, and it turned out not to be `this`. - @SuppressWarnings("unchecked") - T t = (T) result; - return t; + if (valueOrSentinel == SENTINEL) { + throw new NoSuchElementException(); } - 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; } }