1 46 51 package org.mr.core.util.byteable; 52 53 import java.io.IOException ; 54 55 60 public class ByteableMapEntry implements Byteable { 61 62 public static final byte STRING_STRING =0; 63 public static final byte BYTEABLE_BYTEABLE =1; 64 public static final byte STRING_BYTEABLE =2; 65 66 byte type; 67 String sKey; 68 String sValue; 69 70 Byteable bKey; 71 Byteable bValue; 72 73 public ByteableMapEntry(String key, String value) { 74 sKey = key; 75 sValue = value; 76 type = STRING_STRING; 77 } 78 79 80 public ByteableMapEntry(Byteable key, Byteable value) { 81 bKey =key; 82 bValue = value; 83 type = BYTEABLE_BYTEABLE; 84 85 } 86 87 public ByteableMapEntry(String key, Byteable value) { 88 sKey = key; 89 bValue = value; 90 type =STRING_BYTEABLE; 91 } 92 93 static final String name = "BMapEnt"; 94 97 public String getByteableName() { 98 99 return name; 100 } 101 104 public void toBytes(ByteableOutputStream out) throws IOException { 105 out.writeByte(type); 106 switch(type){ 107 case(STRING_STRING): 108 out.writeUTF(sKey); 109 out.writeUTF(sValue); 110 break; 111 case(BYTEABLE_BYTEABLE): 112 out.writeByteable(bKey); 113 out.writeByteable(bValue); 114 break; 115 case(STRING_BYTEABLE): 116 out.writeUTF(sKey); 117 out.writeByteable(bValue); 118 break; 119 } 120 121 } 122 125 public Byteable createInstance(ByteableInputStream in) throws IOException { 126 127 ByteableMapEntry result = null; 128 byte type = in.readByte(); 129 switch(type){ 130 case(STRING_STRING): 131 result = new ByteableMapEntry(in.readUTF() , in.readUTF()); 132 break; 133 case(BYTEABLE_BYTEABLE): 134 result = new ByteableMapEntry(in.readByteable() , in.readByteable()); 135 break; 136 case(STRING_BYTEABLE): 137 result = new ByteableMapEntry(in.readUTF() , in.readByteable()); 138 break; 139 } 140 return result; 141 } 142 143 146 public void registerToByteableRegistry() { 147 ByteableRegistry.registerByteableFactory(getByteableName() , this); 148 } 149 public static void register(){ 150 ByteableMapEntry instance = new ByteableMapEntry("" , ""); 151 instance.registerToByteableRegistry(); 152 } 153 154 155 public Object getValue(){ 156 if(sValue != null) 157 return sValue; 158 else 159 return bValue; 160 } 161 162 public Object getKey() { 163 if(sKey != null) 164 return sKey; 165 return bKey; 166 } 167 168 169 170 173 public byte getType() { 174 return type; 175 } 176 177 public String toString(){ 178 String result = "Map Entry Key: "; 179 if(sKey != null) 180 result += sKey; 181 if(bKey != null) 182 result +=String.valueOf(bKey); 183 result += " Value: "; 184 if(sValue != null) 185 result += sValue; 186 if(bValue != null) 187 result +=String.valueOf(bValue); 188 return result; 189 } 190 191 192 } 193 | Popular Tags |