example.stock
Class Database

java.lang.Object
  extended by example.stock.Database
Direct Known Subclasses:
AlertDatabase, StockDatabase

public abstract class Database
extends java.lang.Object

This class provides a wrapper class for the RecordStore class. It allows for easier addition and deletion as well as better searching and updating of records. The used recordIDs are kept in a Vector which we use to access the indices of the records. The last used recordID is stored at the beginning of the database and when the database is opened, each recordID up to the last one used is tested to see if a record exists in that position and a new Vector of used recordIDs is generated.


Field Summary
protected  javax.microedition.rms.RecordStore database
          The database storing all the records and the last used recordID in position 1
protected  int lastID
          The last used ID in the database
protected  javax.microedition.rms.RecordComparator rc
          The object used to compare two records and see if they are equal
protected  java.util.Vector recordIDs
          The Vector of used recordIDs that are in the database
 
Constructor Summary
Database()
           
 
Method Summary
 void add(java.lang.String record)
          Add the record to the database
Add the recordID to our vector
Update the database's last ID counter
 void cleanUp(java.lang.String fileName)
          Remove the database from persistant storage
 void close()
          Close the database and remove it from persistant storage if it is empty
 void delete(java.lang.String s)
          Delete the record from the database and remove that recordID from the vector of used recordIDs
 int getNumRecords()
          Return the number of records in the database
 void open(java.lang.String fileName)
          Initializes the database and if it's not a new database, loads the recordID of the last record out of the first position in the RecordStore.
 java.lang.String search(java.lang.String s)
          Find and return a record
 void update(java.lang.String s, byte[] data)
          Update the record with the name s with the data in the byte[] array
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

database

protected volatile javax.microedition.rms.RecordStore database
The database storing all the records and the last used recordID in position 1


recordIDs

protected volatile java.util.Vector recordIDs
The Vector of used recordIDs that are in the database


lastID

protected int lastID
The last used ID in the database


rc

protected javax.microedition.rms.RecordComparator rc
The object used to compare two records and see if they are equal

Constructor Detail

Database

public Database()
Method Detail

open

public void open(java.lang.String fileName)
          throws javax.microedition.rms.RecordStoreNotFoundException,
                 javax.microedition.rms.RecordStoreException,
                 javax.microedition.rms.RecordStoreFullException

Initializes the database and if it's not a new database, loads the recordID of the last record out of the first position in the RecordStore. We have stored it there when we closed the database, then checks each ID from 1 to lastID to see if they exist in the database and then add the IDs that exist to the recordIDs Vector

Parameters:
fileName - The name of the RecordStore to open
Throws:
RecordStoreNotFoundException - is thrown if the RecordStore indicated with fileName cannot be found
RecordStoreException - is thrown when a general exception occurs in a RecordStore operation
RecordStoreFullException - is thrown when the storage system is is full
javax.microedition.rms.RecordStoreNotFoundException
javax.microedition.rms.RecordStoreException
javax.microedition.rms.RecordStoreFullException

close

public void close()
           throws javax.microedition.rms.RecordStoreNotOpenException,
                  javax.microedition.rms.RecordStoreException

Close the database and remove it from persistant storage if it is empty

Throws:
RecordStoreNotOpenException - is thrown when trying to close a RecordStore that is not open
RecordStoreException - is thrown when a general exception occurs in a RecordStore operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreException

cleanUp

public void cleanUp(java.lang.String fileName)
             throws javax.microedition.rms.RecordStoreNotFoundException,
                    javax.microedition.rms.RecordStoreException

Remove the database from persistant storage

Parameters:
fileName - the name of the RecordStore to remove
Throws:
javax.microedition.rms.RecordStoreNotFoundException
javax.microedition.rms.RecordStoreException

add

public void add(java.lang.String record)
         throws javax.microedition.rms.RecordStoreNotOpenException,
                javax.microedition.rms.RecordStoreFullException,
                javax.microedition.rms.RecordStoreException

Add the record to the database
Add the recordID to our vector
Update the database's last ID counter

Parameters:
record - The record data to be added to the database
Throws:
RecordStoreNotOpenException - is thrown when trying to close a RecordStore that is not open
RecordStoreFullException - is thrown when the storage system is is full
RecordStoreException - is thrown when a general exception occurs in a RecordStore operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreFullException
javax.microedition.rms.RecordStoreException

delete

public void delete(java.lang.String s)
            throws javax.microedition.rms.RecordStoreNotOpenException,
                   javax.microedition.rms.RecordStoreException

Delete the record from the database and remove that recordID from the vector of used recordIDs

Parameters:
s - The name of the record to delete from the database
Throws:
RecordStoreNotOpenException - is thrown when trying to close a RecordStore that is not open
RecordStoreException - is thrown when a general exception occurs in a RecordStore operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreException

search

public java.lang.String search(java.lang.String s)
                        throws javax.microedition.rms.RecordStoreNotOpenException,
                               javax.microedition.rms.RecordStoreException

Find and return a record

Parameters:
s - The name of the record to search for
Returns:
The record that we're looking for or null if not found
Throws:
RecordStoreNotOpenException - is thrown when trying to close a RecordStore that is not open
RecordStoreException - is thrown when a general exception occurs in a RecordStore operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreException

update

public void update(java.lang.String s,
                   byte[] data)
            throws javax.microedition.rms.RecordStoreNotOpenException,
                   javax.microedition.rms.RecordStoreFullException,
                   javax.microedition.rms.RecordStoreException

Update the record with the name s with the data in the byte[] array

Parameters:
s - The name of the record to update
data - the new data to update the record with
Throws:
RecordStoreNotOpenException - is thrown when trying to close a RecordStore that is not open
RecordStoreFullException - is thrown when the storage system is is full
RecordStoreException - is thrown when a general exception occurs in a RecordStore operation
javax.microedition.rms.RecordStoreNotOpenException
javax.microedition.rms.RecordStoreFullException
javax.microedition.rms.RecordStoreException

getNumRecords

public int getNumRecords()
                  throws javax.microedition.rms.RecordStoreNotOpenException

Return the number of records in the database

Returns:
the number of records in the database
Throws:
RecordStoreNotOpenException - is thrown when trying to close a RecordStore that is not open
javax.microedition.rms.RecordStoreNotOpenException