1 46 50 package org.mr.core.util.byteable; 51 52 import java.io.IOException ; 53 54 import java.util.Collection ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 58 59 60 75 public class ByteableMap implements Byteable { 76 77 private HashMap content = new HashMap (); 78 79 84 public synchronized void put(Byteable key , Byteable value){ 85 content.put(key , new ByteableMapEntry(key , value) ); 86 87 } 88 93 public synchronized void put(String key , Byteable value){ 94 content.put(key , new ByteableMapEntry(key , value) ); 95 96 } 97 102 public synchronized void put(String key , String value){ 103 content.put(key , new ByteableMapEntry(key , value) ); 104 105 } 106 110 public synchronized void remove(Object key){ 111 content.remove(key); 112 } 113 114 119 public synchronized Object get(Object key){ 120 ByteableMapEntry entry = (ByteableMapEntry)content.get(key); 121 if(entry == null) return null; 122 return entry.getValue(); 123 } 124 125 static final String name = "BMap"; 126 129 public String getByteableName() { 130 return name; 131 } 132 133 136 public synchronized void toBytes(ByteableOutputStream out) throws IOException { 137 138 Collection entries = content.values(); 139 int size = entries.size(); 140 out.writeInt(size); 141 Iterator iter = entries.iterator(); 142 while(iter.hasNext()){ 143 ByteableMapEntry entry = (ByteableMapEntry)iter.next(); 144 out.writeByteable(entry); 145 } 146 147 } 149 150 151 154 public Byteable createInstance(ByteableInputStream in) throws IOException { 155 ByteableMap result = new ByteableMap(); 156 int size = in.readInt(); 158 for (int i = 0; i < size; i++) { 159 ByteableMapEntry entry =(ByteableMapEntry) in.readByteable(); 160 result.content.put(entry.getKey() , entry); 161 } 162 return result; 163 } 164 165 168 public void registerToByteableRegistry() { 169 ByteableRegistry.registerByteableFactory(getByteableName() , this); 170 } 171 172 175 public HashMap getUnderlineMap() { 176 return content; 177 } 178 182 public static void register(){ 183 ByteableMap instance = new ByteableMap(); 184 instance.registerToByteableRegistry(); 185 } 186 189 public String toString(){ 190 return content.toString(); 191 } 192 193 } 194 | Popular Tags |