summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2013-04-07 00:54:19 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2013-04-07 00:54:19 +0000
commit88c35857e592ef00968af414d6bda1a8b47702b4 (patch)
tree1d2b0c2353ecd13c48ddeeb50599d81d35cefaca
parent260914d657114b4e5c0e52efb30e4984ffe6cdef (diff)
downloadjackcess-88c35857e592ef00968af414d6bda1a8b47702b4.tar.gz
jackcess-88c35857e592ef00968af414d6bda1a8b47702b4.zip
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
-rw-r--r--src/java/com/healthmarketscience/jackcess/impl/CursorImpl.java20
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/CursorTest.java43
2 files changed, 60 insertions, 3 deletions
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<Map<String, Object>> expectedRows = createTestTableData();
+ List<Map<String, Object>> foundRows = new ArrayList<Map<String, Object>>();
+
+ Iterator<Row> 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<String, Object> 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);