alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/**
 * GuestbookDatabase.java
 * Pete Willemsen
 * CoolServlets.com
 * February 3, 2000
 * Version 1.0.3
 *
 * Any errors or feature requests can be reported on CoolServlets.com.
 * We hope you enjoy this program!
 *
 *    Copyright (C) 2000  Pete Willemsen
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.coolservlets.guestbook;

import java.io.*;
import java.util.*;

public class GuestbookDatabase implements Serializable {

    public GuestbookDatabase() {
	_database = new Hashtable();
    }

    /** 
     * Add a Guestbook entry to the database specified by key.  If
     * no "key" exists, a new entry will be created.  Note that
     * the hashtable stores a vector of Guestbook entries
     * at each entry. */
    public void addEntry( String key, GuestbookEntry e ) {

	if (_database.containsKey( key )) {
	    
	    // just lookup the key in the hash table and add the
	    // guestbook entry to the vector

	    Vector entry_vector = (Vector)_database.get( key );
	    
	    // Add a guestbook entry to the end of the vector.
	    // Presumably (unless I don't understand Java), this is
	    // faster than adding it to the beginning of the vector
	    // which if it happens would generally require that all
	    // items with greater index be copied.
	    entry_vector.addElement( e );

	}
	else {
	    
	    // add this key to the hashtable and create a new vector
	    // of guestbook entries for that key
	    Vector entry_vector = new Vector();
	    
	    // Add a guestbook entry to the end of the vector.
	    // Presumably (unless I don't understand Java), this is
	    // faster than adding it to the beginning of the vector
	    // which if it happens would generally require that all
	    // items with greater index be copied.
	    entry_vector.addElement( e );

	    _database.put( key, entry_vector );
	    
	}
	    
    }

    public GuestbookEntry getEntry( String key, int idx  ) {
	
	Vector entry_vector;

	// First, look up the guestbookentry vector from the hashtable
	if (_database.containsKey( key )) { 
	    entry_vector = (Vector)_database.get( key );
	}
	else {
	    // key does not exist in hashtable, so return null
	    return null;
	}

	// Now, it is necessary that guestbook entries be treated such
	// that database[0] is the most recent entry.  Since I append
	// entries onto the vector (for speed), re-map the indices to
	// achieve the correct order.

	if (idx >= 0 && idx < entry_vector.size()) {
	    int remap_idx = (entry_vector.size() - 1) - idx;
	    return (GuestbookEntry)entry_vector.elementAt( remap_idx );
	}
	else {
	    // should I really throw out of range exception, but until
	    // then, return null
	    return null;
	}
    }

    public int size( String key ) {
	Vector entry_vector;

	// Look up the guestbookentry vector from the hashtable and
	// return the size if found
	if (_database.containsKey( key )) {
	    entry_vector = (Vector)_database.get( key );
	    return entry_vector.size();
	}
	else 
	    return 0;
    }
    
    private Hashtable _database;
} 

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.