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.UnsignedInteger; 13 14 15 import hunt.math; 16 import hunt.Number; 17 import std.algorithm.comparison; 18 import std.conv : to; 19 import hunt.logging; 20 21 import std.concurrency : initOnce; 22 23 24 class UnsignedInteger : AbstractNumber!int 25 { 26 // private int _underlying; 27 //__gshared UnsignedInteger[] cachedValues ; 28 29 30 //static UnsignedInteger ZERO() { 31 // return cachedValues[0]; 32 //} 33 //static UnsignedInteger ONE () {return cachedValues[1];} 34 35 static UnsignedInteger ZERO() 36 { 37 __gshared UnsignedInteger inst; 38 return initOnce!inst(cachedValues[0]); 39 } 40 41 static UnsignedInteger ONE() 42 { 43 __gshared UnsignedInteger inst; 44 return initOnce!inst(cachedValues[1]); 45 } 46 47 static UnsignedInteger[] cachedValues() 48 { 49 __gshared UnsignedInteger[] inst; 50 return initOnce!inst(initCachedValues()); 51 } 52 53 static UnsignedInteger MAX_VALUE() 54 { 55 __gshared UnsignedInteger inst; 56 return initOnce!inst(new UnsignedInteger(0xffffffff)); 57 } 58 59 private static UnsignedInteger[] initCachedValues() 60 { 61 UnsignedInteger[] cached = new UnsignedInteger[256]; 62 for(int i = 0; i < 256; i++) 63 { 64 cached[i] = new UnsignedInteger(i); 65 } 66 67 return cached; 68 69 } 70 71 //static this() 72 //{ 73 // 74 // cachedValues = new UnsignedInteger[256]; 75 // for(int i = 0; i < 256; i++) 76 // { 77 // cachedValues[i] = new UnsignedInteger(i); 78 // } 79 // 80 // static UnsignedInteger ZERO = cachedValues[0]; 81 // static UnsignedInteger ONE = cachedValues[1]; 82 // static UnsignedInteger MAX_VALUE = new UnsignedInteger(0xffffffff); 83 //} 84 85 86 this(int value) 87 { 88 super(value); 89 } 90 91 // override 92 // public int intValue() 93 // { 94 // return _underlying; 95 // } 96 97 // override 98 // public long longValue() 99 // { 100 // return (_underlying) & 0xFFFFFFFF; 101 // } 102 103 // override 104 // public float floatValue() 105 // { 106 // return cast(float) (longValue()); 107 // } 108 109 // override 110 // public double doubleValue() 111 // { 112 // return cast(double) (longValue()); 113 // } 114 115 // override bool opEquals(Object o) 116 // { 117 // if (this is o) 118 // { 119 // return true; 120 // } 121 // if (o is null || cast(UnsignedInteger)o is null) 122 // { 123 // return false; 124 // } 125 126 // UnsignedInteger that = cast(UnsignedInteger)o; 127 128 // if (_underlying != that.intValue()) 129 // { 130 // return false; 131 // } 132 133 // return true; 134 // } 135 136 override int opCmp(Object o) 137 { 138 return cast(int)(longValue() - (cast(UnsignedInteger)o).longValue()); 139 } 140 141 // override 142 // public size_t toHash() @trusted nothrow 143 // { 144 // return cast(size_t)_underlying; 145 // } 146 // 147 //override 148 //public string toString() 149 //{ 150 // return to!string(longValue()); 151 //} 152 153 public static UnsignedInteger valueOf(int underlying) 154 { 155 if((underlying & 0xFFFFFF00) == 0) 156 { 157 return cachedValues[underlying]; 158 } 159 else 160 { 161 return new UnsignedInteger(underlying); 162 } 163 } 164 165 public UnsignedInteger add(UnsignedInteger i) 166 { 167 int val = intValue() + i.intValue(); 168 return UnsignedInteger.valueOf(val); 169 } 170 171 public UnsignedInteger subtract(UnsignedInteger i) 172 { 173 int val = intValue() - i.intValue(); 174 return UnsignedInteger.valueOf(val); 175 } 176 177 public static UnsignedInteger valueOf(string value) 178 { 179 long longVal = to!long(value); 180 return valueOf(longVal); 181 } 182 183 public static UnsignedInteger valueOf(long longVal) 184 { 185 if(longVal < 0L || longVal >= (1L<<32)) 186 { 187 logError("lies outside the range"); 188 } 189 return valueOf(cast (int)(longVal)); 190 } 191 192 193 // override 194 // byte byteValue() 195 // { 196 // return 1; 197 // } 198 199 // override short shortValue() 200 // { 201 // return 1; 202 // } 203 204 // override string toString() 205 // { 206 // return ""; 207 // } 208 }