Upload
muthusvm
View
1.448
Download
1
Embed Size (px)
DESCRIPTION
J2ME Record Management System
Citation preview
Outline-session 9 (09-April-2009)
>> J2ME Record Management System-Notification of Changes with Record Listener-Exception Handling
Sorting with Record Comparator• RecordComparator is a Java interface• implement this interface when you would like the
enumerator to return records in sorted orderImplementation:public class Comparator implements RecordComparator{public int compare(byte[] rec1, byte[] rec2){String str1 = new String(rec1), str2 = new String(rec2);int result = str1.compareTo(str2);if (result == 0)return RecordComparator.EQUIVALENT;else if (result < 0)return RecordComparator.PRECEDES;elsereturn RecordComparator.FOLLOWS;}}
Sorting with Record Comparator
// Create a new comparator for sortingComparator comp = new Comparator();// Reference the comparator when creating the result setRecordEnumeration re = rs.enumerateRecords(null,comp,false);// Iterate through the sorted resultswhile (re.hasNextElement()){String str = new String(re.nextRecord());...}
Record Comparator API
String Sort with Compound Records
• When a record has multiple data types stored within it (what I'll refer to as a compound record), there may be more than one field that would be appropriate for sorting
String[] pets = {"duke", "tiger", "spike", "beauregard"};boolean[] dog = {true, false, true, true};
int[] rank = {3, 0, 1, 2};• StringSort.java• IntSort.java
Searching with RecordFilter
• an enumerator can filter records• When using RecordComparator all records in a record store
are in the result set. With a RecordFilter, only those records that match the filter criteria will become part of the enumerator result set.
Searching with RecordFilterclass SearchFilter implements RecordFilter{private String searchText = null;public SearchFilter(String searchText)304{// This is the text to search forthis.searchText = searchText.toLowerCase();}public boolean matches(byte[] candidate){String str = new String(candidate).toLowerCase();// Look for a matchif (searchText != null && str.indexOf(searchText) != -1)return true;elsereturn false;}}
Searching with RecordFilter
// Create a new search filterSearchFilter search = new SearchFilter("search text");// Reference the filter when creating the result setRecordEnumeration re =rs.enumerateRecords(search,null,false);// If there is at least one record in result set, a matchwas foundif (re.numRecords() > 0)// Do something
RecordFilter -API
• RecordFilter API• Table 11.6. RecordFilter Interface:
javax.microedition.rms.RecordFilter• Method Description• boolean matches(byte[] candidate) Search a
record for a specific value