• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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     * Returns value of this instance as a primitive
42     *
43     * @returns value of this instance
44     */
45    public unboxed(): short {
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: short): Short {
57        // TODO(ivan-tyulyandin): caching is possible
58        return new Short(value);
59    }
60
61    /**
62     * Minimal value that this type can have as an shortegral
63     */
64    public static MIN_VALUE: short = -32768;
65
66    /**
67     * Maximal value that this type can have as an shortegral
68     */
69    public static MAX_VALUE: short = 32767;
70
71    /**
72     * Size of this type in bits
73     */
74    public static BIT_SIZE: byte = 16;
75
76    /**
77     * Size of this type in bytes
78     */
79    public static BYTE_SIZE: byte = 2;
80
81    /**
82     * Returns value of this instance
83     *
84     * @returns value as short
85     */
86    public override toByte(): byte {
87        return (this.value).toByte();
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;
97    }
98
99    /**
100     * Returns value of this instance
101     *
102     * @returns value as int
103     */
104    public override toInt(): int {
105        return Short.toInt(this.value);
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 Short.toFloat(this.value);
124    }
125
126    /**
127     * Returns value of this instance
128     *
129     * @returns value as double
130     */
131    public override toDouble(): double {
132        return Short.toDouble(this.value);
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 byte value
146     *
147     * @param value value to cast
148     *
149     * @returns casted value
150     */
151    public static native toByte(value: short): byte;
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: short): 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: short): 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: short): 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: short): 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: short): char;
197
198    /**
199     * Returns the primitive as short value
200     *
201     * @param value value to cast
202     *
203     * @returns casted value
204     */
205    public static toShort(value: short): short {
206        return value;
207    }
208
209    /**
210     * Compares this instance to other Short 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 Short object to compare with
216     *
217     * @returns result of the comparison
218     */
219    public override compareTo(other: Short): 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        return StringBuilder.toString(this.value);
230    }
231
232    /**
233     * Converts this object to a string
234     *
235     * @returns result of the conversion
236     */
237    public toString(radix: number): string {
238        return (new Long(this.value)).toString(radix);
239    }
240
241    /**
242     * Returns a hash code (shorteger representation) for this instance
243     *
244     * @returns representation of this instance
245     */
246    public override $_hashCode(): int {
247        return this.toInt();
248    }
249
250    /**
251    * Checks for equality this instance with provided object, treated as a Short
252    *
253    * @param other object to be checked against
254    *
255    * @returns true if provided object and this instance have same value, false otherwise
256    * Returns false if type of provided object is not the same as this type
257    */
258    public equals(other: NullishType): boolean {
259        if (this === other) {
260            return true
261        }
262
263        if (!(other instanceof Short)) {
264            return false
265        }
266
267        return this.value == (other as Short).value
268    }
269
270    /**
271     * Performs shortegral addition of this instance with provided one, returns the result as new instance
272     *
273     * @param other Right hand side of the addition
274     *
275     * @returns Result of the addition
276     */
277    public add(other: Short): Short {
278        return Short.valueOf((this.value + other.toShort()).toShort())
279    }
280
281    /**
282     * Performs shortegral subtraction of this instance with provided one, returns the result as new instance
283     *
284     * @param other Right hand side of the subtraction
285     *
286     * @returns Result of the subtraction
287     */
288    public sub(other: Short): Short {
289        return Short.valueOf((this.value - other.toShort()).toShort())
290    }
291
292    /**
293     * Performs shortegral multiplication of this instance with provided one, returns the result as new instance
294     *
295     * @param other Right hand side of the multiplication
296     *
297     * @returns Result of the multiplication
298     */
299    public mul(other: Short): Short {
300        return Short.valueOf((this.value * other.toShort()).toShort())
301    }
302
303    /**
304     * Performs shortegral division of this instance with provided one, returns the result as new instance
305     *
306     * @param other Right hand side of the division
307     *
308     * @returns Result of the division
309     */
310    public div(other: Short): Short {
311        return Short.valueOf((this.value / other.toShort()).toShort())
312    }
313
314    /**
315     * Checks if this instance value is less than value of provided instance
316     *
317     * @param other Right hand side of the comparison
318     *
319     * @returns true if this value is less than provided, false otherwise
320     */
321    public isLessThan(other: Short): boolean {
322        return this.value < other.toShort();
323    }
324
325    /**
326     * Checks if this instance value is less than or equal to value of provided instance
327     *
328     * @param other Right hand side of the comparison
329     *
330     * @returns true if this value is less than or equal to provided, false otherwise
331     */
332    public isLessEqualThan(other: Short): boolean {
333        return this.value <= other.toShort();
334    }
335
336    /**
337     * Checks if this instance value is greater than value of provided instance
338     *
339     * @param other Right hand side of the comparison
340     *
341     * @returns true if this value is greater than provided, false otherwise
342     */
343    public isGreaterThan(other: Short): boolean {
344        return this.value > other.toShort();
345    }
346
347    /**
348     * Checks if this instance value is greater than or equal to value of provided instance
349     *
350     * @param other Right hand side of the comparison
351     *
352     * @returns true if this value is greater than or equal to provided, false otherwise
353     */
354    public isGreaterEqualThan(other: Short): boolean {
355        return this.value >= other.toShort();
356    }
357
358    /**
359     * Creates a Short instance based on JSONValue
360     *
361     * @param json: JSONValue - a JSON representation
362     *
363     * @throws JSONTypeError if json does not encode a valid short
364     *
365     * @returns Short - short value decoded from JSON
366     */
367    public createFromJSONValue(json: JSONValue): Short {
368        if (json instanceof JSONNumber) {
369            let num = (json as JSONNumber).value
370            if (Double.isInteger(num) && Short.MIN_VALUE <= num && num <= Short.MAX_VALUE) {
371                return Short.valueOf(num.toShort())
372            }
373        }
374        throw new JSONTypeError("Cannot create Short from JSON", new ErrorOptions(json as Object))
375    }
376}
377