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.UnsignedByteType;
13 
14 
15 import hunt.proton.amqp.UnsignedByte;
16 
17 import hunt.proton.codec.TypeEncoding;
18 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding;
19 import hunt.proton.codec.EncodingCodes;
20 import hunt.proton.codec.AbstractPrimitiveType;
21 import hunt.proton.codec.EncoderImpl;
22 import hunt.proton.codec.DecoderImpl;
23 
24 import hunt.collection.Collection;
25 import hunt.collection.Collections;
26 import hunt.proton.codec.PrimitiveTypeEncoding;
27 
28 class UnsignedByteType : AbstractPrimitiveType!(UnsignedByte)
29 {
30     private UnsignedByteEncoding _unsignedByteEncoding;
31 
32     this(EncoderImpl encoder, DecoderImpl decoder)
33     {
34         _unsignedByteEncoding = new UnsignedByteEncoding(encoder, decoder);
35         encoder.register(typeid(UnsignedByte), this);
36         decoder.register(this);
37     }
38 
39     public TypeInfo getTypeClass()
40     {
41         return typeid(UnsignedByte);
42     }
43 
44     public ITypeEncoding getEncoding(Object val)
45     {
46         return _unsignedByteEncoding;
47     }
48 
49     public void fastWrite(EncoderImpl encoder, UnsignedByte value)
50     {
51         encoder.writeRaw(EncodingCodes.UBYTE);
52         encoder.writeRaw(value.byteValue());
53     }
54 
55     public UnsignedByteEncoding getCanonicalEncoding()
56     {
57         return _unsignedByteEncoding;
58     }
59 
60     public Collection!(TypeEncoding!(UnsignedByte)) getAllEncodings()
61     {
62         return Collections.singleton!(TypeEncoding!(UnsignedByte))(_unsignedByteEncoding);
63     }
64 
65     //public  Collection!(PrimitiveTypeEncoding!(UnsignedByte)) getAllEncodings()
66     //{
67     //    return super.getAllEncodings();
68     //}
69 
70 
71     class UnsignedByteEncoding : FixedSizePrimitiveTypeEncoding!(UnsignedByte)
72     {
73 
74         this(EncoderImpl encoder, DecoderImpl decoder)
75         {
76             super(encoder, decoder);
77         }
78 
79         override
80         protected int getFixedSize()
81         {
82             return 1;
83         }
84 
85         override
86         public byte getEncodingCode()
87         {
88             return EncodingCodes.UBYTE;
89         }
90 
91         public UnsignedByteType getType()
92         {
93             return this.outer;
94         }
95 
96         public void writeValue(Object val)
97         {
98             getEncoder().writeRaw((cast(UnsignedByte)val).byteValue());
99         }
100 
101         public bool encodesSuperset(TypeEncoding!(UnsignedByte) encoding)
102         {
103             return (getType() == encoding.getType());
104         }
105 
106         public UnsignedByte readValue()
107         {
108             return UnsignedByte.valueOf(getDecoder().readRawByte());
109         }
110     }
111 }