1 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 ; 32 import java.util.Enumeration ; 33 import java.io.ObjectOutput ; 34 import java.io.ObjectInput ; 35 import java.io.IOException ; 36 37 38 42 public class FormatableHashtable extends Hashtable implements Formatable 43 { 44 57 58 61 public FormatableHashtable() 62 { 63 } 64 65 66 71 public Object put(Object key, Object 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 ) && (!(value instanceof Formatable)) && (!(value instanceof String ))) 83 ) { 84 85 if (!value.getClass().isArray()) { 86 87 } 91 } 92 } 93 return super.put(key, value); 94 } 95 96 public void putInt(Object key, int value) { 97 98 super.put(key, new FormatableIntHolder(value)); 99 } 100 101 public int getInt(Object key) { 102 103 return ((FormatableIntHolder) get(key)).getInt(); 104 } 105 public void putLong(Object key, long value) { 106 107 super.put(key, new FormatableLongHolder(value)); 108 } 109 110 public long getLong(Object key) { 111 112 return ((FormatableLongHolder) get(key)).getLong(); 113 } 114 public void putBoolean(Object key, boolean value) { 115 116 putInt(key,value ? 1 : 0); 117 } 118 119 public boolean getBoolean(Object key) { 120 121 return getInt(key) == 0 ? false : true; 122 123 } 124 125 139 public void writeExternal(ObjectOutput out) throws IOException 140 { 141 out.writeInt(size()); 142 for (Enumeration e = keys(); e.hasMoreElements(); ) 143 { 144 Object key = e.nextElement(); 145 out.writeObject(key); 146 out.writeObject(get(key)); 147 148 if (SanityManager.DEBUG) 149 { 150 Object 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 167 public void readExternal(ObjectInput in) 168 throws IOException , ClassNotFoundException 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 , ClassNotFoundException 178 { 179 int size = in.readInt(); 180 for (; size > 0; size--) 181 { 182 super.put(in.readObject(), in.readObject()); 183 } 184 } 185 186 191 public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_HASHTABLE_V01_ID; } 192 } 193 | Popular Tags |