Interface RecordFilter

Interface RecordFilter
• javax.microedition.rms
• public interface RecordFilter

Interface RecordFilter
• 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
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
• Record Searching(Filtering) is performed by
using interface RecordFilter interface.
• RecordFilter interface requires two
parameters: (1) match() (2) filterclose()

Interface RecordFilter
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
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
• 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
filterClose()
• It is used to free the resources

Interface RecordFilter
•
•
•
•
•
•
•
•
•
•
•
•
•

//searching program and exception handling
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class SearchEx extends MIDlet implements CommandListener
{
private Display d;
private Alert a;
private Form f;
private Command exit;
private Command start;
private RecordStore rec = null;

•

private RecordEnumeration r = null;

•
•
•
•
•
•
•
•
•
•
•
•
•
•

private Filter fil = null;
private boolean b;
public SearchEx ()
{
d = Display.getDisplay(this);
b=false;
exit = new Command("Exit", Command.SCREEN, 1);
start = new Command("Start", Command.SCREEN, 1);
f = new Form("Mixed RecordEnumeration", null);
f.addCommand(exit);
f.addCommand(start);
f.setCommandListener(this);
}

Interface RecordFilter
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void startApp()
{
d.setCurrent(f);
}
public void pauseApp()
{
}
public void destroyApp( boolean un ) throws MIDletStateChangeException
{
if(un==false)
{
throw new MIDletStateChangeException();
}
}
Interface RecordFilter
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void commandAction(Command c, Displayable dd)
{
if (c == exit)
{
try
{
if(b==false)
{
StringItem s=new StringItem(null,"please press exit once again");
f.append(s);
destroyApp(false);
}
else
{
destroyApp(true);
notifyDestroyed();
}
}//end of try
catch(MIDletStateChangeException e)
{
b=true;
}
}

Interface RecordFilter
•
•
•
•
•
•
•
•
•
•
•
•
•
•

else if (c == start)
{
try
{
rec = RecordStore.openRecordStore(
"myRecordStore", true );
}
catch (Exception error)
{
a = new Alert("Error Creating",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
Interface RecordFilter
•
•

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
•
•
•
•
•
•

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
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

try
{
rec.closeRecordStore();
}
catch (Exception error)
{
a = new Alert("Error Closing",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
if (RecordStore.listRecordStores() != null)
{
try
{
RecordStore.deleteRecordStore("myRecordStore");
r.destroy();
fil.filterClose();
}
catch (Exception error)
{
a = new Alert("Error Removing",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
}
}
}
}

Interface RecordFilter
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
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void filterClose()
{
try
{
if (bai != null)
{
bai.close();
}
if (dis != null)
{
dis.close();
}
}
catch ( Exception error)
{
}
}
}

Interface RecordFilter

Interface Record filter

  • 1.
  • 2.
    • javax.microedition.rms • publicinterface RecordFilter Interface RecordFilter
  • 3.
    • This interfaceis 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 isa 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 Step1 • 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
  • 9.
    filterClose() • It isused to free the resources Interface RecordFilter
  • 10.
    • • • • • • • • • • • • • //searching program andexception handling import javax.microedition.rms.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; public class SearchEx extends MIDlet implements CommandListener { private Display d; private Alert a; private Form f; private Command exit; private Command start; private RecordStore rec = null; • private RecordEnumeration r = null; • • • • • • • • • • • • • • private Filter fil = null; private boolean b; public SearchEx () { d = Display.getDisplay(this); b=false; exit = new Command("Exit", Command.SCREEN, 1); start = new Command("Start", Command.SCREEN, 1); f = new Form("Mixed RecordEnumeration", null); f.addCommand(exit); f.addCommand(start); f.setCommandListener(this); } Interface RecordFilter
  • 11.
    • • • • • • • • • • • • • • public void startApp() { d.setCurrent(f); } publicvoid pauseApp() { } public void destroyApp( boolean un ) throws MIDletStateChangeException { if(un==false) { throw new MIDletStateChangeException(); } } Interface RecordFilter
  • 12.
    • • • • • • • • • • • • • • • • • • • • • • • • public void commandAction(Commandc, Displayable dd) { if (c == exit) { try { if(b==false) { StringItem s=new StringItem(null,"please press exit once again"); f.append(s); destroyApp(false); } else { destroyApp(true); notifyDestroyed(); } }//end of try catch(MIDletStateChangeException e) { b=true; } } Interface RecordFilter
  • 13.
    • • • • • • • • • • • • • • else if (c== start) { try { rec = RecordStore.openRecordStore( "myRecordStore", true ); } catch (Exception error) { a = new Alert("Error Creating", error.toString(), null, AlertType.WARNING); a.setTimeout(Alert.FOREVER); d.setCurrent(a); } 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 = newFilter("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
  • 16.
    • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • try { rec.closeRecordStore(); } catch (Exception error) { a= new Alert("Error Closing", error.toString(), null, AlertType.WARNING); a.setTimeout(Alert.FOREVER); d.setCurrent(a); } if (RecordStore.listRecordStores() != null) { try { RecordStore.deleteRecordStore("myRecordStore"); r.destroy(); fil.filterClose(); } catch (Exception error) { a = new Alert("Error Removing", error.toString(), null, AlertType.WARNING); a.setTimeout(Alert.FOREVER); d.setCurrent(a); } } } } } Interface RecordFilter
  • 17.
    RecordFilter • • • //Filter is userdefined 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
  • 18.
    • • • • • • • • • • • • • • • • • • public void filterClose() { try { if(bai != null) { bai.close(); } if (dis != null) { dis.close(); } } catch ( Exception error) { } } } Interface RecordFilter

Editor's Notes

  • #18 http://improvejava.blogspot.in