Initial checkin of RIM client from LifeMarks, after initial refactorings and style...
[zxing.git] / rim / src / com / google / zxing / client / rim / persistence / history / DecodeHistory.java
diff --git a/rim/src/com/google/zxing/client/rim/persistence/history/DecodeHistory.java b/rim/src/com/google/zxing/client/rim/persistence/history/DecodeHistory.java
new file mode 100644 (file)
index 0000000..2e769cf
--- /dev/null
@@ -0,0 +1,101 @@
+/*\r
+ * Copyright 2008 ZXing authors\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.google.zxing.client.rim.persistence.history;\r
+\r
+import net.rim.device.api.system.PersistentObject;\r
+import net.rim.device.api.system.PersistentStore;\r
+\r
+import java.util.Vector;\r
+\r
+/**\r
+ * Singleton used to persist the history of qrcode URLs decoded by the client.\r
+ *\r
+ * This code was contributed by LifeMarks.\r
+ *\r
+ * @author Matt York (matt@lifemarks.mobi)\r
+ */\r
+public class DecodeHistory {\r
+\r
+  private static final long ID_LONG = 0xb7cc76147b48ad0dL;\r
+\r
+  private static DecodeHistory instance;\r
+\r
+  private final PersistentObject store;\r
+  private final Vector historyItems;\r
+\r
+  private DecodeHistory() {\r
+    store = PersistentStore.getPersistentObject(ID_LONG);\r
+    Vector temp = (Vector) store.getContents();\r
+    historyItems = temp == null ? new Vector() : temp;\r
+  }\r
+\r
+  /**\r
+   * Returns the single instance of this class.\r
+   */\r
+  public static DecodeHistory getInstance() {\r
+    if (instance == null) {\r
+      instance = new DecodeHistory();\r
+    }\r
+    return instance;\r
+  }\r
+\r
+  /**\r
+   * Adds a history object.\r
+   */\r
+  public void addHistoryItem(DecodeHistoryItem item) {\r
+    historyItems.addElement(item);\r
+  }\r
+\r
+  /**\r
+   * Returns all history objects.\r
+   */\r
+  public Vector getItems() {\r
+    return historyItems;\r
+  }\r
+\r
+  /**\r
+   * Gets a particular history object at a given index.\r
+   */\r
+  public DecodeHistoryItem getItemAt(int index) {\r
+    return (DecodeHistoryItem) historyItems.elementAt(index);\r
+  }\r
+\r
+  /**\r
+   * Returns the number of history objects.\r
+   */\r
+  public int getNumItems() {\r
+    return historyItems.size();\r
+  }\r
+\r
+  /**\r
+   * Clears the history.\r
+   */\r
+  public void clear() {\r
+    historyItems.setSize(0);\r
+  }\r
+\r
+  /**\r
+   * Persists the history to the device.\r
+   */\r
+  public void persist() {\r
+    synchronized (store) {\r
+      store.setContents(historyItems);\r
+      store.commit();\r
+    }\r
+  }\r
+\r
+}\r