KickJava   Java API By Example, From Geeks To Geeks.

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


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 Integer. Its primary use is to
29 * store the codes for classes stored in the repository.
30 */

31 public class CounterIndex extends NameIndex {
32
33     /* next integer to be assigned */
34     int nextInt = 0;
35
36     /** Create a new CounterIndex */
37     public CounterIndex() {
38     }
39
40     /** get an integer by its name. If none exists, throw an exception
41     * @param name the name associated with the integer
42     */

43     public synchronized int get(String JavaDoc name) throws StorageException {
44         return ((Integer JavaDoc)getObj(name)).intValue();
45     }
46
47     /** get an integer by its name. If none exists, return null
48     * @param name the name associated with the integer
49     */

50     public synchronized Integer JavaDoc getIf(String JavaDoc name) throws StorageException {
51         return (Integer JavaDoc)getObjIf(name);
52     }
53
54     /**
55     * Add a name-MOFID pair to the MofidIndex. If one already existed,
56     * throw an exception.
57     * @param name name of object to add to index
58     * @param id MOFID of object to add
59     */

60     public synchronized int add(String JavaDoc name) throws StorageException {
61     int i = nextInt++;
62         addObj(name, new Integer JavaDoc(i));
63     return i;
64     }
65
66     /* write to a DataOutputStream
67     */

68     public synchronized void write(DataOutputStream dstrm) throws StorageException{
69     super.write(dstrm);
70     try {
71         dstrm.writeInt(nextInt);
72     }
73     catch (IOException ex) {
74         throw new StorageIOException(ex);
75     }
76     }
77
78     /** read from a DataOutputStream
79     */

80     public synchronized void read(DataInputStream dstrm) throws StorageException{
81     super.read(dstrm);
82     try {
83         nextInt = dstrm.readInt();
84     }
85     catch (IOException ex) {
86         throw new StorageIOException(ex);
87     }
88     }
89
90     /**
91     * write object to stream. Used by serialization.
92     * @param obj object to write
93     * @param strm stream to write it to
94     */

95     protected void writeObjectToStream(Object JavaDoc obj, DataOutputStream strm)
96         throws StorageException {
97     try {
98         strm.writeInt(((Integer JavaDoc)obj).intValue());
99     }
100     catch (IOException ex) {
101         throw new StorageIOException(ex);
102     }
103     }
104
105     /**
106     * read object from stream. Used by deserialization.
107     * @param strm stream to read from
108     * @return obj object read
109     */

110     protected Object JavaDoc readObjectFromStream(DataInputStream strm)
111         throws StorageException {
112     try {
113         return new Integer JavaDoc(strm.readInt());
114     }
115     catch (IOException ex) {
116         throw new StorageIOException(ex);
117     }
118     
119     }
120 }
121
122
123
124
Popular Tags