Skip to content

Commit

Permalink
Roll back initial atttempt at micro-optimizing singletonIterator.
Browse files Browse the repository at this point in the history
But at least keep part of the change that moved from an anonymous class to a named class, since that maybe justifies one tenth of the time that I've forced people to spend on this....

RELNOTES=n/a
PiperOrigin-RevId: 630380623
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 3, 2024
1 parent ba820c6 commit 19d042f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
18 changes: 7 additions & 11 deletions android/guava/src/com/google/common/collect/Iterators.java
Expand Up @@ -1105,30 +1105,26 @@ protected T get(int index) {

private static final class SingletonIterator<T extends @Nullable Object>
extends UnmodifiableIterator<T> {
private static final Object SENTINEL = new Object();

private @Nullable Object valueOrSentinel;
private final T value;
private boolean done;

SingletonIterator(T value) {
this.valueOrSentinel = value;
this.value = value;
}

@Override
public boolean hasNext() {
return valueOrSentinel != SENTINEL;
return !done;
}

@Override
@ParametricNullness
public T next() {
if (valueOrSentinel == SENTINEL) {
if (done) {
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;
done = true;
return value;
}
}

Expand Down
18 changes: 7 additions & 11 deletions guava/src/com/google/common/collect/Iterators.java
Expand Up @@ -1105,30 +1105,26 @@ protected T get(int index) {

private static final class SingletonIterator<T extends @Nullable Object>
extends UnmodifiableIterator<T> {
private static final Object SENTINEL = new Object();

private @Nullable Object valueOrSentinel;
private final T value;
private boolean done;

SingletonIterator(T value) {
this.valueOrSentinel = value;
this.value = value;
}

@Override
public boolean hasNext() {
return valueOrSentinel != SENTINEL;
return !done;
}

@Override
@ParametricNullness
public T next() {
if (valueOrSentinel == SENTINEL) {
if (done) {
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;
done = true;
return value;
}
}

Expand Down

0 comments on commit 19d042f

Please sign in to comment.