KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > NameIndex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
20
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24 import org.netbeans.mdr.persistence.*;
25 import org.netbeans.mdr.persistence.btreeimpl.btreeindex.*;
26
27 /**
28 * This is an index from names (Strings) to some object.
29 */

30 abstract class NameIndex implements Streamable {
31
32     /* hash table */
33     protected HashMap hashOnName;
34
35     /* name of object */
36     String JavaDoc name = "";
37
38     /** Create a new NameIndex */
39     public NameIndex() {
40         hashOnName = new HashMap();
41     }
42
43     /** give object a name
44     * @param nm name of NameIndex
45     */

46     public void setName(String JavaDoc nm) {
47         name = nm;
48     }
49
50     /** return name set with setName
51     */

52     public String JavaDoc toString() {
53         return name;
54     }
55
56     /** get an object by its name. If none exists, throw an exception
57     * @param name the name associated with the object
58     */

59     protected synchronized Object JavaDoc getObj(String JavaDoc name)throws StorageException {
60         Object JavaDoc id = getObjIf(name);
61         if (id == null) {
62             throw new StorageBadRequestException(
63                 MessageFormat.format(
64                     "There is no value for key \"{0}\"",
65                     new Object JavaDoc[] {name} ) );
66         }
67         return id;
68     }
69
70     /** get an object by its name. If none exists, return null
71     * @param name the name associated with the object
72     */

73     protected synchronized Object JavaDoc getObjIf(String JavaDoc name)throws StorageException {
74         return hashOnName.get(name);
75     }
76
77     /**
78     * Add a name-Object pair to the NameIndex. If one already existed,
79     * throw an exception.
80     * @param name name of object to add to index
81     * @param id object to add
82     */

83     protected synchronized void addObj(String JavaDoc name, Object JavaDoc id) throws StorageException {
84         if (hashOnName.get(name) != null) {
85             throw new StorageBadRequestException(
86                 MessageFormat.format(
87                     "There is already a value for key \"{0}\"",
88                     new Object JavaDoc[] {name} ) );
89         }
90         hashOnName.put(name, id);
91     }
92
93     /** Remove a name-Object pair from the NameIndex. If it is not present,
94     * throw an exception.
95     * @param name name of object to rmove from index
96     */

97     public synchronized void remove(String JavaDoc name) throws StorageException {
98         if (hashOnName.get(name) == null) {
99             throw new StorageBadRequestException(
100                 MessageFormat.format(
101                     "There is no value for key \"{0}\"",
102                     new Object JavaDoc[] {name} ) );
103         }
104         hashOnName.remove(name);
105     }
106
107     /**
108     * Remove all name-Object pairs from the NameIndex.
109     */

110     public synchronized void clear() {
111         hashOnName.clear();
112     }
113
114     /** return the names of all objects in the NameIndex, in no particular
115     * order. Note that this is a snapshot made at the time this method is
116     * called.
117     * @return list of all names
118     */

119     public synchronized String JavaDoc[] listNames() {
120         ArrayList list = new ArrayList();
121         list.addAll(hashOnName.keySet());
122         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
123     }
124
125     /** iterate over entries
126     */

127     public synchronized Iterator iterator() {
128         return hashOnName.entrySet().iterator();
129     }
130
131     /** serialize the NameIndex to a stream. The format is:
132     * <p>
133     * number of pairs
134     * <p>
135     * name of object 1 (in UTF-8)
136     * <p>
137     * Representation of object 1
138     * <p>
139     * ...
140     * name of object N (in UTF-8)
141     * <p>
142     * Representation of object N
143     * <p>
144     * @param strm stream to serialize to
145     */

146     public synchronized void write(OutputStream strm) throws StorageException{
147         if (strm instanceof DataOutputStream)
148         write((DataOutputStream)strm);
149     else
150         write(new DataOutputStream(strm));
151     }
152
153     /** write to a DataOutputStream
154     */

155     public synchronized void write(DataOutputStream dstrm) throws StorageException{
156         try {
157             Set entries = hashOnName.entrySet();
158             dstrm.writeInt(entries.size());
159             Iterator itr = entries.iterator();
160             while (itr.hasNext()) {
161                 Map.Entry entry = (Map.Entry)itr.next();
162                 dstrm.writeUTF((String JavaDoc)entry.getKey());
163         writeObjectToStream(entry.getValue(), dstrm);
164             }
165             dstrm.flush();
166         }
167         catch (IOException ex) {
168             throw new StorageIOException(ex);
169         }
170     }
171
172     /** deserialize the NameIndex from a stream. The format is as
173     * described in write
174     * @param strm stream to deserialize from
175     */

176     public synchronized void read(InputStream strm) throws StorageException{
177         if (strm instanceof DataInputStream)
178         read((DataInputStream)strm);
179     else
180         read(new DataInputStream(strm));
181     }
182
183     /** read from a DataOutputStream
184     */

185     public synchronized void read(DataInputStream dstrm) throws StorageException{
186         try {
187             clear();
188             int size = dstrm.readInt();
189             for (int i = 0; i < size; i++) {
190                 String JavaDoc key = dstrm.readUTF().intern();
191         Object JavaDoc id = readObjectFromStream(dstrm);
192                 addObj(key, id);
193             }
194         }
195         catch (IOException ex) {
196             throw new StorageIOException(ex);
197         }
198     }
199
200     /**
201     * write object to stream. Used by serialization.
202     * @param obj object to write
203     * @param strm stream to write it to
204     */

205     protected abstract void writeObjectToStream(
206         Object JavaDoc obj, DataOutputStream strm) throws StorageException ;
207
208     /**
209     * read object from stream. Used by deserialization.
210     * @param strm stream to read from
211     * @return obj object read
212     */

213     protected abstract Object JavaDoc readObjectFromStream(DataInputStream strm)
214         throws StorageException;
215 }
216
217
218
219
Popular Tags