Class StoredIterator<E>
- All Implemented Interfaces:
Cloneable,Iterator<E>,ListIterator<E>
While in general this class conforms to the Iterator interface,
it is important to note that all iterators for stored collections must be
explicitly closed with close(). The static method close(java.util.Iterator) allows calling close for all iterators without
harm to iterators that are not from stored collections, and also avoids
casting. If a stored iterator is not closed, unpredictable behavior
including process death may result.
This class implements the Iterator interface for all stored
iterators. It also implements ListIterator because some list
iterator methods apply to all stored iterators, for example, previous() and hasPrevious(). Other list iterator methods are always
supported for lists, but for other types of collections are only supported
under certain conditions. See nextIndex(), previousIndex(),
add(E) and set(E) for details.
In addition, this class provides the following methods for stored collection iterators only. Note that the use of these methods is not compatible with the standard Java collections interface.
-
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element into the list or inserts a duplicate into other types of collections (optional operation).voidclose()Closes this iterator.static voidCloses the given iterator usingclose()if it is aStoredIterator.intcount()Returns the number of elements having the same key value as the key value of the element last returned by next() or previous().final StoredCollection<E> Returns the collection associated with this iterator.booleanhasNext()Returns true if this iterator has more elements when traversing in the forward direction.booleanReturns true if this iterator has more elements when traversing in the reverse direction.final booleanReturns whether write-locks will be obtained when reading with this cursor.next()Returns the next element in the iteration.intReturns the index of the element that would be returned by a subsequent call to next.previous()Returns the next element in the iteration.intReturns the index of the element that would be returned by a subsequent call to previous.voidremove()Removes the last element that was returned by next or previous (optional operation).voidReplaces the last element returned by next or previous with the specified element (optional operation).voidsetReadModifyWrite(boolean lockForWrite) Changes whether write-locks will be obtained when reading with this cursor.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Method Details
-
close
Closes the given iterator usingclose()if it is aStoredIterator. If the given iterator is not aStoredIterator, this method does nothing.- Parameters:
i- is the iterator to close.- Throws:
RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C edition).
-
isReadModifyWrite
public final boolean isReadModifyWrite()Returns whether write-locks will be obtained when reading with this cursor. Obtaining write-locks can prevent deadlocks when reading and then modifying data.- Returns:
- the write-lock setting.
-
setReadModifyWrite
public void setReadModifyWrite(boolean lockForWrite) Changes whether write-locks will be obtained when reading with this cursor. Obtaining write-locks can prevent deadlocks when reading and then modifying data.- Parameters:
lockForWrite- the write-lock setting.
-
hasNext
public boolean hasNext()Returns true if this iterator has more elements when traversing in the forward direction. False is returned if the iterator has been closed. This method conforms to theIterator.hasNext()interface.- Specified by:
hasNextin interfaceIterator<E>- Specified by:
hasNextin interfaceListIterator<E>- Returns:
- whether
next()will succeed. - Throws:
RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C edition).
-
hasPrevious
public boolean hasPrevious()Returns true if this iterator has more elements when traversing in the reverse direction. It returns false if the iterator has been closed. This method conforms to theListIterator.hasPrevious()interface.- Specified by:
hasPreviousin interfaceListIterator<E>- Returns:
- whether
previous()will succeed. - Throws:
RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C edition).
-
next
Returns the next element in the iteration. This method conforms to theIterator.next()interface.- Specified by:
nextin interfaceIterator<E>- Specified by:
nextin interfaceListIterator<E>- Returns:
- the next element.
- Throws:
RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
previous
Returns the next element in the iteration. This method conforms to theListIterator.previous()interface.- Specified by:
previousin interfaceListIterator<E>- Returns:
- the previous element.
- Throws:
RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
nextIndex
public int nextIndex()Returns the index of the element that would be returned by a subsequent call to next. This method conforms to theListIterator.nextIndex()interface except that it returns Integer.MAX_VALUE for stored lists when positioned at the end of the list, rather than returning the list size as specified by the ListIterator interface. This is because the database size is not available.- Specified by:
nextIndexin interfaceListIterator<E>- Returns:
- the next index.
- Throws:
UnsupportedOperationException- if this iterator's collection does not use record number keys.RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
previousIndex
public int previousIndex()Returns the index of the element that would be returned by a subsequent call to previous. This method conforms to theListIterator.previousIndex()interface.- Specified by:
previousIndexin interfaceListIterator<E>- Returns:
- the previous index.
- Throws:
UnsupportedOperationException- if this iterator's collection does not use record number keys.RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
set
Replaces the last element returned by next or previous with the specified element (optional operation). This method conforms to theListIterator.set(E)interface.In order to call this method, if the underlying Database is transactional then a transaction must be active when creating the iterator.
- Specified by:
setin interfaceListIterator<E>- Parameters:
value- the new value.- Throws:
UnsupportedOperationException- if the collection is aStoredKeySet(the set returned byMap.keySet()), or if duplicates are sorted since this would change the iterator position, or if the collection is indexed, or if the collection is read-only.IllegalArgumentException- if an entity value binding is used and the primary key of the value given is different than the existing stored primary key.RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
remove
public void remove()Removes the last element that was returned by next or previous (optional operation). This method conforms to theListIterator.remove()interface except that when the collection is a list and the RECNO-RENUMBER access method is not used, list indices will not be renumbered.In order to call this method, if the underlying Database is transactional then a transaction must be active when creating the iterator.
Note that for the JE product, RECNO-RENUMBER databases are not supported, and therefore list indices are never renumbered by this method.
- Specified by:
removein interfaceIterator<E>- Specified by:
removein interfaceListIterator<E>- Throws:
UnsupportedOperationException- if the collection is a sublist, or if the collection is read-only.RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
add
Inserts the specified element into the list or inserts a duplicate into other types of collections (optional operation). This method conforms to theListIterator.add(E)interface when the collection is a list and the RECNO-RENUMBER access method is used. Otherwise, this method may only be called when duplicates are allowed. If duplicates are unsorted, the new value will be inserted in the same manner as list elements. If duplicates are sorted, the new value will be inserted in sort order.Note that for the JE product, RECNO-RENUMBER databases are not supported, and therefore this method may only be used to add duplicates.
- Specified by:
addin interfaceListIterator<E>- Parameters:
value- the new value.- Throws:
UnsupportedOperationException- if the collection is a sublist, or if the collection is indexed, or if the collection is read-only, or if the collection is a list and the RECNO-RENUMBER access method was not used, or if the collection is not a list and duplicates are not allowed.IllegalStateException- if the collection is empty and is not a list with RECNO-RENUMBER access.IllegalArgumentException- if a duplicate value is being added that already exists and duplicates are sorted.RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
count
public int count()Returns the number of elements having the same key value as the key value of the element last returned by next() or previous(). If no duplicates are allowed, 1 is always returned. This method does not exist in the standardIteratororListIteratorinterfaces.- Returns:
- the number of duplicates.
- Throws:
IllegalStateException- if next() or previous() has not been called for this iterator, or if remove() or add() were called after the last call to next() or previous().
-
close
public void close()Closes this iterator. This method does not exist in the standardIteratororListIteratorinterfaces.After being closed, only the
hasNext()andhasPrevious()methods may be called and these will return false.close()may also be called again and will do nothing. If other methods are called aNullPointerExceptionwill generally be thrown.- Throws:
RuntimeExceptionWrapper- if a checked exception is thrown, including aDatabaseExceptionon BDB (C Edition).
-
getCollection
Returns the collection associated with this iterator. This method does not exist in the standardIteratororListIteratorinterfaces.- Returns:
- the collection associated with this iterator.
-