Java Reference
In-Depth Information
public int [] getXPoints (int size) {
int [] x = new int [getCount()];
for (int i = 0; i < getCount(); i++) {
x [i] = (getTime (i)-MIN_TIME) * size / (MAX_TIME-MIN_TIME);
}
return x;
}
Finally we add a
parseTime()
method in order to convert a time string such as
12:00
, consisting
of an hour and a minute value separated by a colon, to our internal time format. For MIDP, instead of
this conversion we could also use the time selector provided by
DateField
.
public static int parseTime (String time) {
int cut = time.indexOf (':');
if (cut==-1)
return Integer.parseInt (time) * 60;
return Integer.parseInt (time.substring (0, cut)) * 60
+ Integer.parseInt (time.substring (cut+1));
}
Persistent Storage: The
LogStorage
Class
Beneath the
DayLog
class for storing the log of a single day, we need another class to store all the
DayLog
s persistently. The
LogStorage
class uses a record store for that purpose.
In the constructor, the record store with the name
"BloodSugarLog"
is opened and assigned to the
days
object variable:
import javax.microedition.rms.*;
import java.util.*;
import java.io.*;
import java.util.*;
public class LogStorage {
RecordStore days;
public LogStorage() throws RecordStoreException {
days = RecordStore.openRecordStore ("BloodSugarLog", true);
}
}
The most important functionality of the persistent storage is to provide efficient access to the day log of
a specific date. If the logs are stored and put in order by time in the record store, we do not need to
iterate all records in order to find a specific date. Instead, we can take advantage of the ordering and
perform a so-called
binary search.
We just pick the middle element and compare it to our target date.
Now we know in which half we need to continue the search. Thus, we reduce the questionable records
by half with each iteration.
For our binary search, we implement a helper method that just reads the date of a record without
building the complete
DayLog
data structure:
Search WWH ::
Custom Search