Unverified 提交 498e99ba authored 作者: Andrei Tokar's avatar Andrei Tokar 提交者: GitHub

Merge pull request #1340 from h2database/no-such-element

NoSuchElementException instead of returning null
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
package org.h2.mvstore; package org.h2.mvstore;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException;
/** /**
* A cursor to iterate over elements in ascending order. * A cursor to iterate over elements in ascending order.
...@@ -79,7 +80,7 @@ public class Cursor<K, V> implements Iterator<K> { ...@@ -79,7 +80,7 @@ public class Cursor<K, V> implements Iterator<K> {
@Override @Override
public K next() { public K next() {
if(!hasNext()) { if(!hasNext()) {
return null; throw new NoSuchElementException();
} }
current = null; current = null;
return last; return last;
......
...@@ -362,7 +362,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -362,7 +362,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
@Override @Override
public boolean next() { public boolean next() {
current = it.next(); current = it.hasNext() ? it.next() : null;
searchRow = null; searchRow = null;
row = null; row = null;
return current != null; return current != null;
...@@ -374,6 +374,5 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -374,6 +374,5 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
} }
} }
} }
...@@ -15,6 +15,7 @@ import java.util.AbstractMap; ...@@ -15,6 +15,7 @@ import java.util.AbstractMap;
import java.util.BitSet; import java.util.BitSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
/** /**
* A map that supports transactions. * A map that supports transactions.
...@@ -636,6 +637,7 @@ public class TransactionMap<K, V> { ...@@ -636,6 +637,7 @@ public class TransactionMap<K, V> {
this.committingTransactions = committingTransactions; this.committingTransactions = committingTransactions;
this.includeAllUncommitted = includeAllUncommitted; this.includeAllUncommitted = includeAllUncommitted;
fetchNext();
} }
protected abstract X registerCurrent(K key, VersionedValue data); protected abstract X registerCurrent(K key, VersionedValue data);
...@@ -672,19 +674,16 @@ public class TransactionMap<K, V> { ...@@ -672,19 +674,16 @@ public class TransactionMap<K, V> {
@Override @Override
public final boolean hasNext() { public final boolean hasNext() {
if(current == null) {
fetchNext();
}
return current != null; return current != null;
} }
@Override @Override
public final X next() { public final X next() {
if(!hasNext()) { if(current == null) {
return null; throw new NoSuchElementException();
} }
X result = current; X result = current;
current = null; fetchNext();
return result; return result;
} }
......
...@@ -12,6 +12,7 @@ import java.nio.charset.StandardCharsets; ...@@ -12,6 +12,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Random; import java.util.Random;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -1977,7 +1978,7 @@ public class TestMVStore extends TestBase { ...@@ -1977,7 +1978,7 @@ public class TestMVStore extends TestBase {
assertEquals(i, it.next().intValue()); assertEquals(i, it.next().intValue());
} }
assertFalse(it.hasNext()); assertFalse(it.hasNext());
assertNull(it.next()); assertThrows(NoSuchElementException.class, it).next();
for (int j = 0; j < 10; j++) { for (int j = 0; j < 10; j++) {
it = m.keyIterator(j); it = m.keyIterator(j);
for (int i = j; i < 10; i++) { for (int i = j; i < 10; i++) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论