Java Reference
In-Depth Information
// Open a RecordStore rs
// Create a RecordFilter rf
// Create a RecordComparator rc
RecordEnumeration re = rs.enumerateRecords(rf, rc, false);
while (re.hasNextElement()) {
byte[] recordBytes = re.nextRecord();
// Process the retrieved bytes.
}
The
RecordFilter
and
RecordComparator
can both be
null
, in which case the
RecordEnumeration
will iterate through every record in the record store. The
Preferences
class uses
RecordEnumeration
in this way.
■
Note
RecordEnumeration
makes no guarantees about the order of the returned records if the
RecordComparator
is
null
.
As you're moving through the selected records, you can also move backward.
RecordEnumeration
includes
hasPreviousElement()
,
previousRecord()
, and
previousRecordId()
methods that work just like their
next
counterparts.
Four out of the five ways to move the current position in the
RecordEnumeration
are the
nextRecord()
,
nextRecordId()
,
previousRecord()
, and
previousRecordId()
methods. The fifth
method is kind of like a rewind button:
reset()
moves the record pointer back to the very
beginning of the selected records.
When you're finished using a
RecordEnumeration
, you should release its resources. You can
do this by calling
destroy()
, after which the
RecordEnumeration
is no longer usable.
Keeping a RecordEnumeration Up-to-Date
In a multithreaded environment, it's entirely possible that a
RecordStore
will change at the
same time you're iterating through a
RecordEnumeration
for the same
RecordStore
. There are
two ways to deal with this.
The first thing you can do is call
rebuild()
, which explicitly rebuilds the
RecordEnumeration
based on the
RecordFilter
and
RecordComparator
you originally specified.
The other possibility is to request a
RecordEnumeration
that is automatically updated with
any changes to the underlying
RecordStore
. You can do this by passing
true
for the
keepUpdated
parameter of
RecordStore
's
enumerateRecords()
method. You can find out if the
RecordEnumeration
is automatically updated by calling
isKeptUpdated()
. Furthermore, you can change its state by
calling
keepUpdated()
.
Automatically updated
RecordEnumerations
typically register themselves as
RecordListeners
with the underlying
RecordStore
. Each time the
RecordStore
is changed, the
RecordEnumeration
is rebuilt. Keep in mind that this is an expensive operation (in terms of time), so if there are
many
RecordStore
changes, you'll be paying a price for it.
Search WWH ::
Custom Search