提交 d5547874 authored 作者: Andrei Tokar's avatar Andrei Tokar

throw NoSuchElementException instead of returning null in unchecked Iterator::next implementations

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