Interface RecordComparator

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

Interface RecordComparator
• It is used to comparing two records. Generally it is used to
find the sorting order.
• The return value must indicate the ordering of the two
records.
• The compare method is called by RecordEnumeration to
sort and return records in an application specified order.
For example:
• RecordComparator c = new AddressRecordComparator(); if
(c.compare(recordStore.getRecord(rec1),
recordStore.getRecord(rec2)) ==
RecordComparator.PRECEDES) return rec1;

Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•

import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class MSortRec 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 Comparator2 com = null;
Interface RecordComparator
•
•
•
•
•
•
•
•
•
•

public MSortRec ()
{
d = Display.getDisplay(this);
exit = new Command("Exit", Command.SCREEN, 1);
start = new Command("Start", Command.SCREEN, 1);
f = new Form("Mixed RecordEnumeration");
f.addCommand(exit);
f.addCommand(start);
f.setCommandListener(this);
}
Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void startApp()
{
d.setCurrent(f);
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}
public void commandAction(Command c, Displayable dd)
{
if (c == exit)
{
destroyApp(true);
notifyDestroyed();
}

Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•

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

try
{
//byte[] outputRecord;
byte[] by;
String s[] = {"Sita", "Rama", "Anand"};
int i[] = {15, 10, 5};
//ByteArrayOutputStream outputStream =
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
//DataOutputStream outputDataStream =
DataOutputStream ods =
new DataOutputStream(baos);

Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

for (int x = 0; x < 3; x++)
{
ods.writeUTF(s[x]);
ods.writeInt(i[x]);
ods.flush();
by = baos.toByteArray();
rec.addRecord(by, 0,by.length);
baos.reset();
}
baos.close();
ods.close();
}
catch ( Exception error)
{
a = new Alert("Error Writing",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}

Interface RecordComparator
•
•

try
{

•
•
•
•
•

String[] s = new String[3];
int z = 0;
byte[] by = new byte[300];
ByteArrayInputStream bais =
new ByteArrayInputStream(by);

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

DataInputStream dis =
new DataInputStream(bais);
StringBuffer sb = new StringBuffer();
com = new Comparator2();
r= rec.enumerateRecords(null, com, false);//now RecordEnumeration is build
while (r.hasNextElement())//if there is another record is there in RecordEnumeration, returns true,
//false otherwise
{
rec.getRecord( r.nextRecordId(),by, 0);//it returns the record ID of next record
//present in the RecordEnumeration and copies into by array
sb.append(dis.readUTF());
sb.append(dis.readInt());
sb.append("n");
dis.reset();
}

Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•

a = new Alert("Reading", sb.toString(), null,
AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
dis.close();
bais.close();
}
catch (Exception error)
{
a = new Alert("Error Reading",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•

try
{
rec.closeRecordStore();
}
catch (Exception er)
{
a = new Alert("Error Closing",
er.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

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

Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

class Comparator2 implements RecordComparator
{
private byte[] comparatorInputData = new byte[300];
private ByteArrayInputStream comparatorInputStream = null;
private DataInputStream comparatorInputDataType = null;
public int compare(byte[] record1, byte[] record2)
{
int record1int, record2int;
try
{
int maxlen = Math.max(record1.length, record2.length);
if (maxlen > comparatorInputData.length)
{
comparatorInputData = new byte[maxlen];
}

Interface RecordComparator
• comparatorInputStream = new
ByteArrayInputStream(record1);
• comparatorInputDataType =
• new DataInputStream(comparatorInputStream);
• comparatorInputDataType.readUTF();
• record1int = comparatorInputDataType.readInt();
• comparatorInputStream = new
ByteArrayInputStream(record2);
• comparatorInputDataType =
• new DataInputStream(comparatorInputStream);
• comparatorInputDataType.readUTF();
• record2int = comparatorInputDataType.readInt();
Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

if (record1int == record2int)
{
return RecordComparator.EQUIVALENT;
}
else if (record1int < record2int)
{
return RecordComparator.PRECEDES;
}
else
{
return RecordComparator.FOLLOWS;
}
}
catch (Exception error)
{
return RecordComparator.EQUIVALENT;
}
}

Interface RecordComparator
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void compareClose()
{
try
{
if (comparatorInputStream!= null)
{
comparatorInputStream.close();
}
if (comparatorInputDataType!= null)
{
comparatorInputDataType.close();
}
}
catch (Exception error)
{
}
}
}

Interface RecordComparator
• For java tutorials visit:
http://improvejava.blogspot.in

Interface RecordComparator

Interface record comparator

Editor's Notes

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