Java Reference
In-Depth Information
The following simple implementation compares each byte of the given records and sorts
them numerically. If the two records have the same data, up to the length of the shorter one,
then they are deemed EQUIVALENT .
public class SimpleComparator
implements javax.microedition.rms.RecordComparator {
public int compare(byte[] rec1, byte[] rec2) {
int limit = Math.min(rec1.length, rec2.length);
for (int index = 0; index < limit; index++) {
if (rec1[index] < rec2[index])
return PRECEDES;
else if (rec1[index] > rec2[index])
return FOLLOWS;
}
return EQUIVALENT;
}
}
Working with RecordEnumeration
RecordStore 's enumerateRecords() method returns an implementation of the RecordEnumeration
interface. RecordEnumeration is surprisingly complicated. Its basic function is to allow you to
iterate through the records retrieved from the RecordStore . Unlike a regular J2SE Enumeration
or Iterator , however, RecordEnumeration allows you to scroll both forward and backward through
its contents. In addition, you can peek at the next or previous record ID. Finally, RecordEnumeration
offers the possibility of keeping its data synchronized with the actual RecordStore . Behind
the scenes, this can be accomplished by registering the RecordEnumeration as a listener for
RecordStore changes.
The basic operation of RecordEnumeration is to iterate through a set of records. You can
find out if there's a next record by calling hasNextElement() . If the next record exists, you can
retrieve its data by calling the following method:
public byte[] nextRecord()
throws InvalidRecordIDException,
RecordStoreNotOpenException,
RecordStoreException
Alternately, you can retrieve the next record's ID by calling this method:
public int nextRecordId() throws InvalidRecordIDException
You can't really have your cake and eat it, though; both nextRecord() and nextRecordId()
advance the RecordEnumeration to the next record. If you want to retrieve both the ID and the
data for the next record, you'd need to call nextRecordId() and then retrieve the record data
directly from the RecordStore .
A typical use of RecordEnumeration would be to walk straight through the selected records,
like this:
Search WWH ::




Custom Search