Guest User

sample blackberry persistent store usage

a guest
Apr 3rd, 2013
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package mypackage;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. import net.rim.device.api.system.PersistentObject;
  6. import net.rim.device.api.system.PersistentStore;
  7. import net.rim.device.api.util.Persistable;
  8.  
  9. /**
  10.  * Use this class like so:
  11.  * <code>
  12.  *    PersistentStoreHelper store = PersistentStoreHelper.getInstance();
  13.  *    Vector msgs = (Vector)store.get("chatMsgs");
  14.  *    if (msgs == null) {
  15.  *        msgs = new Vector();
  16.  *        store.put("chatMsgs", msgs);
  17.  *    }
  18.  *    msgs.addElement("this is a new chat message");
  19.  *    store.put("someOtherKey", someOtherValue);
  20.  *    // commit will save changes both to the msgs list, and to `someOtherValue` added
  21.  *    store.commit();
  22.  * </code>
  23.  */
  24. public class PersistentStoreHelper implements Persistable {  // only Persistable so inner class can be!
  25.  
  26.    private static final long KEY = 0x9df9f961bc6d6daL;
  27.    /**
  28.     * persistentHashtable is now an instance of a class that
  29.     * only exists in your app
  30.     */
  31.    private MyAppsHashtable persistentHashtable;
  32.    private PersistentObject persistentObject;
  33.  
  34.    /** PersistentStoreHelper is a singleton */
  35.    private static PersistentStoreHelper instance = null;
  36.  
  37.    /** call this to get access to the methods of this class */
  38.    public static PersistentStoreHelper getInstance() {
  39.       if (instance == null) {
  40.          instance = new PersistentStoreHelper();
  41.       }
  42.       return instance;
  43.    }
  44.  
  45.    /** constructor private to force users to call getInstance() */
  46.    private PersistentStoreHelper() {
  47.       persistentObject = PersistentStore.getPersistentObject(KEY);
  48.       if (persistentObject.getContents() == null) {  
  49.          persistentHashtable = new MyAppsHashtable();    
  50.          persistentObject.setContents(persistentHashtable);
  51.       } else {
  52.          persistentHashtable =
  53.                (MyAppsHashtable)persistentObject.getContents();
  54.       }
  55.    }
  56.  
  57.    /** use this method to retrieve a saved value from the store, by its key */
  58.    public Object get(String key) {
  59.       return persistentHashtable.get(key);
  60.    }
  61.  
  62.    /** use this method to add a value to be saved in the store */
  63.    public void put(String key, Object value) {
  64.       persistentHashtable.put(key, value);
  65.    }
  66.  
  67.    /** call this when you want to make sure all recent changes are saved to device storage */
  68.    public void commit() {
  69.       persistentObject.setContents(persistentHashtable);
  70.       persistentObject.commit();
  71.    }
  72.  
  73.    private class MyAppsHashtable extends Hashtable implements Persistable {
  74.       // don't need anything else ... the declaration does it all!
  75.    }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment