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 boolean value and related operations 20 */ 21export final class Boolean extends Object implements Comparable<Boolean> { 22 private value: boolean; 23 24 /** 25 * Constructs a new Boolean with provided value 26 * 27 * @param value value to construct class instance with 28 */ 29 public constructor(value: boolean) { 30 this.value = value 31 } 32 33 /** 34 * Constructs a new Boolean with provided byte value 35 * 36 * @param value value to construct class instance with 37 */ 38 public constructor(value: byte) { 39 this.value = value != 0; 40 } 41 42 /** 43 * Constructs a new Boolean with provided short value 44 * 45 * @param value value to construct class instance with 46 */ 47 public constructor(value: short) { 48 this.value = value != 0; 49 } 50 51 /** 52 * Constructs a new Boolean with provided int value 53 * 54 * @param value value to construct class instance with 55 */ 56 public constructor(value: int) { 57 this.value = value != 0; 58 } 59 60 /** 61 * Constructs a new Boolean with provided long value 62 * 63 * @param value value to construct class instance with 64 */ 65 public constructor(value: long) { 66 this.value = value != 0; 67 } 68 69 /** 70 * Constructs a new Boolean with provided bigint value 71 * 72 * @param value value to construct class instance with 73 */ 74 public constructor(value: bigint) { 75 this.value = value != new BigInt(0); 76 } 77 78 /** 79 * Constructs a new Boolean with provided char value 80 * 81 * @param value value to construct class instance with 82 */ 83 public constructor(value: char) { 84 this.value = value != c'\u0000'; 85 } 86 87 /** 88 * Constructs a new Boolean with provided float value 89 * 90 * @param value value to construct class instance with 91 */ 92 public constructor(value: float) { 93 this.value = (value != 0.0 && !isNaN(value)); 94 } 95 96 /** 97 * Constructs a new Boolean with provided number value 98 * 99 * @param value value to construct class instance with 100 */ 101 public constructor(value: number) { 102 this.value = (value != 0 && !isNaN(value)); 103 } 104 105 106 /** 107 * Constructs a new Boolean with provided string value 108 * 109 * @param value value to construct class instance with 110 */ 111 public constructor(value: string) { 112 this.value = value != ""; 113 } 114 115 /** 116 * Constructs a new Boolean with provided Object value 117 * 118 * @param value value to construct class instance with 119 */ 120 public constructor(value: NullishType = undefined) { 121 if (value == null || value == undefined) { 122 this.value = false; 123 } else if (value instanceof Boolean) { 124 this.value = value.unboxed(); 125 } else if (value instanceof Number) { 126 this.value = (value.unboxed() != 0 && !isNaN(value.unboxed())); 127 } else if (value instanceof Char) { 128 this.value = value.unboxed() != c'\u0000'; 129 } else if (value instanceof Float) { 130 this.value = (value.unboxed() != 0.0 && !isNaN(value.unboxed())); 131 } else if (value instanceof Long) { 132 this.value = value.unboxed() != 0; 133 } else if (value instanceof Int) { 134 this.value = value.unboxed() != 0; 135 } else if (value instanceof Byte) { 136 this.value = value.unboxed() != 0; 137 } else if (value instanceof Short) { 138 this.value = value.unboxed() != 0; 139 } else if (value instanceof String) { 140 this.value = value != ""; 141 } else { 142 this.value = false; 143 } 144 } 145 146 /** 147 * Creates a new instance of a Boolean based on the specified value 148 * 149 * @param value The value to be converted to a Boolean (optional). 150 * 151 * @returns A new Boolean instance representing the specified value. 152 */ 153 static invoke(): boolean { 154 return Boolean.FALSE; 155 } 156 157 /** 158 * Creates a new instance of a Boolean based on the specified value 159 * 160 * @param value The value to be converted to a Boolean (optional). 161 * 162 * @returns A new Boolean instance representing the specified value. 163 */ 164 static invoke<T>(value: T): boolean { 165 return new Boolean(value).valueOf(); 166 } 167 168 /** 169 * Returns the value of this instance as a primitive 170 * 171 * @returns primitive boolean value 172 */ 173 public unboxed(): boolean { 174 return this.value; 175 } 176 177 /** 178 * Static instance that represents true boolean value 179 */ 180 public static readonly TRUE = new Boolean(true); 181 182 /** 183 * Static instance that represents false boolean value 184 */ 185 public static readonly FALSE = new Boolean(false); 186 187 /** 188 * Static method that converts primitive boolean to boxed version 189 * 190 * @param b value to be converted 191 * 192 * @returns boxed value that represents provided primitive value 193 */ 194 public static valueOf(b: boolean): Boolean { 195 if (b) { 196 return Boolean.TRUE; 197 }; 198 return Boolean.FALSE; 199 } 200 201 /** 202 * Returns the value of this instance as primitive 203 * 204 * @returns primitive boolean value 205 */ 206 public valueOf(): boolean { 207 return this.value 208 } 209 210 /** 211 * Checks for equality this instance with provided object, treated as a Boolean 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 Boolean)) { 224 return false 225 } 226 227 return this.value == (other as Boolean).value 228 } 229 230 /** 231 * Returns a hash code (integer representation) for this instance 232 * 233 * @returns representation of this instance 234 */ 235 public override $_hashCode(): int { 236 if (this.value) { 237 return 1; 238 } 239 return 0; 240 } 241 242 /** 243 * Converts this object to a string 244 * 245 * @returns "True" if this instance is true, "False" otherwise 246 */ 247 public override toString(): String { 248 if (this.value) { 249 return "true"; 250 } 251 return "false"; 252 } 253 254 /** 255 * Compares this instance to other Boolean object 256 * The result is less than 0 if this instance lesser than provided object 257 * 0 if they are equal 258 * and greater than 0 otherwise. 259 * 260 * @param other Boolean object to compare with 261 * 262 * @returns result of the comparison 263 */ 264 public override compareTo(other: Boolean): int { 265 return this.$_hashCode() - other.$_hashCode(); 266 } 267 268 /** 269 * Checks if this instance is true 270 * 271 * @returns true if this instance is true, false otherwise 272 */ 273 public isTrue(): boolean { 274 return this.value == true; 275 } 276 277 /** 278 * Checks if this instance is false 279 * 280 * @returns true if this instance is false, false otherwise 281 */ 282 public isFalse(): boolean { 283 return this.value == false; 284 } 285 286 /** 287 * Inverts this instance value 288 * 289 * @returns true if value was false and vice versa 290 */ 291 public negate(): Boolean { 292 return Boolean.valueOf(!this.value) 293 } 294 295 /** 296 * Does logical `and` on this instance and provided instance 297 * 298 * @param other provided instance 299 * 300 * @returns value as a result of logical `and` 301 */ 302 public and(other: Boolean): Boolean { 303 return Boolean.valueOf(this.value && other.unboxed()) 304 } 305 306 /** 307 * Does logical `or` on this instance and provided instance 308 * 309 * @param other provided instance 310 * 311 * @returns value as a result of logical `or` 312 */ 313 public or(other: Boolean): Boolean { 314 return Boolean.valueOf(this.value || other.unboxed()) 315 } 316 317 /** 318 * Does `xor` on this instance and provided instance 319 * 320 * @param other provided instance 321 * 322 * @returns value as a result of `xor` 323 */ 324 public xor(other: Boolean): Boolean { 325 return Boolean.valueOf(this.value ^ other.unboxed()) 326 } 327 328 /** 329 * Creates a Boolean instance based on JSONValue 330 * 331 * @param json: JSONValue - a JSON representation 332 * 333 * @throws JSONTypeError if json does not encode a valid boolean literal 334 * 335 * @returns Boolean - true if JSON encodes true literal, false if JSON encodes false literal 336 */ 337 static createFromJSONValue(json: JSONValue): Boolean { 338 if (json instanceof JSONFalse) { 339 return Boolean.FALSE 340 } else if (json instanceof JSONTrue) { 341 return Boolean.TRUE 342 } 343 throw new JSONTypeError("Cannot create Boolean from JSON", new ErrorOptions(json as Object)) 344 } 345} 346