1/* 2 * Copyright (c) 2021-2025 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 * Returns value of this instance as a primitive 42 * 43 * @returns value of this instance 44 */ 45 public unboxed(): byte { 46 return this.value; 47 } 48 49 /** 50 * Returns boxed representation of the primitive 51 * 52 * @param value value to box 53 * 54 * @returns boxed value 55 */ 56 public static valueOf(value: byte): Byte { 57 // TODO(ivan-tyulyandin): caching is possible 58 return new Byte(value); 59 } 60 61 /** 62 * Minimal value that this type can have as an integral 63 */ 64 public static MIN_VALUE: byte = -128; 65 66 /** 67 * Maximal value that this type can have as an integral 68 */ 69 public static MAX_VALUE: byte = 127; 70 71 /** 72 * Size of this type in bits 73 */ 74 public static BIT_SIZE: byte = 8; 75 76 /** 77 * Size of this type in bytes 78 */ 79 public static BYTE_SIZE: byte = 1; 80 81 /** 82 * Returns value of this instance 83 * 84 * @returns value as byte 85 */ 86 public override toByte(): byte { 87 return this.value; 88 } 89 90 /** 91 * Returns value of this instance 92 * 93 * @returns value as short 94 */ 95 public override toShort(): short { 96 return this.value.toShort(); 97 } 98 99 /** 100 * Returns value of this instance 101 * 102 * @returns value as int 103 */ 104 public override toInt(): int { 105 return this.value.toInt(); 106 } 107 108 /** 109 * Returns value of this instance 110 * 111 * @returns value as long 112 */ 113 public override toLong(): long { 114 return this.value.toLong(); 115 } 116 117 /** 118 * Returns value of this instance 119 * 120 * @returns value as float 121 */ 122 public override toFloat(): float { 123 return this.value.toFloat(); 124 } 125 126 /** 127 * Returns value of this instance 128 * 129 * @returns value as double 130 */ 131 public override toDouble(): double { 132 return this.value.toDouble(); 133 } 134 135 /** 136 * Returns value of this instance 137 * 138 * @returns value as char 139 */ 140 public toChar(): char { 141 return this.value.toChar(); 142 } 143 144 /** 145 * Returns the primitive as short value 146 * 147 * @param value value to cast 148 * 149 * @returns casted value 150 */ 151 public static native toShort(value: byte): short; 152 153 /** 154 * Returns the primitive as int value 155 * 156 * @param value value to cast 157 * 158 * @returns casted value 159 */ 160 public static native toInt(value: byte): int; 161 162 /** 163 * Returns the primitive as long value 164 * 165 * @param value value to cast 166 * 167 * @returns casted value 168 */ 169 public static native toLong(value: byte): long; 170 171 /** 172 * Returns the primitive as float value 173 * 174 * @param value value to cast 175 * 176 * @returns casted value 177 */ 178 public static native toFloat(value: byte): float; 179 180 /** 181 * Returns the primitive as double value 182 * 183 * @param value value to cast 184 * 185 * @returns casted value 186 */ 187 public static native toDouble(value: byte): double; 188 189 /** 190 * Returns the primitive as char value 191 * 192 * @param value value to cast 193 * 194 * @returns casted value 195 */ 196 public static native toChar(value: byte): char; 197 198 /** 199 * Returns the primitive as byte value 200 * 201 * @param value value to cast 202 * 203 * @returns casted value 204 */ 205 public static toByte(value: byte): byte { 206 return value; 207 } 208 209 /** 210 * Compares this instance to other Byte object 211 * The result is less than 0 if this instance lesser than provided object 212 * 0 if they are equal 213 * and greater than 0 otherwise. 214 * 215 * @param other Byte object to compare with 216 * 217 * @returns result of the comparison 218 */ 219 public override compareTo(other: Byte): int { 220 return (this.value - other.unboxed()) as int; 221 } 222 223 /** 224 * Converts this object to a string 225 * 226 * @returns result of the conversion 227 */ 228 public override toString(): String { 229 // Check for zero, log10 would go inf otherwise 230 if (this.value == 0) { 231 return "0"; 232 } 233 let val = this.value; 234 let negative: boolean = (val < 0); 235 let digitsNum = Double.toInt(log10(abs(this.value)) + 1); 236 if (negative) { 237 ++digitsNum; 238 } 239 let data : FixedArray<char> = new char[digitsNum]; 240 let curPlace = digitsNum - 1; 241 while (val != 0) { 242 let remainder = val % 10; 243 if (negative) { 244 remainder = -remainder; 245 } 246 data[curPlace] = (remainder + c'0').toChar(); 247 val /= 10; 248 curPlace -= 1; 249 } 250 if (negative) { 251 data[0] = c'-'; 252 } 253 return new String(data); 254 } 255 256 /** 257 * Converts this object to a string 258 * 259 * @returns result of the conversion 260 */ 261 public toString(radix: number): string { 262 return (new Long(this.value)).toString(radix); 263 } 264 265 /** 266 * Returns a hash code (integer representation) for this instance 267 * 268 * @returns representation of this instance 269 */ 270 public override $_hashCode(): int { 271 return this.toInt(); 272 } 273 274 /** 275 * Checks for equality this instance with provided object, treated as a Byte 276 * 277 * @param other object to be checked against 278 * 279 * @returns true if provided object and this instance have same value, false otherwise 280 * Returns false if type of provided object is not the same as this type 281 */ 282 public equals(other: NullishType): boolean { 283 if (this === other) { 284 return true 285 } 286 287 if (!(other instanceof Byte)) { 288 return false 289 } 290 291 return this.value == (other as Byte).value 292 } 293 294 /** 295 * Performs integral addition of this instance with provided one, returns the result as new instance 296 * 297 * @param other Right hand side of the addition 298 * 299 * @returns Result of the addition 300 */ 301 public add(other: Byte): Byte { 302 return (this.value + other.toByte()).toByte() 303 } 304 305 /** 306 * Performs integral subtraction of this instance with provided one, returns the result as new instance 307 * 308 * @param other Right hand side of the subtraction 309 * 310 * @returns Result of the subtraction 311 */ 312 public sub(other: Byte): Byte { 313 return (this.value - other.toByte()).toByte() 314 } 315 316 /** 317 * Performs integral multiplication of this instance with provided one, returns the result as new instance 318 * 319 * @param other Right hand side of the multiplication 320 * 321 * @returns Result of the multiplication 322 */ 323 public mul(other: Byte): Byte { 324 return (this.value * other.toByte()).toByte() 325 } 326 327 /** 328 * Performs integral division of this instance with provided one, returns the result as new instance 329 * 330 * @param other Right hand side of the division 331 * 332 * @returns Result of the division 333 */ 334 public div(other: Byte): Byte { 335 return (this.value / other.toByte()).toByte() 336 } 337 338 /** 339 * Checks if this instance value is less than value of provided instance 340 * 341 * @param other Right hand side of the comparison 342 * 343 * @returns true if this value is less than provided, false otherwise 344 */ 345 public isLessThan(other: Byte): boolean { 346 return this.value < other.toByte(); 347 } 348 349 /** 350 * Checks if this instance value is less than or equal to value of provided instance 351 * 352 * @param other Right hand side of the comparison 353 * 354 * @returns true if this value is less than or equal to provided, false otherwise 355 */ 356 public isLessEqualThan(other: Byte): boolean { 357 return this.value <= other.toByte(); 358 } 359 360 /** 361 * Checks if this instance value is greater than value of provided instance 362 * 363 * @param other Right hand side of the comparison 364 * 365 * @returns true if this value is greater than provided, false otherwise 366 */ 367 public isGreaterThan(other: Byte): boolean { 368 return this.value > other.toByte(); 369 } 370 371 /** 372 * Checks if this instance value is greater than or equal to value of provided instance 373 * 374 * @param other Right hand side of the comparison 375 * 376 * @returns true if this value is greater than or equal to provided, false otherwise 377 */ 378 public isGreaterEqualThan(other: Byte): boolean { 379 return this.value >= other.toByte(); 380 } 381 382 383 /** 384 * Creates a Byte instance based on JSONValue 385 * 386 * @param json: JSONValue - a JSON representation 387 * 388 * @throws JSONTypeError if json does not encode a valid byte 389 * 390 * @returns Byte - byte value decoded from JSON 391 */ 392 public createFromJSONValue(json: JSONValue): Byte { 393 if (json instanceof JSONNumber) { 394 let num = (json as JSONNumber).value 395 if (Double.isInteger(num) && Byte.MIN_VALUE <= num && num <= Byte.MAX_VALUE) { 396 return num.toByte() 397 } 398 } 399 throw new JSONTypeError("Cannot create Byte from JSON", new ErrorOptions(json as Object)) 400 } 401 402 } 403