]> source.dussan.org Git - jgit.git/commitdiff
Add reset() to AbstractTreeIterator API 87/1487/1
authorShawn O. Pearce <spearce@spearce.org>
Wed, 1 Sep 2010 16:20:33 +0000 (09:20 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Wed, 1 Sep 2010 17:19:43 +0000 (10:19 -0700)
This allows callers to force the iterator back to its starting point,
so it can be traversed again.  The default way to do this is to use
back(1) until first() is true, but this isn't very efficient for any
iterator.  All current implementations have better ways to implement
reset without needing to seek backwards.

Change-Id: Ia26e6c852fdac8a0e9c80ac72c8cca9d897463f4
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheIterator.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/AbstractTreeIterator.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/CanonicalTreeParser.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/EmptyTreeIterator.java
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java

index b4e2d2c2ddf691ca4eea87716d4e542ae3570be3..aa8d9fb85d217779a6c434b39220df24ef6f8318 100644 (file)
@@ -159,6 +159,15 @@ public class DirCacheIterator extends AbstractTreeIterator {
                return 0;
        }
 
+       @Override
+       public void reset() {
+               if (!first()) {
+                       ptr = treeStart;
+                       if (!eof())
+                               parseEntry();
+               }
+       }
+
        @Override
        public boolean first() {
                return ptr == treeStart;
index a54b3e9cfa5d18adda2f981a8d987eacaa4d784e..eee62c63a5ab350080bf3e5c0da529884f981893 100644 (file)
@@ -496,6 +496,22 @@ public abstract class AbstractTreeIterator {
                return createSubtreeIterator(reader);
        }
 
+       /**
+        * Position this iterator on the first entry.
+        *
+        * The default implementation of this method uses {@code back(1)} until
+        * {@code first()} is true. This is most likely not the most efficient
+        * method of repositioning the iterator to its first entry, so subclasses
+        * are strongly encouraged to override the method.
+        *
+        * @throws CorruptObjectException
+        *             the tree is invalid.
+        */
+       public void reset() throws CorruptObjectException {
+               while (!first())
+                       back(1);
+       }
+
        /**
         * Is this tree iterator positioned on its first entry?
         * <p>
index 8e4094a05528aa750def981e1de79c682cc7e171..01b8274253bd8e13c78dcb001ceafcd6e64e57f4 100644 (file)
@@ -243,6 +243,12 @@ public class CanonicalTreeParser extends AbstractTreeIterator {
                return nextPtr - Constants.OBJECT_ID_LENGTH;
        }
 
+       @Override
+       public void reset() {
+               if (!first())
+                       reset(raw);
+       }
+
        @Override
        public boolean first() {
                return currPtr == 0;
index 7d4ee6d2bd3fb32e6b94352c70cc0abbd2277e13..49d75871e8438f48f24044c0588e8f2973e4ae3c 100644 (file)
@@ -107,6 +107,11 @@ public class EmptyTreeIterator extends AbstractTreeIterator {
                return 0;
        }
 
+       @Override
+       public void reset() {
+               // Do nothing.
+       }
+
        @Override
        public boolean first() {
                return true;
index 5256e8aae9855520ee3708c665a10a00f91f7911..51c3483699a7709e85bc4138d74a50a0a1950200 100644 (file)
@@ -332,6 +332,15 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator {
                return 0;
        }
 
+       @Override
+       public void reset() {
+               if (!first()) {
+                       ptr = 0;
+                       if (!eof())
+                               parseEntry();
+               }
+       }
+
        @Override
        public boolean first() {
                return ptr == 0;