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.UnsignedIntegerType; 13 14 import hunt.proton.amqp.UnsignedInteger; 15 16 import hunt.proton.codec.TypeEncoding; 17 import hunt.collection.Collection; 18 import hunt.proton.codec.AbstractPrimitiveType; 19 import hunt.proton.codec.PrimitiveTypeEncoding; 20 import hunt.proton.codec.EncoderImpl; 21 import hunt.proton.codec.DecoderImpl; 22 import hunt.proton.codec.EncodingCodes; 23 import hunt.collection.ArrayList; 24 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding; 25 26 interface UnsignedIntegerEncoding : PrimitiveTypeEncoding!(UnsignedInteger) 27 { 28 29 } 30 31 32 class UnsignedIntegerType : AbstractPrimitiveType!(UnsignedInteger) 33 { 34 35 private UnsignedIntegerEncoding _unsignedIntegerEncoding; 36 private UnsignedIntegerEncoding _smallUnsignedIntegerEncoding; 37 private UnsignedIntegerEncoding _zeroUnsignedIntegerEncoding; 38 39 40 this(EncoderImpl encoder, DecoderImpl decoder) 41 { 42 _unsignedIntegerEncoding = new AllUnsignedIntegerEncoding(encoder, decoder); 43 _smallUnsignedIntegerEncoding = new SmallUnsignedIntegerEncoding(encoder, decoder); 44 _zeroUnsignedIntegerEncoding = new ZeroUnsignedIntegerEncoding(encoder, decoder); 45 encoder.register(typeid(UnsignedInteger), this); 46 decoder.register(this); 47 } 48 49 public TypeInfo getTypeClass() 50 { 51 return typeid(UnsignedInteger); 52 } 53 54 public ITypeEncoding getEncoding(Object val) 55 { 56 int i = (cast(UnsignedInteger)val).intValue(); 57 return i == 0 58 ? _zeroUnsignedIntegerEncoding 59 : (i >= 0 && i <= 255) ? _smallUnsignedIntegerEncoding : _unsignedIntegerEncoding; 60 } 61 62 public void fastWrite(EncoderImpl encoder, UnsignedInteger value) 63 { 64 int intValue = value.intValue(); 65 if (intValue == 0) 66 { 67 encoder.writeRaw(EncodingCodes.UINT0); 68 } 69 else if (intValue > 0 && intValue <= 255) 70 { 71 encoder.writeRaw(EncodingCodes.SMALLUINT); 72 encoder.writeRaw(cast(byte)intValue); 73 } 74 else 75 { 76 encoder.writeRaw(EncodingCodes.UINT); 77 encoder.writeRaw(intValue); 78 } 79 } 80 81 public UnsignedIntegerEncoding getCanonicalEncoding() 82 { 83 return _unsignedIntegerEncoding; 84 } 85 86 public Collection!(TypeEncoding!(UnsignedInteger)) getAllEncodings() 87 { 88 ArrayList!(TypeEncoding!(UnsignedInteger)) lst = new ArrayList!(TypeEncoding!(UnsignedInteger))(); 89 lst.add(_unsignedIntegerEncoding); 90 lst.add(_smallUnsignedIntegerEncoding); 91 lst.add(_zeroUnsignedIntegerEncoding); 92 return lst; 93 // return Arrays.asList(_unsignedIntegerEncoding, _smallUnsignedIntegerEncoding, _zeroUnsignedIntegerEncoding); 94 } 95 96 //Collection!(PrimitiveTypeEncoding!(UnsignedInteger)) getAllEncodings() 97 //{ 98 // return super.getAllEncodings(); 99 //} 100 101 class AllUnsignedIntegerEncoding 102 : FixedSizePrimitiveTypeEncoding!(UnsignedInteger) 103 , UnsignedIntegerEncoding 104 { 105 106 this(EncoderImpl encoder, DecoderImpl decoder) 107 { 108 super(encoder, decoder); 109 } 110 111 override 112 protected int getFixedSize() 113 { 114 return 4; 115 } 116 117 override 118 public byte getEncodingCode() 119 { 120 return EncodingCodes.UINT; 121 } 122 123 public UnsignedIntegerType getType() 124 { 125 return this.outer; 126 } 127 128 public void writeValue(Object val) 129 { 130 getEncoder().writeRaw((cast(UnsignedInteger)val).intValue()); 131 } 132 133 public void write(int i) 134 { 135 writeConstructor(); 136 getEncoder().writeRaw(i); 137 138 } 139 140 override 141 public void skipValue() 142 { 143 super.skipValue(); 144 } 145 146 override public bool encodesJavaPrimitive() 147 { 148 return super.encodesJavaPrimitive(); 149 } 150 151 override public TypeInfo getTypeClass() 152 { 153 return super.getTypeClass(); 154 } 155 156 public bool encodesSuperset(TypeEncoding!(UnsignedInteger) encoding) 157 { 158 return (getType() == encoding.getType()); 159 } 160 161 public UnsignedInteger readValue() 162 { 163 return UnsignedInteger.valueOf(getDecoder().readRawInt()); 164 } 165 166 override void writeConstructor() 167 { 168 return super.writeConstructor(); 169 } 170 171 override int getConstructorSize() 172 { 173 return super.getConstructorSize(); 174 } 175 } 176 177 class SmallUnsignedIntegerEncoding 178 : FixedSizePrimitiveTypeEncoding!(UnsignedInteger) 179 , UnsignedIntegerEncoding 180 { 181 this(EncoderImpl encoder, DecoderImpl decoder) 182 { 183 super(encoder, decoder); 184 } 185 186 override 187 public byte getEncodingCode() 188 { 189 return EncodingCodes.SMALLUINT; 190 } 191 192 override 193 protected int getFixedSize() 194 { 195 return 1; 196 } 197 198 199 public UnsignedIntegerType getType() 200 { 201 return this.outer; 202 } 203 204 public void writeValue(Object val) 205 { 206 getEncoder().writeRaw(cast(byte)(cast(UnsignedInteger)val).intValue()); 207 } 208 209 override 210 public void skipValue() 211 { 212 super.skipValue(); 213 } 214 215 override public bool encodesJavaPrimitive() 216 { 217 return super.encodesJavaPrimitive(); 218 } 219 220 override public TypeInfo getTypeClass() 221 { 222 return super.getTypeClass(); 223 } 224 225 public bool encodesSuperset(TypeEncoding!(UnsignedInteger) encoder) 226 { 227 return encoder is this ; // || typeof(encoder).stringof == typeof(ZeroUnsignedIntegerEncoding).stringof; 228 } 229 230 public UnsignedInteger readValue() 231 { 232 return UnsignedInteger.valueOf((cast(int)getDecoder().readRawByte()) & 0xff); 233 } 234 235 override void writeConstructor() 236 { 237 return super.writeConstructor(); 238 } 239 240 override int getConstructorSize() 241 { 242 return super.getConstructorSize(); 243 } 244 } 245 246 247 class ZeroUnsignedIntegerEncoding 248 : FixedSizePrimitiveTypeEncoding!(UnsignedInteger) 249 , UnsignedIntegerEncoding 250 { 251 this(EncoderImpl encoder, DecoderImpl decoder) 252 { 253 super(encoder, decoder); 254 } 255 256 override 257 public byte getEncodingCode() 258 { 259 return EncodingCodes.UINT0; 260 } 261 262 override 263 protected int getFixedSize() 264 { 265 return 0; 266 } 267 268 269 public UnsignedIntegerType getType() 270 { 271 return this.outer; 272 } 273 274 public void writeValue(Object val) 275 { 276 } 277 278 public bool encodesSuperset(TypeEncoding!(UnsignedInteger) encoder) 279 { 280 return encoder == this; 281 } 282 283 public UnsignedInteger readValue() 284 { 285 return UnsignedInteger.ZERO; 286 } 287 288 override 289 public void skipValue() 290 { 291 super.skipValue(); 292 } 293 294 override public bool encodesJavaPrimitive() 295 { 296 return super.encodesJavaPrimitive(); 297 } 298 299 override public TypeInfo getTypeClass() 300 { 301 return super.getTypeClass(); 302 } 303 304 override void writeConstructor() 305 { 306 return super.writeConstructor(); 307 } 308 309 override int getConstructorSize() 310 { 311 return super.getConstructorSize(); 312 } 313 } 314 }