FileDocCategorySizeDatePackage
BookServiceServantData.javaAPI DocExample3061Tue Oct 29 13:51:16 GMT 2002ora.jwsnut.chapter6.serializerbookservice

SBookServiceServantData

public class SBookServiceServantData 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.
private static HashMap
categorizedBookMap
Categorized data in the form of a HashMap.
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 = 
                    SBookServiceServantData.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())));
                    } else if (st.countTokens() == 5) {
                        list.add(new ElectronicBookInfo(st.nextToken(), st.nextToken(),
                        st.nextToken(), Double.parseDouble(st.nextToken()),
                        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;