Java Reference
In-Depth Information
RecordStore travelList = RecordStore.open ("travelList", true);
RecordEnumeration enumeration = travelList.enumerateRecords
(null, new JourneyDateComparator(), false);
while (enumeration.hasNext()) {
Journey journey = new Journey (enumeration.next())
// place real operations here
}
enumeration.destroy()
travelList.closeRecordStore();
You can also add methods for comparing the destination (or distance), allowing you to iterate the
records ordered correspondingly.
Table 5.4
shows a general overview of the methods of the
RecordEnumeration
class.
Table 5
.
4. Methods of
RecordEnumeration
Name
Purpose
void destroy()
Releases system resources associated with this
enumeration.
boolean
hasNextElement()
Indicates whether records are left in the enumeration.
boolean
hasPreviousElement()
Analogous to
hasNextElement()
, but in the inverse
direction.
boolean isKeptUpdated()
Indicates whether the enumeration is kept updated.
void keepUpdated
(boolean keepUpd)
Sets or resets the automatic update mode.
byte[] nextRecord()
Returns the next record.
int nextRecordId()
Returns the ID of the next record.
int numRecords()
Returns the number of records in this enumeration. Differs
from
RecordStore.getNumRecords()
for filtered or
outdated enumerations only.
byte[] previousRecord()
Returns the previous record.
int previousRecordId()
Returns the ID of the previous record.
void rebuild()
Rebuilds the enumeration to reflect all changes. Performed
automatically in the updated mode.
void reset()
Resets the enumeration to the first record.
Filtered Record Enumerations
You have already seen that the
enumerateRecords()
method has a filter parameter, but we have
not explained how to use it. The corresponding
RecordFilter
interface works similarly to the
RecordComparator
interface. It provides a single method,
matches()
, which takes a record byte
array as input and returns a
boolean
value, determining whether the given record passes the filter.
For example, if you need an enumeration of the journeys where the distance was greater than x
kilometers, you can implement it as follows:
Class MinDistanceFilter {
int min;
DistanceFilter (int min) {
this.min = min;
}
boolean matches (byte[] record) {
return new Journey (record).getDistance() >= min;
}
Search WWH ::
Custom Search