From: James Ahlborn Date: Sun, 7 Apr 2013 00:54:19 +0000 (+0000) Subject: make iterator with no reset return current row first X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=88c35857e592ef00968af414d6bda1a8b47702b4;p=jackcess.git make iterator with no reset return current row first git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jackcess-2@703 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java b/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java index b0c228a..06cda47 100644 --- a/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java +++ b/src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java @@ -675,6 +675,14 @@ public abstract class CursorImpl implements Cursor return _rowState.isUpToDate(); } + /** + * Returns {@code true} of the current row is valid, {@code false} otherwise. + */ + protected boolean isCurrentRowValid() throws IOException { + return(_curPos.getRowId().isValid() && !isCurrentRowDeleted() && + !isBeforeFirst() && !isAfterLast()); + } + @Override public String toString() { return getClass().getSimpleName() + " CurPosition " + _curPos + @@ -717,9 +725,15 @@ public abstract class CursorImpl implements Cursor _columnNames = columnNames; _moveForward = moveForward; _colMatcher = ((columnMatcher != null) ? columnMatcher : _columnMatcher); - if(reset) { - reset(_moveForward); - } + try { + if(reset) { + reset(_moveForward); + } else if(isCurrentRowValid()) { + _hasNext = _validRow = true; + } + } catch(IOException e) { + throw new RuntimeIOException(e); + } } public boolean hasNext() { diff --git a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java index 0a5e551..59de129 100644 --- a/test/src/java/com/healthmarketscience/jackcess/CursorTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/CursorTest.java @@ -287,6 +287,49 @@ public class CursorTest extends TestCase { assertEquals(expectedRow, cursor.getCurrentRow()); } + public void testMoveNoReset() throws Exception { + for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { + Database db = createTestTable(fileFormat); + + Table table = db.getTable("test"); + Cursor cursor = CursorBuilder.createCursor(table); + doTestMoveNoReset(cursor); + + db.close(); + } + } + + private static void doTestMoveNoReset(Cursor cursor) + throws Exception + { + List> expectedRows = createTestTableData(); + List> foundRows = new ArrayList>(); + + Iterator iter = cursor.newIterable().iterator(); + + for(int i = 0; i < 6; ++i) { + foundRows.add(iter.next()); + } + + iter = cursor.newIterable().reset(false).reverse().iterator(); + iter.next(); + Map row = iter.next(); + assertEquals(expectedRows.get(4), row); + + iter = cursor.newIterable().reset(false).iterator(); + iter.next(); + row = iter.next(); + assertEquals(expectedRows.get(5), row); + iter.next(); + + iter = cursor.newIterable().reset(false).iterator(); + for(int i = 6; i < 10; ++i) { + foundRows.add(iter.next()); + } + + assertEquals(expectedRows, foundRows); + } + public void testSearch() throws Exception { for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) { Database db = createTestTable(fileFormat);