blob: d592983253eddc0878c5cd124b5c8b567de6202a [file] [log] [blame]
/*-
* Copyright (C) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
*
* This file was distributed by Oracle as part of a version of Oracle Berkeley
* DB Java Edition made available at:
*
* http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html
*
* Please see the LICENSE file included in the top-level directory of the
* appropriate version of Oracle Berkeley DB Java Edition for a copy of the
* license and additional information.
*/
package com.sleepycat.persist;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.LockMode;
import com.sleepycat.util.RuntimeExceptionWrapper;
/**
* Implements Iterator for an arbitrary EntityCursor.
*
* @author Mark Hayes
*/
class BasicIterator<V> implements Iterator<V> {
private EntityCursor<V> entityCursor;
private ForwardCursor<V> forwardCursor;
private LockMode lockMode;
private V nextValue;
/**
* An EntityCursor is given and the remove() method is supported.
*/
BasicIterator(EntityCursor<V> entityCursor, LockMode lockMode) {
this.entityCursor = entityCursor;
this.forwardCursor = entityCursor;
this.lockMode = lockMode;
}
/**
* A ForwardCursor is given and the remove() method is not supported.
*/
BasicIterator(ForwardCursor<V> forwardCursor, LockMode lockMode) {
this.forwardCursor = forwardCursor;
this.lockMode = lockMode;
}
public boolean hasNext() {
if (nextValue == null) {
try {
nextValue = forwardCursor.next(lockMode);
} catch (DatabaseException e) {
throw RuntimeExceptionWrapper.wrapIfNeeded(e);
}
return nextValue != null;
} else {
return true;
}
}
public V next() {
if (hasNext()) {
V v = nextValue;
nextValue = null;
return v;
} else {
throw new NoSuchElementException();
}
}
public void remove() {
if (entityCursor == null) {
throw new UnsupportedOperationException();
}
try {
if (!entityCursor.delete()) {
throw new IllegalStateException
("Record at cursor position is already deleted");
}
} catch (DatabaseException e) {
throw RuntimeExceptionWrapper.wrapIfNeeded(e);
}
}
}