The document describes the RecordFilter interface in Java ME, which is used for searching or filtering records in a record store. It allows specifying which records should be included during enumeration. The interface requires classes to implement two methods - matches() to check if a record matches the filter criteria, and filterClose() to free resources. The document provides examples of creating a filter class, using it to get a record enumeration, and the full code for a searching application.
3. • This interface is used to searching in the recordstore.
• Searching is also called as ‘Filtering’.
• It specifies which records should be included in
enumeration
• Returns true if the candidate record is selected by the
RecordFilter.
For example:
• RecordFilter f = new DateRecordFilter(); // class
implements RecordFilter if
(f.matches(recordStore.getRecord(theRecordID)) == true)
DoSomethingUseful(theRecordID);
Interface RecordFilter
4. Searching Records:
• Here, if records matches a search criteria are
copied into the RecordEnumeration and
unmatched records are not included in the
RecordEnumeration.
Interface RecordFilter
5. • Record Searching(Filtering) is performed by
using interface RecordFilter interface.
• RecordFilter interface requires two
parameters: (1) match() (2) filterclose()
Interface RecordFilter
6. Match()
• It is a member method of Filter class
• It is used to read columns from the record and
uses logical operators to verify whether they
are matching the search key or not.
• It contains the searching logic
Interface RecordFilter
7. Logic for searching
Step 1
• Create a filter class by implementing RecordFilter
• Filter class should contain the logic for comparing
records and search criteria
Step 2:
• Create an object of Filter class and create a
record enumeration using its object:
ie: MyFilter mf=new MyFilter(“searchKey”);
Rec_enum=rec.enumerateRecords(filter,null,false);
Interface RecordFilter
8. • The enumerateRecords() method calls methods
defined in the Filter class.
Step 3:
• Execute loop record enumeration and copy each record
to a variable.
Step 4:
• Display the data in dialog box
Step 5:
• If any errors occurs display them
Step 6:
• Close and remove RecordEnumeration and record store
Interface RecordFilter
14. •
•
try
{
•
•
•
•
•
•
•
•
•
•
•
•
•
String s[] = {"Sita", "Rama", "Anand"};
for (int x = 0 ; x < 3; x++)
{
byte[] by = s[x].getBytes();
rec.addRecord(by, 0,by.length);
}//x,by are lost
}//s is lost
catch ( Exception error)
{
a = new Alert("Error Writing",error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
Interface RecordFilter
15. •
•
•
•
•
•
try
{
fil = new Filter("Rama");//searching element
r = rec.enumerateRecords(fil, null, false);//Record Enumeration is constructed,search element
//is stored in recordenumeration r
if (r.numRecords() > 0)//It returns no.of records in the RecordEnumeration
•
•
•
•
•
•
•
•
•
•
•
•
•
•
{
String s = new String(r.nextRecord());
a = new Alert("Reading", s,null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
}
catch (Exception error)
{
a = new Alert("Error Reading",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
Interface RecordFilter
17. RecordFilter
•
•
•
//Filter is user defined class
class Filter implements RecordFilter
{
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
private String key = null;
private ByteArrayInputStream bai = null;
private DataInputStream dis = null;
//this constructor is called when enumerateRecords() method is called
public Filter(String key)
{
this.key = key.toLowerCase();
}
//matches() is called automatically after constructor
public boolean matches(byte[] by)
{
String s = new String(by).toLowerCase();//converted into string form frm byte, converted into lowercase
if (s!= null && s.indexOf(key) != -1)//Returns the index within this string of the first
//occurrence of the specified substring.
return true;
else
return false;
}
Interface RecordFilter