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

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.persistence.btreeimpl.btreestorage;

import java.io.*;
import java.text.*;
import java.util.*;
import org.netbeans.mdr.persistence.*;
import org.netbeans.mdr.persistence.btreeimpl.btreeindex.*;

/**
* This is an index from names (Strings) to some object.
*/
abstract class NameIndex implements Streamable  {

    /* hash table */
    protected HashMap hashOnName;

    /* name of object */
    String name = "";

    /** Create a new NameIndex  */
    public NameIndex() {
        hashOnName = new HashMap();
    }

    /** give object a name
    * @param nm name of NameIndex
    */
    public void setName(String nm) {
    	name = nm;
    }

    /** return name set with setName
    */
    public String toString() {
    	return name;
    }

    /** get an object by its name.  If none exists, throw an exception
    * @param name the name associated with the object
    */
    protected synchronized Object getObj(String name)throws StorageException  {
        Object id = getObjIf(name);
        if (id == null) {
            throw new StorageBadRequestException(
                MessageFormat.format(
                    "There is no value for key \"{0}\"", 
                    new Object[] {name} ) );
        }
        return id;
    }

    /** get an object by its name.  If none exists, return null
    * @param name the name associated with the object
    */
    protected synchronized Object getObjIf(String name)throws StorageException  {
        return hashOnName.get(name);
    }

    /**
    * Add a name-Object pair to the NameIndex.  If one already existed,
    * throw an exception.
    * @param name name of object to add to index
    * @param id object to add
    */
    protected synchronized void addObj(String name, Object id) throws StorageException {
        if (hashOnName.get(name) != null) {
            throw new StorageBadRequestException(
                MessageFormat.format(
                    "There is already a value for key \"{0}\"", 
                    new Object[] {name} ) );
        }
        hashOnName.put(name, id);
    }

    /** Remove a name-Object pair from the NameIndex.  If it is not present, 
    * throw an exception.
    * @param name name of object to rmove from index
    */
    public synchronized void remove(String name) throws StorageException {
        if (hashOnName.get(name) == null) {
            throw new StorageBadRequestException(
                MessageFormat.format(
                    "There is no value for key \"{0}\"", 
                    new Object[] {name} ) );
        }
        hashOnName.remove(name);
    }

    /**
    * Remove all name-Object pairs from the NameIndex.
    */
    public synchronized void clear() {
        hashOnName.clear();
    }

    /** return the names of all objects in the NameIndex, in no particular
    * order.  Note that this is a snapshot made at the time this method is 
    * called.
    * @return list of all names
    */
    public synchronized String[] listNames() {
        ArrayList list = new ArrayList();
        list.addAll(hashOnName.keySet());
        return (String[])list.toArray(new String[0]);
    }

    /** iterate over entries
    */
    public synchronized Iterator iterator() {
    	return hashOnName.entrySet().iterator();
    }

    /** serialize the NameIndex to a stream.  The format is:
    * 

* number of pairs *

* name of object 1 (in UTF-8) *

* Representation of object 1 *

* ... * name of object N (in UTF-8) *

* Representation of object N *

* @param strm stream to serialize to */ public synchronized void write(OutputStream strm) throws StorageException{ if (strm instanceof DataOutputStream) write((DataOutputStream)strm); else write(new DataOutputStream(strm)); } /** write to a DataOutputStream */ public synchronized void write(DataOutputStream dstrm) throws StorageException{ try { Set entries = hashOnName.entrySet(); dstrm.writeInt(entries.size()); Iterator itr = entries.iterator(); while (itr.hasNext()) { Map.Entry entry = (Map.Entry)itr.next(); dstrm.writeUTF((String)entry.getKey()); writeObjectToStream(entry.getValue(), dstrm); } dstrm.flush(); } catch (IOException ex) { throw new StorageIOException(ex); } } /** deserialize the NameIndex from a stream. The format is as * described in write * @param strm stream to deserialize from */ public synchronized void read(InputStream strm) throws StorageException{ if (strm instanceof DataInputStream) read((DataInputStream)strm); else read(new DataInputStream(strm)); } /** read from a DataOutputStream */ public synchronized void read(DataInputStream dstrm) throws StorageException{ try { clear(); int size = dstrm.readInt(); for (int i = 0; i < size; i++) { String key = dstrm.readUTF(); Object id = readObjectFromStream(dstrm); addObj(key, id); } } catch (IOException ex) { throw new StorageIOException(ex); } } /** * write object to stream. Used by serialization. * @param obj object to write * @param strm stream to write it to */ protected abstract void writeObjectToStream( Object obj, DataOutputStream strm) throws StorageException ; /** * read object from stream. Used by deserialization. * @param strm stream to read from * @return obj object read */ protected abstract Object readObjectFromStream(DataInputStream strm) throws StorageException; }

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.