BookServiceServantDatapublic class BookServiceServantData extends Object A class that loads the data for the book service
and provides access to it in the form of an array
of BookInfo objects. |
Fields Summary |
---|
private static BookInfo[] | bookInfoThe loaded data, created when it is first requested | private static HashMap | bookMapThe data in the form of a HashMap, created when it is
first requested. |
Methods Summary |
---|
static BookInfo[] | getBookInfo()Gets the book info, creating it if necessary.
if (bookInfo == null) {
// First request - create the data
ArrayList list = new ArrayList();
try {
InputStream is =
BookServiceServantData.class.getResourceAsStream("booklist.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "!");
if (st.countTokens() == 4) {
list.add(new BookInfo(st.nextToken(), st.nextToken(),
st.nextToken(), Double.parseDouble(st.nextToken())));
}
}
} catch (Exception ex) {
// Just return an empty or partial list
ex.printStackTrace();
}
// Convert the list to an array
bookInfo = new BookInfo[list.size()];
list.toArray(bookInfo);
}
return bookInfo;
| static java.util.HashMap | getBookInfoHashMap()Returns all of the books known to the book
service in the form of a HashMap where the
key to each entry is the book title in upper case
and the value is a BookInfo object.
if (bookMap == null) {
BookInfo[] info = getBookInfo();
bookMap = new HashMap();
for (int i = 0; i < info.length; i++) {
BookInfo book = info[i];
bookMap.put(book.getTitle().toUpperCase(), book);
}
}
return bookMap;
|
|