1/* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16package std.core; 17 18/** 19 * Represents boxed byte value and related operations 20 */ 21export final class Byte extends Integral implements Comparable<Byte>, JSONable<Byte> { 22 private value: byte; 23 24 /** 25 * Constructs a new Byte instance with initial value zero 26 */ 27 public constructor() { 28 this.value = 0; 29 } 30 31 /** 32 * Constructs a new Byte instance with provided initial value 33 * 34 * @param value the initial value 35 */ 36 public constructor(value: byte) { 37 this.value = value; 38 } 39 40 /** 41 * Constructs a new Byte instance with provided initial value 42 * 43 * @param value the initial value 44 */ 45 public constructor(value: Byte) { 46 this.value = value.byteValue(); 47 } 48 49 /** 50 * Returns value of this instance as a primitive 51 * 52 * @returns value of this instance 53 */ 54 public unboxed(): byte { 55 return this.value; 56 } 57 58 /** 59 * Returns boxed representation of the primitive 60 * 61 * @param value value to box 62 * 63 * @returns boxed value 64 */ 65 public static valueOf(value: byte): Byte { 66 // TODO(ivan-tyulyandin): caching is possible 67 return new Byte(value); 68 } 69 70 /** 71 * Minimal value that this type can have as an integral 72 */ 73 public static MIN_VALUE: byte = -128; 74 75 /** 76 * Maximal value that this type can have as an integral 77 */ 78 public static MAX_VALUE: byte = 127; 79 80 /** 81 * Size of this type in bits 82 */ 83 public static BIT_SIZE: byte = 8; 84 85 /** 86 * Size of this type in bytes 87 */ 88 public static BYTE_SIZE: byte = 1; 89 90 /** 91 * Returns value of this instance 92 * 93 * @returns value as byte 94 */ 95 public override byteValue(): byte { 96 return this.value; 97 } 98 99 /** 100 * Returns value of this instance 101 * 102 * @returns value as short 103 */ 104 public override shortValue(): short { 105 return this.value as short; 106 } 107 108 /** 109 * Returns value of this instance 110 * 111 * @returns value as int 112 */ 113 public override intValue(): int { 114 return this.value as int; 115 } 116 117 /** 118 * Returns value of this instance 119 * 120 * @returns value as long 121 */ 122 public override longValue(): long { 123 return this.value as long; 124 } 125 126 /** 127 * Returns value of this instance 128 * 129 * @returns value as float 130 */ 131 public override floatValue(): float { 132 return this.value as float; 133 } 134 135 /** 136 * Returns value of this instance 137 * 138 * @returns value as double 139 */ 140 public override doubleValue(): double { 141 return this.value as double; 142 } 143 144 145 /** 146 * Compares this instance to other Byte object 147 * The result is less than 0 if this instance lesser than provided object 148 * 0 if they are equal 149 * and greater than 0 otherwise. 150 * 151 * @param other Byte object to compare with 152 * 153 * @returns result of the comparison 154 */ 155 public override compareTo(other: Byte): int { 156 return (this.value - other.unboxed()) as int; 157 } 158 159 /** 160 * Converts this object to a string 161 * 162 * @returns result of the conversion 163 */ 164 public override toString(): String { 165 // Check for zero, log10 would go inf otherwise 166 if (this.value == 0) { 167 return "0"; 168 } 169 let val = this.value; 170 let negative: boolean = (val < 0); 171 let digitsNum = (log10(abs(this.value)) + 1) as int; 172 if (negative) { 173 ++digitsNum; 174 } 175 let data = new char[digitsNum]; 176 let curPlace = digitsNum - 1; 177 while (val != 0) { 178 let remainder = val % 10; 179 if (negative) { 180 remainder = -remainder; 181 } 182 data[curPlace] = (remainder + c'0') as char; 183 val /= 10; 184 curPlace -= 1; 185 } 186 if (negative) { 187 data[0] = c'-'; 188 } 189 return new String(data); 190 } 191 192 /** 193 * Converts this object to a string 194 * 195 * @returns result of the conversion 196 */ 197 public toString(radix: number): string { 198 return (new Long(this.value)).toString(radix); 199 } 200 201 /** 202 * Returns a hash code (integer representation) for this instance 203 * 204 * @returns representation of this instance 205 */ 206 public override $_hashCode(): int { 207 return this.intValue(); 208 } 209 210 /** 211 * Checks for equality this instance with provided object, treated as a Byte 212 * 213 * @param other object to be checked against 214 * 215 * @returns true if provided object and this instance have same value, false otherwise 216 * Returns false if type of provided object is not the same as this type 217 */ 218 public equals(other: NullishType): boolean { 219 if (__runtimeIsSameReference(this, other)) { 220 return true 221 } 222 223 if (!(other instanceof Byte)) { 224 return false 225 } 226 227 return this.value == (other as Byte).value 228 } 229 230 /** 231 * Performs integral addition of this instance with provided one, returns the result as new instance 232 * 233 * @param other Right hand side of the addition 234 * 235 * @returns Result of the addition 236 */ 237 public add(other: Byte): Byte { 238 return Byte.valueOf((this.value + other.byteValue()) as byte) 239 } 240 241 /** 242 * Performs integral subtraction of this instance with provided one, returns the result as new instance 243 * 244 * @param other Right hand side of the subtraction 245 * 246 * @returns Result of the subtraction 247 */ 248 public sub(other: Byte): Byte { 249 return Byte.valueOf((this.value - other.byteValue()) as byte) 250 } 251 252 /** 253 * Performs integral multiplication of this instance with provided one, returns the result as new instance 254 * 255 * @param other Right hand side of the multiplication 256 * 257 * @returns Result of the multiplication 258 */ 259 public mul(other: Byte): Byte { 260 return Byte.valueOf((this.value * other.byteValue()) as byte) 261 } 262 263 /** 264 * Performs integral division of this instance with provided one, returns the result as new instance 265 * 266 * @param other Right hand side of the division 267 * 268 * @returns Result of the division 269 */ 270 public div(other: Byte): Byte { 271 return Byte.valueOf((this.value / other.byteValue()) as byte) 272 } 273 274 /** 275 * Checks if this instance value is less than value of provided instance 276 * 277 * @param other Right hand side of the comparison 278 * 279 * @returns true if this value is less than provided, false otherwise 280 */ 281 public isLessThan(other: Byte): boolean { 282 return this.value < other.byteValue(); 283 } 284 285 /** 286 * Checks if this instance value is less than or equal to value of provided instance 287 * 288 * @param other Right hand side of the comparison 289 * 290 * @returns true if this value is less than or equal to provided, false otherwise 291 */ 292 public isLessEqualThan(other: Byte): boolean { 293 return this.value <= other.byteValue(); 294 } 295 296 /** 297 * Checks if this instance value is greater than value of provided instance 298 * 299 * @param other Right hand side of the comparison 300 * 301 * @returns true if this value is greater than provided, false otherwise 302 */ 303 public isGreaterThan(other: Byte): boolean { 304 return this.value > other.byteValue(); 305 } 306 307 /** 308 * Checks if this instance value is greater than or equal to value of provided instance 309 * 310 * @param other Right hand side of the comparison 311 * 312 * @returns true if this value is greater than or equal to provided, false otherwise 313 */ 314 public isGreaterEqualThan(other: Byte): boolean { 315 return this.value >= other.byteValue(); 316 } 317 318 319 /** 320 * Creates a Byte instance based on JSONValue 321 * 322 * @param json: JSONValue - a JSON representation 323 * 324 * @throws JSONTypeError if json does not encode a valid byte 325 * 326 * @returns Byte - byte value decoded from JSON 327 */ 328 public createFromJSONValue(json: JSONValue): Byte { 329 if (json instanceof JSONNumber) { 330 let num = (json as JSONNumber).value 331 if (Double.isInteger(num) && Byte.MIN_VALUE <= num && num <= Byte.MAX_VALUE) { 332 return Byte.valueOf(num as byte) 333 } 334 } 335 throw new JSONTypeError("Cannot create Byte from JSON", new ErrorOptions(json as Object)) 336 } 337 338} 339