Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 05:21, 30 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Record sorting in RMS

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: sparkystar (20 Dec 2007)
Last edited: hamishwillee (30 Jul 2013)

Introduction

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.

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.

RecordStore rs;
...
RecordEnumeration re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement())
{
String str = new String(re.nextRecord());
System.out.println("Record: " + str);
}

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
This page was last modified on 30 July 2013, at 05:21.
54 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×