Implementing the MIDlet main class

The MIDlet main class implements the user interface and actions in the MIDlet.

The MIDlet main class creates the following UI:

Figure: PersistentStorage test results view

To implement the MIDlet main class:

  1. Create the RmsMidlet.java class file.

  2. Import the required classes and packages.

    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.rms.RecordStore;
    import javax.microedition.rms.RecordStoreException;
  3. Set RmsMidlet to extend MIDlet and CommandListener. Create the required variables.

    public class RmsMidlet extends MIDlet implements CommandListener {
        private Command exit;
        private Command run;
        private Command delete;
        private Form form;
    
        private long startTime;
        private long stopTime;
    
  4. Create the test case instances for the four different RMS usage approaches. Create a dummy record data string.

        private RmsConnection basicRms = new BasicRms();
        private RmsConnection badRms = new BadRms();
        private RmsConnection cacheRms = new CacheRms();
        private RmsConnection fileRms = new FileRms();
    
        private final String recordData = "some dummy data for record";
  5. Create the RmsMidlet class constructor, MIDlet lifecycle methods, command handling method, and Form handling methods.

        public RmsMidlet() {
            form = new Form("RMS");
            exit = new Command("Exit", Command.EXIT, 1);
            run = new Command("Run", Command.SCREEN, 1);
            delete = new Command("Delete", Command.SCREEN, 1);
            form.addCommand(exit);
            form.addCommand(run);
            form.addCommand(delete);
            form.setCommandListener(this);
        }
    
        public void startApp() {
            Display.getDisplay(this).setCurrent(form);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    
        public void commandAction(Command command, Displayable displayable)
        {
            if (command == exit)
            {
                destroyApp(true);
                notifyDestroyed();
            }
            else if (command == run)
            {
                run();
            }
            else if (command == delete)
            {
                reset();
            }
        }
    
        private void log(String msg) {
            System.out.println(msg);
            form.append(msg + "\n");
        }
    
        private void clear() {
            form.deleteAll();
        }
  6. Use the run method to execute the tests when the user selects Run.

        private void run() {
            init();
            log("Starting test...");
            String simpleResult = test(basicRms);
            log("Basic RecordStore:\n" + simpleResult);
            String badResult = test(badRms);
            log("Bad RecordStore:\n" + badResult);
            String cacheResult = test(cacheRms);
            log("Cache RecordStore:\n" + cacheResult);
            String fileResult = test(fileRms);
            log("File RecordStore:\n" + fileResult);
            log("Test done");
        }
  7. Create methods for populating and handling the record stores and testing their efficiency.

        private void init() {
            clear();
            String[] stores = RecordStore.listRecordStores();
            if (stores == null) {
                log("Populating recordstores...");
                populateStore(basicRms);
                populateStore(badRms);
                populateStore(cacheRms);
                populateStore(fileRms);
                log("Populating recordstores done");
            }
        }
    
        private void populateStore(RmsConnection rms) {
            try {
                rms.open();
                rms.set(recordData.getBytes());
            } catch (Exception e) {
                log("Can not populate recordstores with the "
                        + rms.getClass() + "!");
            } finally {
                rms.close();
            }
        }
    
        private void reset() {
            clear();
            log("Deleting recordstores...");
    
            // Remove normal record stores
            String[] stores = RecordStore.listRecordStores();
            for (int i = 0; stores != null && i < stores.length; i++) {
                try {
                    RecordStore.deleteRecordStore(stores[i]);
                } catch (Exception e) {
                    log("Deleting of the recordstore " + stores[i] + " failed!");
                }
            }
    
            // Remove file record store
            try {
                ((FileRms) fileRms).delete();
            } catch (Exception e) {
                log("Deleting of the recordstore FileRms failed!");
            }
    
            log("Deleting recordstores done");
        }
    
        private String test(RmsConnection rms) {
            String rtn = "";
            try {
                startTiming();
                rms.open();
                for (int i = 0; i < 100; i++) {
                    byte[] data = rms.get();
                    String out = new String(data);
                    // sanity check - what goes in must come out
                    checkEquals(recordData, out);
                }
                rms.close();
                stopTiming();
                rtn = getTime();
            } catch (SecurityException e) {
                rtn = "Not allowed to test";
            } catch (DataMatchException e) {
                rtn = "Tested data does not match";
            } catch (Exception e) {
                rtn = "Error in running the test";
            } finally {
                rms.close();
                return rtn;
            }
        }
    
        private class DataMatchException extends RuntimeException {
        }
    
        private void checkEquals(String expected, String actual) {
            if (expected.compareTo(actual) != 0) {
                throw new DataMatchException();
            }
        }
    
        private void startTiming() {
            startTime = System.currentTimeMillis();
        }
    
        private void stopTiming() {
            stopTime = System.currentTimeMillis();
        }
    
        private String getTime() {
            return (new Long(stopTime - startTime)).toString() + " ms";
        }
    }

In addition to the MIDlet main class, create the RmsConnection interface, which is used for the record store implementations. The main purpose of the interface is to hide the actual details of how the record store is used, and consequently to make it possible to change the record store access strategy easily:

public interface RmsConnection {
    public void open();
    public byte[] get();
    public void set(byte[] data);
    public void close();
}

Now that you have implemented the MIDlet main class and record store interface, implement the different RMS usage approaches.