FileDocCategorySizeDatePackage
BookServiceServantData.javaAPI DocExample2662Sun Oct 27 13:43:14 GMT 2002ora.jwsnut.chapter2.bookservice

BookServiceServantData

public 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[]
bookInfo
The loaded data, created when it is first requested
private static HashMap
bookMap
The data in the form of a HashMap, created when it is first requested.
Constructors Summary
Methods Summary
static BookInfo[]getBookInfo()
Gets the book info, creating it if necessary.

return
an array of BookInfo objects with one entry for each book.

        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.HashMapgetBookInfoHashMap()
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;