Your SlideShare is downloading. ×
0
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 th...
•
•
•
•
•
•
•
•
•
•
•
•
•
•

import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microeditio...
•
•
•
•
•
•
•
•
•
•

public MSortRec ()
{
d = Display.getDisplay(this);
exit = new Command("Exit", Command.SCREEN, 1);
sta...
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void startApp()
{
d.setCurrent(f);
}
public void pauseApp()
{
}
public void dest...
•
•
•
•
•
•
•
•
•
•
•
•
•

else if (c == start)
{
try
{
rec = RecordStore.openRecordStore("myRecordStore", true );
}
catch...
•
•
•
•
•
•
•
•
•
•
•
•

try
{
//byte[] outputRecord;
byte[] by;
String s[] = {"Sita", "Rama", "Anand"};
int i[] = {15, 10...
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

for (int x = 0; x < 3; x++)
{
ods.writeUTF(s[x]);
ods.writeInt(i[x]);
ods.flush();
...
•
•

try
{

•
•
•
•
•

String[] s = new String[3];
int z = 0;
byte[] by = new byte[300];
ByteArrayInputStream bais =
new B...
•
•
•
•
•
•
•
•
•
•
•
•
•
•

a = new Alert("Reading", sb.toString(), null,
AlertType.WARNING);
a.setTimeout(Alert.FOREVER)...
•
•
•
•
•
•
•
•
•
•
•

try
{
rec.closeRecordStore();
}
catch (Exception er)
{
a = new Alert("Error Closing",
er.toString()...
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

if (RecordStore.listRecordStores() != null)
{
try
{
rec.deleteRecordStore("myRecord...
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

class Comparator2 implements RecordComparator
{
private byte[] comparatorInputData = new by...
• comparatorInputStream = new
ByteArrayInputStream(record1);
• comparatorInputDataType =
• new DataInputStream(comparatorI...
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

if (record1int == record2int)
{
return RecordComparator.EQUIVALENT;
}
else if (record...
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

public void compareClose()
{
try
{
if (comparatorInputStream!= null)
{
comparatorInpu...
• For java tutorials visit:
http://improvejava.blogspot.in

Interface RecordComparator
Upcoming SlideShare
Loading in...5
×

Interface record comparator

770

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
770
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • http://improvejava.blogspot.in
  • Transcript of "Interface record comparator"

    1. 1. Interface RecordComparator Interface RecordComparator
    2. 2. • javax.microedition.rms • public interface RecordComparator Interface RecordComparator
    3. 3. • 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
    4. 4. • • • • • • • • • • • • • • 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
    5. 5. • • • • • • • • • • 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
    6. 6. • • • • • • • • • • • • • • • • • 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
    7. 7. • • • • • • • • • • • • • 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
    8. 8. • • • • • • • • • • • • 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
    9. 9. • • • • • • • • • • • • • • • • • • • 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
    10. 10. • • 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
    11. 11. • • • • • • • • • • • • • • 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
    12. 12. • • • • • • • • • • • 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
    13. 13. • • • • • • • • • • • • • • • • • • • 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
    14. 14. • • • • • • • • • • • • • • • 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
    15. 15. • 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
    16. 16. • • • • • • • • • • • • • • • • • • 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
    17. 17. • • • • • • • • • • • • • • • • • • public void compareClose() { try { if (comparatorInputStream!= null) { comparatorInputStream.close(); } if (comparatorInputDataType!= null) { comparatorInputDataType.close(); } } catch (Exception error) { } } } Interface RecordComparator
    18. 18. • For java tutorials visit: http://improvejava.blogspot.in Interface RecordComparator
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×