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