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