From: Jonathan Nieder Date: Fri, 9 Nov 2018 02:11:57 +0000 (-0800) Subject: Simplify RevWalk#iterator by factoring out common code X-Git-Tag: v5.2.0.201811281532-m3~37^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a0cd400c37bb861264fa2147f94123c6f0e19b7c;p=jgit.git 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 --- 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; @@ -1335,6 +1336,22 @@ public class RevWalk implements Iterable, AutoCloseable { 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} *

@@ -1353,12 +1370,7 @@ public class RevWalk implements Iterable, AutoCloseable { */ @Override public Iterator iterator() { - final RevCommit first; - try { - first = RevWalk.this.next(); - } catch (IOException e) { - throw new RevWalkException(e); - } + RevCommit first = nextForIterator(); return new Iterator() { RevCommit next = first; @@ -1370,13 +1382,9 @@ public class RevWalk implements Iterable, 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