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