]> source.dussan.org Git - jgit.git/commitdiff
Simplify RevWalk#iterator by factoring out common code 61/132161/2
authorJonathan Nieder <jrn@google.com>
Fri, 9 Nov 2018 02:11:57 +0000 (18:11 -0800)
committerJonathan Nieder <jrn@google.com>
Fri, 9 Nov 2018 02:19:01 +0000 (18:19 -0800)
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>
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java

index a42721afafe37f9af1d3c66139c4eb1a5aa5d1ff..400ea33c21babe3fc9aab8dbba4e7d8fb8be95f6 100644 (file)
@@ -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<RevCommit>, 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}
         * <p>
@@ -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