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