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.UnsignedShortType;
13 
14 import hunt.proton.amqp.UnsignedShort;
15 
16 import hunt.collection.Collection;
17 import hunt.collection.Collections;
18 
19 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding;
20 import hunt.proton.codec.EncoderImpl;
21 import hunt.proton.codec.DecoderImpl;
22 import hunt.proton.codec.EncodingCodes;
23 import hunt.proton.codec.AbstractPrimitiveType;
24 import hunt.proton.codec.TypeEncoding;
25 import hunt.proton.codec.PrimitiveTypeEncoding;
26 
27 class UnsignedShortType : AbstractPrimitiveType!(UnsignedShort)
28 {
29     private UnsignedShortEncoding _unsignedShortEncoder;
30 
31     this(EncoderImpl encoder, DecoderImpl decoder)
32     {
33         _unsignedShortEncoder = new UnsignedShortEncoding(encoder, decoder);
34         encoder.register(typeid(UnsignedShort), this);
35         decoder.register(this);
36     }
37 
38     public TypeInfo getTypeClass()
39     {
40         return typeid(UnsignedShort);
41     }
42 
43     public ITypeEncoding getEncoding(Object val)
44     {
45         return _unsignedShortEncoder;
46     }
47 
48     public void fastWrite(EncoderImpl encoder, UnsignedShort value)
49     {
50         encoder.writeRaw(EncodingCodes.USHORT);
51         encoder.writeRaw(value.shortValue());
52     }
53 
54     public UnsignedShortEncoding getCanonicalEncoding()
55     {
56         return _unsignedShortEncoder;
57     }
58 
59     public Collection!(TypeEncoding!(UnsignedShort))  getAllEncodings()
60     {
61         return Collections.singleton!(TypeEncoding!(UnsignedShort)) (_unsignedShortEncoder);
62     }
63 
64     //Collection!(PrimitiveTypeEncoding!(UnsignedShort)) getAllEncodings()
65     //{
66     //    return super.getAllEncodings();
67     //}
68 
69 
70     class UnsignedShortEncoding : FixedSizePrimitiveTypeEncoding!(UnsignedShort)
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.USHORT;
88         }
89 
90         public UnsignedShortType getType()
91         {
92             return this.outer;
93            // return UnsignedShortType.this;
94         }
95 
96         public void writeValue(Object val)
97         {
98             getEncoder().writeRaw((cast(UnsignedShort)val).shortValue());
99         }
100 
101         public bool encodesSuperset(TypeEncoding!(UnsignedShort) encoding)
102         {
103             return (getType() == encoding.getType());
104         }
105 
106         public UnsignedShort readValue()
107         {
108             return UnsignedShort.valueOf(getDecoder().readRawShort());
109         }
110     }
111 }