Java Reference
In-Depth Information
•
RecordFilter
: An instance is responsible for selecting which records should be
included in the enumeration.
•
RecordComparator
: An instance is responsible for comparing two records; the record
store enumerator uses this to determine the sort order of the returned records.
•
boolean
: When
true
,
boolean
indicates that the enumerator should remain current
during enumeration to any changes in the record store.
The method returns an instance of
RecordEnumeration
, upon which you can invoke
nextRecord
repeatedly to obtain subsequent records sorted in the order dictated by the
RecordComparator
you provided (if any). You can also invoke
previousRecord
to obtain the
record previously returned by
nextRecord
(or the last record of the enumeration if you
have not obtained any records using
nextRecord
).
The
RecordFilter
interface your record filter must match has a single method,
match
,
which takes an array of bytes (a record from the record store in its serialized form) and
returns
true
if the record matches the criteria you define. The
RecordComparator
interface
defines the
compare
method, which takes two arrays of bytes (each a record from the
record store in its serialized form) and returns one of
RecordComparator.EQUIVALENT
,
RecordComparator.FOLLOWS
, or
RecordComparator.PRECEDES
, depending on whether the first
record is equivalent to, comes after, or comes before the second interface. Listing 6-4
shows a purely fictitious example.
Listing 6-4.
Filtering Record Store Entries with an Enumeration
RecordStore rs = RecordStore.openRecordStore( store, true );
RecordEnumeration re = rs.enumerateRecords(
new RecordFilter() {
public boolean match(byte[] r) {
return true;
}
},
new RecordComparator() {
public int compare( byte[] r1. byte[] r2) {
return RecordComparator.EQUIVALENT;
}, false);
As intuition would dictate, this
isn't
the best way to traverse all records if order is
unimportant. Instead, the most efficient use of
enumerateRecords
is to invoke it without a
filter or comparator (just pass
null
). This returns an enumeration that returns each ele-
ment of the store in an undefined sequence. If you need to visit all records of the store in
a specific order, specify a comparison function but no filter; if you'd like to filter out some
criteria but order is unimportant, specify a record filter but no record comparator.