Created
October 27, 2015 21:12
-
-
Save markmo/3dd46f187b6bce499701 to your computer and use it in GitHub Desktop.
Convert Iterator to Iterable in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static <T> Iterable<T> once(final Iterator<T> source) { | |
return new Iterable<T>() { | |
private AtomicBoolean exhausted = new AtomicBoolean(); | |
@Override | |
public Iterator<T> iterator() { | |
Preconditions.checkState(!exhausted.getAndSet(true)); | |
return source; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment