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.UnsignedByte;
13 
14 import hunt.math;
15 import hunt.Number;
16 import std.algorithm.comparison;
17 import std.conv : to;
18 import hunt.logging;
19 
20 
21 import std.concurrency : initOnce;
22 
23 class UnsignedByte : Number
24 {
25     private byte _underlying;
26     //private static UnsignedByte[] cachedValues = new UnsignedByte[256];
27 
28 
29     static UnsignedByte[]  cachedValues() {
30         __gshared UnsignedByte[]  inst;
31         return initOnce!inst(initCachedVal());
32     }
33 
34 
35     static UnsignedByte[] initCachedVal ()
36     {
37         UnsignedByte[] uByteArray = new  UnsignedByte[256];
38         for(int i = 0; i<256; i++)
39         {
40             uByteArray[i] = new UnsignedByte(cast(byte)i);
41         }
42         return uByteArray;
43     }
44 
45     this(byte underlying)
46     {
47         _underlying = underlying;
48     }
49 
50     override
51     public byte byteValue()
52     {
53         return _underlying;
54     }
55 
56     override
57     public short shortValue()
58     {
59         return to!short(intValue());
60     }
61 
62     override
63     public int intValue()
64     {
65         return (to!int(_underlying)) & 0xFF;
66     }
67 
68     override
69     public long longValue()
70     {
71         return (to!long (_underlying)) & 0xFF;
72     }
73 
74     override
75     public float floatValue()
76     {
77         return (to!float(longValue()));
78     }
79 
80     override
81     public double doubleValue()
82     {
83         return  to!double(longValue());
84     }
85 
86     override bool opEquals(Object o)
87     {
88         if (this is o)
89         {
90             return true;
91         }
92         if (o is null || cast(UnsignedByte)o is null)
93         {
94             return false;
95         }
96 
97         UnsignedByte that = cast(UnsignedByte)o;
98 
99         if (_underlying != that._underlying)
100         {
101             return false;
102         }
103 
104         return true;
105     }
106 
107 
108     override int opCmp(Object o)
109     {
110         UnsignedByte that = cast(UnsignedByte)o;
111         return intValue() - that.intValue();
112     }
113 
114     override
115     public  size_t toHash() @trusted nothrow
116     {
117         return cast(size_t)_underlying;
118     }
119 
120     override
121     public string toString()
122     {
123         return "";
124     }
125 
126     public static UnsignedByte valueOf(byte underlying)
127     {
128         int index = (to!int (underlying)) & 0xFF;
129         return cachedValues[index];
130     }
131 
132     public static UnsignedByte valueOf(string value)
133            // throws NumberFormatException
134     {
135         int intVal = to!int(value);
136         if(intVal < 0 || intVal >= (1<<8))
137         {
138             logError("Value %s lies outside the range",value);
139         }
140         return valueOf(to!byte(intVal));
141     }
142 
143 }