FileDocCategorySizeDatePackage
CDCatalog.javaAPI DocExample3960Wed Sep 19 09:33:08 BST 2001javaxml2

CDCatalog.java

/*-- 

 Copyright (C) 2001 Brett McLaughlin.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows 
    these conditions in the documentation and/or other materials 
    provided with the distribution.

 3. The name "Java and XML" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact brett@newInstance.com.
 
 In addition, we request (but do not require) that you include in the 
 end-user documentation provided with the redistribution and/or in the 
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed for the
      'Java and XML' book, by Brett McLaughlin (O'Reilly & Associates)."

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 */
package javaxml2;

import java.util.Hashtable;

/**
 * <p>
 *  <code>CDCatalog</code> is a standalone Java class that stores
 *    CDs. It allows addition, retrieval, and listing of CDs. In this
 *    case, it is intended to be used as a web service, although no
 *    special coding considerations were taken with this in mind.
 * </p>
 */
public class CDCatalog {

    /** The CDs, by title */
    private Hashtable catalog;

    /**
     * <p>Default constructor to initialize storage.</p>
     */
    public CDCatalog() {
        catalog = new Hashtable();

        // Seed the catalog
        addCD(new CD("Nickel Creek", "Nickel Creek", "Sugar Hill"));
        addCD(new CD("Let it Fall", "Sean Watkins", "Sugar Hill"));
        addCD(new CD("Aerial Boundaries", "Michael Hedges", "Windham Hill"));
        addCD(new CD("Taproot", "Michael Hedges", "Windham Hill"));
    }

    /**
     * <p>This adds a new <code>{@link CD}</code> to the catalog.</p>
     *
     * @param cd <code>CD</code> to add.
     */
    public void addCD(CD cd) {
        if (cd == null) {
            throw new IllegalArgumentException("The CD object cannot be null.");
        }
        catalog.put(cd.getTitle(), cd);        
    }

    /**
     * <p>
     *  This will retrieve the <code>{@link CD}</code> with the supplied
     *    title, or <code>null</code> if no such CD is found.
     * </p>
     *
     * @param title the CD title to retrieve.
     * @return <code>CD</code> - the CD with the supplied title.
     */
    public CD getCD(String title) {
        if (title == null) {
            throw new IllegalArgumentException("Title cannot be null.");
        }

        // Return the requested CD
        return (CD)catalog.get(title);
    }

    /**
     * <p>This returns a listing of all CDs in this catalog.</p>
     *
     * @return <code>Hashtable</code> - all CDs, with the title as the key.
     */
    public Hashtable list() {
        return catalog;
    }
}