Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,27 @@ public class Iterators {
* Returns a single element iterator over the supplied value.
*/
public static <T> Iterator<T> single(T element) {
return new Iterator<>() {
return new SingleIterator<>(element);
}

private T value = Objects.requireNonNull(element);
private static final class SingleIterator<T> implements Iterator<T> {
private T value;

@Override
public boolean hasNext() {
return value != null;
}
SingleIterator(T element) {
value = Objects.requireNonNull(element);
}

@Override
public T next() {
final T res = value;
value = null;
return res;
}
};
@Override
public boolean hasNext() {
return value != null;
}

@Override
public T next() {
final T res = value;
value = null;
return res;
}
}

@SafeVarargs
Expand Down Expand Up @@ -496,5 +501,4 @@ public static <T> int hashCode(Iterator<? extends T> iterator, ToIntFunction<T>
}
return result;
}

}