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