FileDocCategorySizeDatePackage
DocBookServiceServantData.javaAPI DocExample2768Tue Oct 15 11:23:00 BST 2002ora.jwsnut.chapter6.docbookservice

DocBookServiceServantData

public class DocBookServiceServantData 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
            loadData();
        }
        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;
    
private static voidloadData()
Loads the book and stock info

        ArrayList list = new ArrayList();
        try {
            InputStream is = 
                DocBookServiceServantData.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() == 5) {
                    list.add(new BookInfo(st.nextToken(), st.nextToken(),
                            st.nextToken(), Double.parseDouble(st.nextToken()),
                            Integer.parseInt(st.nextToken())));
                }
            }
        } catch (Exception ex) {
            // Just return an empty or partial list                
            ex.printStackTrace();
        }

        // Convert the lists to arrays
        bookInfo = new BookInfo[list.size()];
        list.toArray(bookInfo);