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