Browse Source

Merge changes I97c062d0,Ib4e1f37c

* changes:
  Simplify RevWalk#iterator by factoring out common code
  Simplify exception handling in RevWalk#iterator
stable-5.2
David Pursehouse 6 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
54dd1d112a
  1. 42
      org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java

42
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java

@ -54,6 +54,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.LargeObjectException; import org.eclipse.jgit.errors.LargeObjectException;
@ -1335,6 +1336,22 @@ public class RevWalk implements Iterable<RevCommit>, AutoCloseable {
shallowCommitsInitialized = false; 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} * {@inheritDoc}
* <p> * <p>
@ -1353,16 +1370,7 @@ public class RevWalk implements Iterable<RevCommit>, AutoCloseable {
*/ */
@Override @Override
public Iterator<RevCommit> iterator() { public Iterator<RevCommit> iterator() {
final RevCommit first; RevCommit first = nextForIterator();
try {
first = RevWalk.this.next();
} catch (MissingObjectException e) {
throw new RevWalkException(e);
} catch (IncorrectObjectTypeException e) {
throw new RevWalkException(e);
} catch (IOException e) {
throw new RevWalkException(e);
}
return new Iterator<RevCommit>() { return new Iterator<RevCommit>() {
RevCommit next = first; RevCommit next = first;
@ -1374,17 +1382,9 @@ public class RevWalk implements Iterable<RevCommit>, AutoCloseable {
@Override @Override
public RevCommit next() { public RevCommit next() {
try { RevCommit r = next;
final RevCommit r = next; next = nextForIterator();
next = RevWalk.this.next(); return r;
return r;
} catch (MissingObjectException e) {
throw new RevWalkException(e);
} catch (IncorrectObjectTypeException e) {
throw new RevWalkException(e);
} catch (IOException e) {
throw new RevWalkException(e);
}
} }
@Override @Override

Loading…
Cancel
Save