Simplify RevWalk#iterator by factoring out common code

Factor out a helper that calls next() and tunnels IOException in a
RuntimeException, similar to TunnelException.tunnel(RevWalk::next) in
Guava terms[1].

This should make the code a little more readable.  No functional
change intended.

[1] https://github.com/google/guava/issues/2828#issuecomment-304187823

Change-Id: I97c062d03a17663d5c40895fd3d2c6a7306d4f39
Signed-off-by: Jonathan Nieder <jrn@google.com>
This commit is contained in:
Jonathan Nieder 2018-11-08 18:11:57 -08:00
parent aeba003200
commit a0cd400c37
1 changed files with 21 additions and 13 deletions

View File

@ -54,6 +54,7 @@
import java.util.List;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.LargeObjectException;
@ -1335,6 +1336,22 @@ public void dispose() {
shallowCommitsInitialized = false;
}
/**
* Like {@link #next()}, but if a checked exception is thrown during the
* walk it is rethrown as a {@link RevWalkException}.
*
* @throws RevWalkException if an {@link IOException} was thrown.
* @return next most recent commit; null if traversal is over.
*/
@Nullable
private RevCommit nextForIterator() {
try {
return next();
} catch (IOException e) {
throw new RevWalkException(e);
}
}
/**
* {@inheritDoc}
* <p>
@ -1353,12 +1370,7 @@ public void dispose() {
*/
@Override
public Iterator<RevCommit> iterator() {
final RevCommit first;
try {
first = RevWalk.this.next();
} catch (IOException e) {
throw new RevWalkException(e);
}
RevCommit first = nextForIterator();
return new Iterator<RevCommit>() {
RevCommit next = first;
@ -1370,13 +1382,9 @@ public boolean hasNext() {
@Override
public RevCommit next() {
try {
final RevCommit r = next;
next = RevWalk.this.next();
return r;
} catch (IOException e) {
throw new RevWalkException(e);
}
RevCommit r = next;
next = nextForIterator();
return r;
}
@Override