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 int value and related operations 20 */ 21export final class Int extends Integral implements Comparable<Int>, JSONable<Int> { 22 private value: int; 23 24 /** 25 * Constructs a new Int instance with initial value zero 26 */ 27 public constructor() { 28 this.value = 0; 29 } 30 31 /** 32 * Constructs a new Int instance with provided initial value 33 * 34 * @param value the initial value 35 */ 36 public constructor(value: int) { 37 this.value = value; 38 } 39 40 /** 41 * Constructs a new Int instance with provided initial value 42 * 43 * @param value the initial value 44 */ 45 public constructor(value: Int) { 46 this.value = value.intValue(); 47 } 48 49 /** 50 * Returns value of this instance as a primitive 51 * 52 * @returns value of this instance 53 */ 54 public unboxed(): int { 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: int): Int { 66 // TODO(ivan-tyulyandin): caching is possible 67 return new Int(value); 68 } 69 70 /** 71 * Minimal value that this type can have as an integral 72 */ 73 public static readonly MIN_VALUE: int = -2147483648; 74 75 /** 76 * Maximal value that this type can have as an integral 77 */ 78 public static readonly MAX_VALUE: int = 2147483647; 79 80 /** 81 * Size of this type in bits 82 */ 83 public static readonly BIT_SIZE: byte = 32; 84 85 /** 86 * Size of this type in bytes 87 */ 88 public static readonly BYTE_SIZE: byte = 4; 89 90 /** 91 * Returns value of this instance 92 * 93 * @returns value as int 94 */ 95 public override byteValue(): byte { 96 return this.value as byte; 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; 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 * Compares this instance to other Int object 146 * The result is less than 0 if this instance lesser than provided object 147 * 0 if they are equal 148 * and greater than 0 otherwise. 149 * 150 * @param other Int object to compare with 151 * 152 * @returns result of the comparison 153 */ 154 public override compareTo(other: Int): int { 155 return this.value - other.unboxed(); 156 } 157 158 /** 159 * Converts this object to a string 160 * 161 * @returns result of the conversion 162 */ 163 public override toString(): String { 164 return StringBuilder.toString(this.value); 165 } 166 167 /** 168 * Converts this object to a string 169 * 170 * @returns result of the conversion 171 */ 172 public toString(radix: number): string { 173 return (new Long(this.value)).toString(radix); 174 } 175 176 /** 177 * Returns a hash code (integer representation) for this instance 178 * 179 * @returns representation of this instance 180 */ 181 public override $_hashCode(): int { 182 return this.value; 183 } 184 185 equals(other: NullishType): boolean { 186 if (__runtimeIsSameReference(this, other)) { 187 return true 188 } 189 190 if (!(other instanceof Int)) { 191 return false 192 } 193 194 return this.value == (other as Int).value 195 } 196 197 /** 198 * Performs integral addition of this instance with provided one, returns the result as new instance 199 * 200 * @param other Right hand side of the addition 201 * 202 * @returns Result of the addition 203 */ 204 public add(other: Int): Int { 205 return Int.valueOf((this.value + other.intValue()) as int) 206 } 207 208 /** 209 * Performs integral subtraction of this instance with provided one, returns the result as new instance 210 * 211 * @param other Right hand side of the subtraction 212 * 213 * @returns Result of the subtraction 214 */ 215 public sub(other: Int): Int { 216 return Int.valueOf((this.value - other.intValue()) as int) 217 } 218 219 /** 220 * Performs integral multiplication of this instance with provided one, returns the result as new instance 221 * 222 * @param other Right hand side of the multiplication 223 * 224 * @returns Result of the multiplication 225 */ 226 public mul(other: Int): Int { 227 return Int.valueOf((this.value * other.intValue()) as int) 228 } 229 230 /** 231 * Performs integral division of this instance with provided one, returns the result as new instance 232 * 233 * @param other Right hand side of the division 234 * 235 * @returns Result of the division 236 */ 237 public div(other: Int): Int { 238 return Int.valueOf((this.value / other.intValue()) as int) 239 } 240 241 /** 242 * Checks if this instance value is less than value of provided instance 243 * 244 * @param other Right hand side of the comparison 245 * 246 * @returns true if this value is less than provided, false otherwise 247 */ 248 public isLessThan(other: Int): boolean { 249 return this.value < other.intValue(); 250 } 251 252 /** 253 * Checks if this instance value is less than or equal to value of provided instance 254 * 255 * @param other Right hand side of the comparison 256 * 257 * @returns true if this value is less than or equal to provided, false otherwise 258 */ 259 public isLessEqualThan(other: Int): boolean { 260 return this.value <= other.intValue(); 261 } 262 263 /** 264 * Checks if this instance value is greater than value of provided instance 265 * 266 * @param other Right hand side of the comparison 267 * 268 * @returns true if this value is greater than provided, false otherwise 269 */ 270 public isGreaterThan(other: Int): boolean { 271 return this.value > other.intValue(); 272 } 273 274 /** 275 * Checks if this instance value is greater than or equal to value of provided instance 276 * 277 * @param other Right hand side of the comparison 278 * 279 * @returns true if this value is greater than or equal to provided, false otherwise 280 */ 281 public isGreaterEqualThan(other: Int): boolean { 282 return this.value >= other.intValue(); 283 } 284 285 /** 286 * Creates a Int instance based on JSONValue 287 * 288 * @param json: JSONValue - a JSON representation 289 * 290 * @throws JSONTypeError if json does not encode a valid int 291 * 292 * @returns Int - int value decoded from JSON 293 */ 294 static createFromJSONValue(json: JSONValue): Int { 295 if (json instanceof JSONNumber) { 296 let num = (json as JSONNumber).value 297 if (Double.isInteger(num) && Int.MIN_VALUE <= num && num <= Int.MAX_VALUE) { 298 return Int.valueOf(num as int) 299 } 300 } 301 throw new JSONTypeError("Cannot create Int from JSON", new ErrorOptions(json as Object)) 302 } 303} 304