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