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.BigIntegerType;
13 
14 /*
15 import java.math.BigInteger;
16 import java.util.Arrays;
17 import hunt.collection.Collection;
18 
19 class BigIntegerType : AbstractPrimitiveType!(BigInteger) {
20 
21     public static interface BigIntegerEncoding : PrimitiveTypeEncoding!(BigInteger)
22     {
23         void write(BigInteger l);
24         void writeValue(BigInteger l);
25         public BigInteger readPrimitiveValue();
26     }
27 
28     private static BigInteger BIG_BYTE_MIN = BigInteger.valueOf(Byte.MIN_VALUE);
29     private static BigInteger BIG_BYTE_MAX = BigInteger.valueOf(Byte.MAX_VALUE);
30     private static BigInteger BIG_LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
31     private static BigInteger BIG_LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
32 
33     private BigIntegerEncoding _BigIntegerEncoding;
34     private BigIntegerEncoding _smallBigIntegerEncoding;
35 
36     BigIntegerType(EncoderImpl encoder, DecoderImpl decoder)
37     {
38         _BigIntegerEncoding = new AllBigIntegerEncoding(encoder, decoder);
39         _smallBigIntegerEncoding = new SmallBigIntegerEncoding(encoder, decoder);
40         encoder.register(BigInteger.class, this);
41     }
42 
43     public Class!(BigInteger) getTypeClass()
44     {
45         return BigInteger.class;
46     }
47 
48     public BigIntegerEncoding getEncoding(BigInteger l)
49     {
50         return (l.compareTo(BIG_BYTE_MIN) >= 0 && l.compareTo(BIG_BYTE_MAX) <= 0) ? _smallBigIntegerEncoding : _BigIntegerEncoding;
51     }
52 
53 
54     public BigIntegerEncoding getCanonicalEncoding()
55     {
56         return _BigIntegerEncoding;
57     }
58 
59     public Collection!(BigIntegerEncoding) getAllEncodings()
60     {
61         return Arrays.asList(_smallBigIntegerEncoding, _BigIntegerEncoding);
62     }
63 
64     private long longValueExact(BigInteger val) {
65         if (val.compareTo(BIG_LONG_MIN) < 0 || val.compareTo(BIG_LONG_MAX) > 0) {
66             throw new ArithmeticException("cannot encode BigInteger not representable as long");
67         }
68         return val.longValue();
69     }
70 
71     private class AllBigIntegerEncoding : FixedSizePrimitiveTypeEncoding!(BigInteger) implements BigIntegerEncoding
72     {
73 
74         public AllBigIntegerEncoding(EncoderImpl encoder, DecoderImpl decoder)
75         {
76             super(encoder, decoder);
77         }
78 
79         override
80         protected int getFixedSize()
81         {
82             return 8;
83         }
84 
85         override
86         public byte getEncodingCode()
87         {
88             return EncodingCodes.LONG;
89         }
90 
91         public BigIntegerType getType()
92         {
93             return BigIntegerType.this;
94         }
95 
96         public void writeValue(BigInteger val)
97         {
98             getEncoder().writeRaw(longValueExact(val));
99         }
100         
101         public void write(BigInteger l)
102         {
103             writeConstructor();
104             getEncoder().writeRaw(longValueExact(l));
105             
106         }
107 
108         public bool encodesSuperset(TypeEncoding!(BigInteger) encoding)
109         {
110             return (getType() == encoding.getType());
111         }
112 
113         public BigInteger readValue()
114         {
115             return readPrimitiveValue();
116         }
117 
118         public BigInteger readPrimitiveValue()
119         {
120             return BigInteger.valueOf(getDecoder().readLong());
121         }
122 
123 
124         override
125         public bool encodesJavaPrimitive()
126         {
127             return true;
128         }
129     }
130 
131     private class SmallBigIntegerEncoding  : FixedSizePrimitiveTypeEncoding!(BigInteger) implements BigIntegerEncoding
132     {
133         public SmallBigIntegerEncoding(EncoderImpl encoder, DecoderImpl decoder)
134         {
135             super(encoder, decoder);
136         }
137 
138         override
139         public byte getEncodingCode()
140         {
141             return EncodingCodes.SMALLLONG;
142         }
143 
144         override
145         protected int getFixedSize()
146         {
147             return 1;
148         }
149 
150         public void write(BigInteger l)
151         {
152             writeConstructor();
153             getEncoder().writeRaw(l.byteValue());
154         }
155 
156         public BigInteger readPrimitiveValue()
157         {
158             return BigInteger.valueOf(getDecoder().readRawByte());
159         }
160 
161         public BigIntegerType getType()
162         {
163             return BigIntegerType.this;
164         }
165 
166         public void writeValue(BigInteger val)
167         {
168             getEncoder().writeRaw(val.byteValue());
169         }
170 
171         public bool encodesSuperset(TypeEncoding!(BigInteger) encoder)
172         {
173             return encoder == this;
174         }
175 
176         public BigInteger readValue()
177         {
178             return readPrimitiveValue();
179         }
180 
181 
182         override
183         public bool encodesJavaPrimitive()
184         {
185             return true;
186         }
187     }
188 }
189 */