Record sorting in RMS
Article Metadata
Sorting
RecordEnumeration is a class that allows forward and backward movement through a record store. Moving through the store requires nothing more than a check to see if there is another record available (in either direction), and if so, requesting its data.
RecordComparator is a Java interface. You implement this interface when you would like the enumeration to return records in sorted order. There is only one method in the RecordComparator interface, compare(). This method is called by the enumeration to create the (sorted) result set. Here is a simple class that implements the interface.
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;
else
return RecordComparator.FOLLOWS;
}
}
When exiting the compare() method, you must return one of the three pre-defined return values. The following code block shows how you might use the Comparator class with an enumeration.
RecordStore rs;
...
// Create a new comparator for sorting
Comparator comp = new Comparator();
// Reference the comparator when creating the result set
RecordEnumeration re = rs.enumerateRecords(null, comp, false);
// Move through the sorted results
while (re.hasNextElement())
{
String str = new String(re.nextRecord());
...
}
Complete code example
// The following example demonstrates how to sort the records in RMS using RecordComparator
// Record Comparator ......
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class RMSSort extends MIDlet implements CommandListener {
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command start;
private Command delete;
private Command show;
private RecordStore recordstore = null;
private RecordEnumeration recEnum;
public RMSSort() {
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.SCREEN, 1);
show = new Command("Show",Command.SCREEN,2);
delete = new Command("Delete",Command.SCREEN,2);
form = new Form("RMS Sort");
form.addCommand(exit);
form.addCommand(start);
form.addCommand(show);
form.addCommand(delete);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}
public void commandAction(Command command, Displayable d) {
if (command == start)
{
try
{
recordstore = RecordStore.openRecordStore("RMSSORT",true);
String[] names= {"senthil", "kumar","sri","dhana","arun"};
for(int i=1;i<names.length;i++){
byte[] outputData = names[i].getBytes();
recordstore.addRecord(outputData,0,outputData.length);
}
int numRec = recordstore.getNumRecords();
System.out.println(" no of rec "+numRec);
}catch(Exception e){System.out.println("Exp"+e);}
}
else if (command == show)
{
StringBuffer recBuffer = new StringBuffer();
Comparator comp = new Comparator();
try{
int numRec = recordstore.getNumRecords();
recEnum = recordstore.enumerateRecords(null,comp,false);
while(recEnum.hasNextElement())
{
recBuffer.append( new String(recEnum.nextRecord()));
recBuffer.append("\n");
}
alert = new Alert(" Records ",recBuffer.toString(),null, AlertType. WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}catch(Exception e){System.out.println("Exp"+e);}
}
else if(command == delete)
{
try{
if(recordstore != null){
int numRec = recordstore.getNumRecords();
for(int i=1;i<=numRec;i++)
recordstore.deleteRecord(i);
// System.out.println(" no of rec "+numRec);
}
}catch(Exception e){
System.out.println(" Exception in delete Rec:" +e);
}
}
}
class Comparator implements RecordComparator
{
public Comparator()
{
}
public int compare(byte[] rec1 , byte[] rec2)
{
String s1 = new String(rec1);
String s2 = new String(rec2);
int comp = s1.compareTo(s2);
if(comp==0)
{
return RecordComparator.EQUIVALENT;
}
else if(comp <0)
{
return RecordComparator.PRECEDES;
}
else
{
return RecordComparator.FOLLOWS;
}
}
}
} // End of main class


28 Sep
2009
The Record Management System (RMS) allows persistent data storage in Java ME, even on devices that don’t support JSR-75. This article demonstrates how we can retrieve the contents of a RecordStore in sorted order. The article primarily consists of a code example, showing a sample implementation of the RecordComparator interface. By simply passing an implementation of this interface of a RecordEnumeration object, the contents can be returned in sorted order.
For those familiar with how sorting works in Java SE, this will be a very familiar concept. The code example is kept nice and simple. It shows both how to instantiate a sorted RecordEnumeration and how to implement the RecordComparator interface (through the compare method). The entire process is also shown in a complete code example, showing the process from start to finish.
One relatively minor criticism I have of this article is that it does not show how the compare method should be implemented differently if the programmer desires the records to be sorted in descending order as opposed to ascending order. This is a simple matter of swapping the PRECEDES and FOLLOWS return values.