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.amqp.Decimal128;
13 
14 import hunt.math;
15 import hunt.Number;
16 import hunt.io.ByteBuffer;
17 import hunt.io.BufferUtils;
18 
19 class Decimal128 : Number
20 {
21     private  BigDecimal _underlying;
22     private  long _msb;
23     private  long _lsb;
24 
25     this(BigDecimal underlying)
26     {
27         _underlying = underlying;
28 
29         _msb = calculateMostSignificantBits(underlying);
30         _lsb = calculateLeastSignificantBits(underlying);
31     }
32 
33 
34     this(long msb,long lsb)
35     {
36         _msb = msb;
37         _lsb = lsb;
38 
39         _underlying = calculateBigDecimal(msb, lsb);
40 
41     }
42 
43     this(byte[] data)
44     {
45         this(BufferUtils.toBuffer(data));
46     }
47 
48     this(ByteBuffer buffer)
49     {
50         this(buffer.getLong(),buffer.getLong());
51     }
52 
53     private static long calculateMostSignificantBits(BigDecimal underlying)
54     {
55         return 0;  //TODO.
56     }
57 
58     private static long calculateLeastSignificantBits(BigDecimal underlying)
59     {
60         return 0;  //TODO.
61     }
62 
63     private static BigDecimal calculateBigDecimal(long msb, long lsb)
64     {
65         return BigDecimal.ZERO;  //TODO.
66     }
67 
68     override
69     public int intValue()
70     {
71         return _underlying.intValue();
72     }
73 
74     override
75     public long longValue()
76     {
77         return _underlying.longValue();
78     }
79 
80     override
81     public float floatValue()
82     {
83         return _underlying.floatValue();
84     }
85 
86     override
87     public double doubleValue()
88     {
89         return _underlying.doubleValue();
90     }
91 
92     public long getMostSignificantBits()
93     {
94         return _msb;
95     }
96 
97     public long getLeastSignificantBits()
98     {
99         return _lsb;
100     }
101 
102     public byte[] asBytes()
103     {
104         byte[] bytes = new byte[16];
105         ByteBuffer buf = BufferUtils.toBuffer(bytes);
106 
107         buf.putLong(getMostSignificantBits());
108         buf.putLong(getLeastSignificantBits());
109         return bytes;
110     }
111 
112     override bool opEquals(Object o)
113     {
114         if (this is o)
115         {
116             return true;
117         }
118         if (o is null || cast(Decimal128)o is null)
119         {
120             return false;
121         }
122 
123         Decimal128 that = cast(Decimal128)o;
124 
125         if (_lsb != that._lsb)
126         {
127             return false;
128         }
129         if (_msb != that._msb)
130         {
131             return false;
132         }
133 
134         return true;
135     }
136 
137     override
138      byte byteValue()
139      {
140          return 1;
141      }
142 
143     override short shortValue()
144     {
145         return 1;
146     }
147 
148     override string toString()
149     {
150         return "";
151     }
152 
153 
154     //override
155     //public int hashCode()
156     //{
157     //    int result = (int) (_msb ^ (_msb >>> 32));
158     //    result = 31 * result + (int) (_lsb ^ (_lsb >>> 32));
159     //    return result;
160     //}
161 }