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.ShortType;
13 
14 import hunt.proton.codec.AbstractPrimitiveType;
15 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding;
16 import hunt.proton.codec.EncoderImpl;
17 import hunt.proton.codec.DecoderImpl;
18 import hunt.proton.codec.EncodingCodes;
19 import hunt.proton.codec.TypeEncoding;
20 
21 import hunt.collection.Collection;
22 import hunt.collection.Collections;
23 import hunt.Short;
24 import hunt.proton.codec.PrimitiveTypeEncoding;
25 
26 class ShortType : AbstractPrimitiveType!(Short)
27 {
28     private ShortEncoding _shortEncoding;
29 
30     this(EncoderImpl encoder, DecoderImpl decoder)
31     {
32         _shortEncoding = new ShortEncoding(encoder, decoder);
33         encoder.register(typeid(Short), this);
34         decoder.register(this);
35     }
36 
37     public TypeInfo getTypeClass()
38     {
39         return typeid(Short);
40     }
41 
42     public ITypeEncoding getEncoding(Object val)
43     {
44         return _shortEncoding;
45     }
46 
47     public void write(short s)
48     {
49         _shortEncoding.write(s);
50     }
51 
52     public ShortEncoding getCanonicalEncoding()
53     {
54         return _shortEncoding;
55     }
56 
57     public Collection!(TypeEncoding!(Short)) getAllEncodings()
58     {
59         return Collections.singleton!(TypeEncoding!(Short))(_shortEncoding);
60     }
61 
62 
63      //Collection!(PrimitiveTypeEncoding!(Short)) getAllEncodings()
64      //{
65      //   return super.getAllEncodings();
66      //}
67 
68 
69 
70     class ShortEncoding : FixedSizePrimitiveTypeEncoding!(Short)
71     {
72 
73         this(EncoderImpl encoder, DecoderImpl decoder)
74         {
75             super(encoder, decoder);
76         }
77 
78         override
79         protected int getFixedSize()
80         {
81             return 2;
82         }
83 
84         override
85         public byte getEncodingCode()
86         {
87             return EncodingCodes.SHORT;
88         }
89 
90         public ShortType getType()
91         {
92             return this.outer;
93         }
94 
95         public void writeValue(Object val)
96         {
97             getEncoder().writeRaw((cast(Short)val).shortValue());
98         }
99 
100         public void writeValue(short val)
101         {
102             getEncoder().writeRaw(val);
103         }
104 
105 
106         public void write(short s)
107         {
108             writeConstructor();
109             getEncoder().writeRaw(s);
110         }
111 
112         public bool encodesSuperset(TypeEncoding!(Short) encoding)
113         {
114             return (getType() == encoding.getType());
115         }
116 
117         public Short readValue()
118         {
119             return  new Short( readPrimitiveValue());
120         }
121 
122         public short readPrimitiveValue()
123         {
124             return getDecoder().readRawShort();
125         }
126 
127 
128         override
129         public bool encodesJavaPrimitive()
130         {
131             return true;
132         }
133 
134     }
135 }