1 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 30 abstract class NameIndex implements Streamable { 31 32 33 protected HashMap hashOnName; 34 35 36 String name = ""; 37 38 39 public NameIndex() { 40 hashOnName = new HashMap(); 41 } 42 43 46 public void setName(String nm) { 47 name = nm; 48 } 49 50 52 public String toString() { 53 return name; 54 } 55 56 59 protected synchronized Object getObj(String name)throws StorageException { 60 Object 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 [] {name} ) ); 66 } 67 return id; 68 } 69 70 73 protected synchronized Object getObjIf(String name)throws StorageException { 74 return hashOnName.get(name); 75 } 76 77 83 protected synchronized void addObj(String name, Object 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 [] {name} ) ); 89 } 90 hashOnName.put(name, id); 91 } 92 93 97 public synchronized void remove(String 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 [] {name} ) ); 103 } 104 hashOnName.remove(name); 105 } 106 107 110 public synchronized void clear() { 111 hashOnName.clear(); 112 } 113 114 119 public synchronized String [] listNames() { 120 ArrayList list = new ArrayList(); 121 list.addAll(hashOnName.keySet()); 122 return (String [])list.toArray(new String [list.size()]); 123 } 124 125 127 public synchronized Iterator iterator() { 128 return hashOnName.entrySet().iterator(); 129 } 130 131 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 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 )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 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 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 key = dstrm.readUTF().intern(); 191 Object id = readObjectFromStream(dstrm); 192 addObj(key, id); 193 } 194 } 195 catch (IOException ex) { 196 throw new StorageIOException(ex); 197 } 198 } 199 200 205 protected abstract void writeObjectToStream( 206 Object obj, DataOutputStream strm) throws StorageException ; 207 208 213 protected abstract Object readObjectFromStream(DataInputStream strm) 214 throws StorageException; 215 } 216 217 218 219 | Popular Tags |