KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > FormatableHashtable


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.FormatableHashtable
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import org.apache.derby.iapi.services.io.ArrayInputStream;
25
26 import org.apache.derby.iapi.services.io.StoredFormatIds;
27 import org.apache.derby.iapi.services.io.FormatIdUtil;
28 import org.apache.derby.iapi.services.io.Formatable;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30
31 import java.util.Hashtable JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.io.ObjectOutput JavaDoc;
34 import java.io.ObjectInput JavaDoc;
35 import java.io.IOException JavaDoc;
36
37
38 /**
39  * A formatable holder for a java.util.Hashtable.
40  * Used to avoid serializing Properties.
41  */

42 public class FormatableHashtable extends Hashtable JavaDoc implements Formatable
43 {
44     /********************************************************
45     **
46     ** This class implements Formatable. That means that it
47     ** can write itself to and from a formatted stream. If
48     ** you add more fields to this class, make sure that you
49     ** also write/read them with the writeExternal()/readExternal()
50     ** methods.
51     **
52     ** If, inbetween releases, you add more fields to this class,
53     ** then you should bump the version number emitted by the getTypeFormatId()
54     ** method.
55     **
56     ********************************************************/

57
58     /**
59      * Niladic constructor for formatable
60      */

61     public FormatableHashtable()
62     {
63     }
64
65
66     /**
67      * Our special put method that wont barf
68      * on a null value.
69      * @see java.util.Hashtable
70      */

71     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
72     {
73         if (value == null)
74         {
75             return remove(key);
76         }
77
78         if (SanityManager.DEBUG) {
79
80         if ((value instanceof FormatableIntHolder) ||
81             (value instanceof FormatableLongHolder) ||
82             ((value instanceof java.io.Serializable JavaDoc) && (!(value instanceof Formatable)) && (!(value instanceof String JavaDoc)))
83             ) {
84
85             if (!value.getClass().isArray()) {
86
87                 // System.out.println("key " + key + " class " + value.getClass());
88
//new Throwable().printStackTrace(System.out);
89
//System.exit(1);
90
}
91         }
92         }
93         return super.put(key, value);
94     }
95
96     public void putInt(Object JavaDoc key, int value) {
97
98         super.put(key, new FormatableIntHolder(value));
99     }
100
101     public int getInt(Object JavaDoc key) {
102
103         return ((FormatableIntHolder) get(key)).getInt();
104     }
105     public void putLong(Object JavaDoc key, long value) {
106
107         super.put(key, new FormatableLongHolder(value));
108     }
109
110     public long getLong(Object JavaDoc key) {
111
112         return ((FormatableLongHolder) get(key)).getLong();
113     }
114     public void putBoolean(Object JavaDoc key, boolean value) {
115
116         putInt(key,value ? 1 : 0);
117     }
118
119     public boolean getBoolean(Object JavaDoc key) {
120
121         return getInt(key) == 0 ? false : true;
122
123     }
124
125     //////////////////////////////////////////////
126
//
127
// FORMATABLE
128
//
129
//////////////////////////////////////////////
130
/**
131      * Write the hash table out. Step through
132      * the enumeration and write the strings out
133      * in UTF.
134      *
135      * @param out write bytes here
136      *
137      * @exception IOException thrown on error
138      */

139     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
140     {
141         out.writeInt(size());
142         for (Enumeration JavaDoc e = keys(); e.hasMoreElements(); )
143         {
144             Object JavaDoc key = e.nextElement();
145             out.writeObject(key);
146             out.writeObject(get(key));
147             
148             if (SanityManager.DEBUG)
149             {
150                 Object JavaDoc value = get(key);
151                 if (value instanceof Formatable[])
152                 {
153                     SanityManager.THROWASSERT("you should be using FormatableArrayHolder rather than writing out an array of Formatables, otherwise you will get bad behavior for null Storables. Your class is a "+value.getClass().getName());
154                 }
155             }
156         }
157     }
158
159     /**
160      * Read the hash table from a stream of stored objects.
161      *
162      * @param in read this.
163      *
164      * @exception IOException thrown on error
165      * @exception ClassNotFoundException thrown on error
166      */

167     public void readExternal(ObjectInput JavaDoc in)
168         throws IOException JavaDoc, ClassNotFoundException JavaDoc
169     {
170         int size = in.readInt();
171         for (; size > 0; size--)
172         {
173             super.put(in.readObject(), in.readObject());
174         }
175     }
176     public void readExternal(ArrayInputStream in)
177         throws IOException JavaDoc, ClassNotFoundException JavaDoc
178     {
179         int size = in.readInt();
180         for (; size > 0; size--)
181         {
182             super.put(in.readObject(), in.readObject());
183         }
184     }
185     
186     /**
187      * Get the formatID which corresponds to this class.
188      *
189      * @return the formatID of this class
190      */

191     public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_HASHTABLE_V01_ID; }
192 }
193
Popular Tags