|
What this is
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: * |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.