1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.proton.codec.BooleanType; 13 14 15 import hunt.proton.codec.TypeEncoding; 16 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding; 17 import hunt.proton.codec.PrimitiveTypeEncoding; 18 import hunt.proton.codec.AbstractPrimitiveType; 19 20 import hunt.proton.codec.EncodingCodes; 21 import hunt.proton.codec.EncoderImpl; 22 import hunt.proton.codec.DecoderImpl; 23 import hunt.collection.Collection; 24 import hunt.collection.ArrayList; 25 import hunt.Boolean; 26 27 interface BooleanEncoding : PrimitiveTypeEncoding!(Boolean) 28 { 29 void write(bool b); 30 void writeValue(Object b); 31 32 bool readPrimitiveValue(); 33 } 34 35 36 class BooleanType : AbstractPrimitiveType!(Boolean) 37 { 38 39 enum byte BYTE_0 = cast(byte) 0; 40 enum byte BYTE_1 = cast(byte) 1; 41 42 43 44 45 private hunt.proton.codec.BooleanType.BooleanEncoding _trueEncoder; 46 private hunt.proton.codec.BooleanType.BooleanEncoding _falseEncoder; 47 private hunt.proton.codec.BooleanType.BooleanEncoding _boolEncoder; 48 49 50 this(EncoderImpl encoder, DecoderImpl decoder) 51 { 52 _trueEncoder = new TrueEncoding(encoder, decoder); 53 _falseEncoder = new FalseEncoding(encoder, decoder); 54 _boolEncoder = new AllBooleanEncoding(encoder, decoder); 55 56 encoder.register(typeid(Boolean), this); 57 decoder.register(this); 58 } 59 60 public TypeInfo getTypeClass() 61 { 62 return typeid(Boolean); 63 } 64 65 public ITypeEncoding getEncoding(Object val) 66 { 67 //return cast(Boolean)val ? _trueEncoder : _falseEncoder; 68 return (cast(Boolean)val).booleanValue ? _trueEncoder : _falseEncoder; 69 } 70 71 //public BooleanEncoding getEncoding(bool val) 72 //{ 73 // return val ? _trueEncoder : _falseEncoder; 74 //} 75 76 public void writeValue(Boolean val) 77 { 78 (cast(BooleanEncoding)getEncoding(val)).write(val.booleanValue); 79 } 80 81 82 83 84 public BooleanEncoding getCanonicalEncoding() 85 { 86 return _boolEncoder; 87 } 88 89 public Collection!(TypeEncoding!(Boolean)) getAllEncodings() 90 { 91 ArrayList!(TypeEncoding!(Boolean)) lst = new ArrayList!(TypeEncoding!(Boolean))(); 92 lst.add(cast(TypeEncoding!(Boolean))_trueEncoder); 93 lst.add(cast(TypeEncoding!(Boolean))_falseEncoder); 94 lst.add(_boolEncoder); 95 return lst; 96 // return Arrays.asList(_trueEncoder, _falseEncoder, _boolEncoder); 97 } 98 99 100 //Collection!(PrimitiveTypeEncoding!(Boolean)) getAllEncodings() 101 //{ 102 // return super.getAllEncodings(); 103 //} 104 105 class TrueEncoding : FixedSizePrimitiveTypeEncoding!(Boolean) , BooleanEncoding 106 { 107 108 this(EncoderImpl encoder, DecoderImpl decoder) 109 { 110 super(encoder, decoder); 111 } 112 113 override 114 protected int getFixedSize() 115 { 116 return 0; 117 } 118 119 override 120 public byte getEncodingCode() 121 { 122 return EncodingCodes.BOOLEAN_TRUE; 123 } 124 125 public BooleanType getType() 126 { 127 return this.outer; 128 } 129 130 public void writeValue(Object val) 131 { 132 } 133 134 public void write(bool b) 135 { 136 writeConstructor(); 137 } 138 139 public void writeValue(bool b) 140 { 141 } 142 143 public bool encodesSuperset(TypeEncoding!(Boolean) encoding) 144 { 145 return encoding == this; 146 } 147 148 public Boolean readValue() 149 { 150 return Boolean.TRUE; 151 } 152 153 public bool readPrimitiveValue() 154 { 155 return true; 156 } 157 158 override 159 public bool encodesJavaPrimitive() 160 { 161 return true; 162 } 163 164 override 165 void skipValue() 166 { 167 super.skipValue(); 168 } 169 170 override 171 TypeInfo getTypeClass() 172 { 173 return super.getTypeClass(); 174 } 175 176 override void writeConstructor() 177 { 178 return super.writeConstructor(); 179 } 180 181 override int getConstructorSize() 182 { 183 return super.getConstructorSize(); 184 } 185 186 } 187 188 189 class FalseEncoding : FixedSizePrimitiveTypeEncoding!(Boolean) , hunt.proton.codec.BooleanType.BooleanEncoding 190 { 191 192 this(EncoderImpl encoder, DecoderImpl decoder) 193 { 194 super(encoder, decoder); 195 } 196 197 override 198 protected int getFixedSize() 199 { 200 return 0; 201 } 202 203 override 204 public byte getEncodingCode() 205 { 206 return EncodingCodes.BOOLEAN_FALSE; 207 } 208 209 public BooleanType getType() 210 { 211 return this.outer; 212 } 213 214 public void writeValue(Boolean val) 215 { 216 } 217 218 public void write(bool b) 219 { 220 writeConstructor(); 221 } 222 223 public void writeValue(Object b) 224 { 225 } 226 227 public bool readPrimitiveValue() 228 { 229 return false; 230 } 231 232 public bool encodesSuperset(TypeEncoding!(Boolean) encoding) 233 { 234 return encoding == this; 235 } 236 237 public Boolean readValue() 238 { 239 return Boolean.FALSE; 240 } 241 242 243 override 244 public bool encodesJavaPrimitive() 245 { 246 return true; 247 } 248 249 override 250 void skipValue() 251 { 252 super.skipValue(); 253 } 254 255 override 256 TypeInfo getTypeClass() 257 { 258 return super.getTypeClass(); 259 } 260 261 override void writeConstructor() 262 { 263 return super.writeConstructor(); 264 } 265 266 override int getConstructorSize() 267 { 268 return super.getConstructorSize(); 269 } 270 271 272 } 273 274 class AllBooleanEncoding : FixedSizePrimitiveTypeEncoding!(Boolean) , BooleanEncoding 275 { 276 277 this(EncoderImpl encoder, DecoderImpl decoder) 278 { 279 super(encoder, decoder); 280 } 281 282 public BooleanType getType() 283 { 284 return this.outer; 285 } 286 287 override 288 protected int getFixedSize() 289 { 290 return 1; 291 } 292 293 override 294 public byte getEncodingCode() 295 { 296 return EncodingCodes.BOOLEAN; 297 } 298 299 public void writeValue(Object val) 300 { 301 getEncoder().writeRaw(cast(Boolean)val ? BYTE_1 : BYTE_0); 302 } 303 304 public void write(bool val) 305 { 306 writeConstructor(); 307 getEncoder().writeRaw(val ? BYTE_1 : BYTE_0); 308 } 309 310 public void writeValue(bool b) 311 { 312 getEncoder().writeRaw(b ? BYTE_1 : BYTE_0); 313 } 314 315 public bool readPrimitiveValue() 316 { 317 318 return getDecoder().readRawByte() != BYTE_0; 319 } 320 321 public bool encodesSuperset(TypeEncoding!(Boolean) encoding) 322 { 323 return (getType() == encoding.getType()); 324 } 325 326 public Boolean readValue() 327 { 328 return readPrimitiveValue() ? Boolean.TRUE : Boolean.FALSE; 329 } 330 331 332 override 333 public bool encodesJavaPrimitive() 334 { 335 return true; 336 } 337 338 339 override 340 public void skipValue() 341 { 342 super.skipValue(); 343 } 344 345 override 346 public TypeInfo getTypeClass() 347 { 348 return super.getTypeClass(); 349 } 350 351 override void writeConstructor() 352 { 353 return super.writeConstructor(); 354 } 355 356 override int getConstructorSize() 357 { 358 return super.getConstructorSize(); 359 } 360 361 } 362 }