diff options
author | Jonathan Nieder <jrn@google.com> | 2018-11-08 18:11:57 -0800 |
---|---|---|
committer | Jonathan Nieder <jrn@google.com> | 2018-11-08 18:19:01 -0800 |
commit | a0cd400c37bb861264fa2147f94123c6f0e19b7c (patch) | |
tree | d9746e37221737b14dd9d367d5122d566c429e12 | |
parent | aeba0032003480dd821edc22fa4ab1a66192549f (diff) | |
download | jgit-a0cd400c37bb861264fa2147f94123c6f0e19b7c.tar.gz jgit-a0cd400c37bb861264fa2147f94123c6f0e19b7c.zip |
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>
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java index a42721afaf..400ea33c21 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java @@ -54,6 +54,7 @@ import java.util.Iterator; 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; @@ -1336,6 +1337,22 @@ public class RevWalk implements Iterable<RevCommit>, AutoCloseable { } /** + * 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> * Returns an Iterator over the commits of this walker. @@ -1353,12 +1370,7 @@ public class RevWalk implements Iterable<RevCommit>, AutoCloseable { */ @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 class RevWalk implements Iterable<RevCommit>, AutoCloseable { @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 |