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