Java Reference
In-Depth Information
The class has accessors and mutators for these three public fields, and it privately
keeps a constant field—the record's version number—written with each record into the
store. Attaching a version to each record permits you to change the record store imple-
mentation without needing to destroy existing records; new code can check the version
of each record, select the appropriate code to load a record, and then resave the record in
the new format.
The bulk of the methods in the implementation of the Location class are getters and
setters for each of the public fields. Most interesting are the toBytes and fromBytes meth-
ods, which serialize and deserialize a record to and from an array of bytes, respectively.
Although it's unlikely the record format will require much modification, you can choose
to use the record-tag approach described previously. This approach is exemplified by
toBytes , which performs this sequence of steps:
1. It writes a tag indicating that the field version follows the tag.
2. It writes the record version.
3. It writes a tag indicating that the location field follows the tag if there's a field to
write, followed by the location field's contents.
4. It writes a tag indicating that the forecast field follows the tag if there's a field to
write, followed by the forecast field's contents.
The fromBytes method works in reverse, except it uses a while loop and switch - case
statements so that it can read the fields of a record in any order.
As shown in Listing 6-6, the LocationStore interface provides a proxy to the record
store that makes it easy to obtain the list of locations, as well as a forecast for a location. It
doesn't wholly abstract the record-store interface—notably, it throws RecordStoreException
in the event of problems with the underlying store—but it lets you think about accessing
locations in the way used by the application rather than solely as records in a store.
Listing 6-6. The Implementation of the LocationStore Class
package com.apress.rischpater.weatherwidget;
import javax.microedition.rms.*;
public class LocationStore {
private static final String storeName = "wx";
private RecordStore store = null;
public LocationStore() {
}
 
Search WWH ::




Custom Search