1 /* 2 * Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 28 import jdk.internal.vm.annotation.IntrinsicCandidate; 29 30 // BEGIN Android-removed: dynamic constants not supported on Android. 31 /* 32 import java.lang.constant.Constable; 33 import java.lang.constant.ConstantDesc; 34 import java.lang.constant.ConstantDescs; 35 import java.lang.constant.DynamicConstantDesc; 36 import java.util.Optional; 37 38 import static java.lang.constant.ConstantDescs.BSM_GET_STATIC_FINAL; 39 import static java.lang.constant.ConstantDescs.CD_Boolean; 40 */ 41 // END Android-removed: dynamic constants not supported on Android. 42 43 /** 44 * The Boolean class wraps a value of the primitive type 45 * {@code boolean} in an object. An object of type 46 * {@code Boolean} contains a single field whose type is 47 * {@code boolean}. 48 * 49 * <p>In addition, this class provides many methods for 50 * converting a {@code boolean} to a {@code String} and a 51 * {@code String} to a {@code boolean}, as well as other 52 * constants and methods useful when dealing with a 53 * {@code boolean}. 54 * 55 * <!-- Android-removed: paragraph on ValueBased 56 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a> 57 * class; programmers should treat instances that are 58 * {@linkplain #equals(Object) equal} as interchangeable and should not 59 * use instances for synchronization, or unpredictable behavior may 60 * occur. For example, in a future release, synchronization may fail. 61 * --> 62 * 63 * @author Arthur van Hoff 64 * @since 1.0 65 */ 66 @jdk.internal.ValueBased 67 public final class Boolean implements java.io.Serializable, 68 Comparable<Boolean> 69 // Android-removed: no Constable support. 70 // , Constable 71 { 72 /** 73 * The {@code Boolean} object corresponding to the primitive 74 * value {@code true}. 75 */ 76 public static final Boolean TRUE = new Boolean(true); 77 78 /** 79 * The {@code Boolean} object corresponding to the primitive 80 * value {@code false}. 81 */ 82 public static final Boolean FALSE = new Boolean(false); 83 84 /** 85 * The Class object representing the primitive type boolean. 86 * 87 * @since 1.1 88 */ 89 @SuppressWarnings("unchecked") 90 public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean"); 91 92 /** 93 * The value of the Boolean. 94 * 95 * @serial 96 */ 97 private final boolean value; 98 99 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 100 @java.io.Serial 101 private static final long serialVersionUID = -3665804199014368530L; 102 103 /** 104 * Allocates a {@code Boolean} object representing the 105 * {@code value} argument. 106 * 107 * @param value the value of the {@code Boolean}. 108 * 109 * @deprecated 110 * It is rarely appropriate to use this constructor. The static factory 111 * {@link #valueOf(boolean)} is generally a better choice, as it is 112 * likely to yield significantly better space and time performance. 113 * Also consider using the final fields {@link #TRUE} and {@link #FALSE} 114 * if possible. 115 */ 116 // Android-changed: not yet forRemoval on Android. 117 @Deprecated(since="9"/*, forRemoval = true*/) Boolean(boolean value)118 public Boolean(boolean value) { 119 this.value = value; 120 } 121 122 /** 123 * Allocates a {@code Boolean} object representing the value 124 * {@code true} if the string argument is not {@code null} 125 * and is equal, ignoring case, to the string {@code "true"}. 126 * Otherwise, allocates a {@code Boolean} object representing the 127 * value {@code false}. 128 * 129 * @param s the string to be converted to a {@code Boolean}. 130 * 131 * @deprecated 132 * It is rarely appropriate to use this constructor. 133 * Use {@link #parseBoolean(String)} to convert a string to a 134 * {@code boolean} primitive, or use {@link #valueOf(String)} 135 * to convert a string to a {@code Boolean} object. 136 */ 137 // Android-changed: not yet forRemoval on Android. 138 @Deprecated(since="9"/*, forRemoval = true*/) Boolean(String s)139 public Boolean(String s) { 140 this(parseBoolean(s)); 141 } 142 143 /** 144 * Parses the string argument as a boolean. The {@code boolean} 145 * returned represents the value {@code true} if the string argument 146 * is not {@code null} and is equal, ignoring case, to the string 147 * {@code "true"}. 148 * Otherwise, a false value is returned, including for a null 149 * argument.<p> 150 * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br> 151 * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}. 152 * 153 * @param s the {@code String} containing the boolean 154 * representation to be parsed 155 * @return the boolean represented by the string argument 156 * @since 1.5 157 */ parseBoolean(String s)158 public static boolean parseBoolean(String s) { 159 return "true".equalsIgnoreCase(s); 160 } 161 162 /** 163 * Returns the value of this {@code Boolean} object as a boolean 164 * primitive. 165 * 166 * @return the primitive {@code boolean} value of this object. 167 */ 168 @IntrinsicCandidate booleanValue()169 public boolean booleanValue() { 170 return value; 171 } 172 173 /** 174 * Returns a {@code Boolean} instance representing the specified 175 * {@code boolean} value. If the specified {@code boolean} value 176 * is {@code true}, this method returns {@code Boolean.TRUE}; 177 * if it is {@code false}, this method returns {@code Boolean.FALSE}. 178 * If a new {@code Boolean} instance is not required, this method 179 * should generally be used in preference to the constructor 180 * {@link #Boolean(boolean)}, as this method is likely to yield 181 * significantly better space and time performance. 182 * 183 * @param b a boolean value. 184 * @return a {@code Boolean} instance representing {@code b}. 185 * @since 1.4 186 */ 187 @IntrinsicCandidate valueOf(boolean b)188 public static Boolean valueOf(boolean b) { 189 return (b ? TRUE : FALSE); 190 } 191 192 /** 193 * Returns a {@code Boolean} with a value represented by the 194 * specified string. The {@code Boolean} returned represents a 195 * true value if the string argument is not {@code null} 196 * and is equal, ignoring case, to the string {@code "true"}. 197 * Otherwise, a false value is returned, including for a null 198 * argument. 199 * 200 * @param s a string. 201 * @return the {@code Boolean} value represented by the string. 202 */ valueOf(String s)203 public static Boolean valueOf(String s) { 204 return parseBoolean(s) ? TRUE : FALSE; 205 } 206 207 /** 208 * Returns a {@code String} object representing the specified 209 * boolean. If the specified boolean is {@code true}, then 210 * the string {@code "true"} will be returned, otherwise the 211 * string {@code "false"} will be returned. 212 * 213 * @param b the boolean to be converted 214 * @return the string representation of the specified {@code boolean} 215 * @since 1.4 216 */ toString(boolean b)217 public static String toString(boolean b) { 218 return b ? "true" : "false"; 219 } 220 221 /** 222 * Returns a {@code String} object representing this Boolean's 223 * value. If this object represents the value {@code true}, 224 * a string equal to {@code "true"} is returned. Otherwise, a 225 * string equal to {@code "false"} is returned. 226 * 227 * @return a string representation of this object. 228 */ toString()229 public String toString() { 230 return value ? "true" : "false"; 231 } 232 233 /** 234 * Returns a hash code for this {@code Boolean} object. 235 * 236 * @return the integer {@code 1231} if this object represents 237 * {@code true}; returns the integer {@code 1237} if this 238 * object represents {@code false}. 239 */ 240 @Override hashCode()241 public int hashCode() { 242 return Boolean.hashCode(value); 243 } 244 245 /** 246 * Returns a hash code for a {@code boolean} value; compatible with 247 * {@code Boolean.hashCode()}. 248 * 249 * @param value the value to hash 250 * @return a hash code value for a {@code boolean} value. 251 * @since 1.8 252 */ hashCode(boolean value)253 public static int hashCode(boolean value) { 254 return value ? 1231 : 1237; 255 } 256 257 /** 258 * Returns {@code true} if and only if the argument is not 259 * {@code null} and is a {@code Boolean} object that 260 * represents the same {@code boolean} value as this object. 261 * 262 * @param obj the object to compare with. 263 * @return {@code true} if the Boolean objects represent the 264 * same value; {@code false} otherwise. 265 */ equals(Object obj)266 public boolean equals(Object obj) { 267 if (obj instanceof Boolean) { 268 return value == ((Boolean)obj).booleanValue(); 269 } 270 return false; 271 } 272 273 /** 274 * Returns {@code true} if and only if the system property named 275 * by the argument exists and is equal to, ignoring case, the 276 * string {@code "true"}. 277 * A system property is accessible through {@code getProperty}, a 278 * method defined by the {@code System} class. <p> If there is no 279 * property with the specified name, or if the specified name is 280 * empty or null, then {@code false} is returned. 281 * 282 * @param name the system property name. 283 * @return the {@code boolean} value of the system property. 284 * @throws SecurityException for the same reasons as 285 * {@link System#getProperty(String) System.getProperty} 286 * @see java.lang.System#getProperty(java.lang.String) 287 * @see java.lang.System#getProperty(java.lang.String, java.lang.String) 288 */ getBoolean(String name)289 public static boolean getBoolean(String name) { 290 boolean result = false; 291 try { 292 result = parseBoolean(System.getProperty(name)); 293 } catch (IllegalArgumentException | NullPointerException e) { 294 } 295 return result; 296 } 297 298 /** 299 * Compares this {@code Boolean} instance with another. 300 * 301 * @param b the {@code Boolean} instance to be compared 302 * @return zero if this object represents the same boolean value as the 303 * argument; a positive value if this object represents true 304 * and the argument represents false; and a negative value if 305 * this object represents false and the argument represents true 306 * @throws NullPointerException if the argument is {@code null} 307 * @see Comparable 308 * @since 1.5 309 */ compareTo(Boolean b)310 public int compareTo(Boolean b) { 311 return compare(this.value, b.value); 312 } 313 314 /** 315 * Compares two {@code boolean} values. 316 * The value returned is identical to what would be returned by: 317 * <pre> 318 * Boolean.valueOf(x).compareTo(Boolean.valueOf(y)) 319 * </pre> 320 * 321 * @param x the first {@code boolean} to compare 322 * @param y the second {@code boolean} to compare 323 * @return the value {@code 0} if {@code x == y}; 324 * a value less than {@code 0} if {@code !x && y}; and 325 * a value greater than {@code 0} if {@code x && !y} 326 * @since 1.7 327 */ compare(boolean x, boolean y)328 public static int compare(boolean x, boolean y) { 329 return (x == y) ? 0 : (x ? 1 : -1); 330 } 331 332 /** 333 * Returns the result of applying the logical AND operator to the 334 * specified {@code boolean} operands. 335 * 336 * @param a the first operand 337 * @param b the second operand 338 * @return the logical AND of {@code a} and {@code b} 339 * @see java.util.function.BinaryOperator 340 * @since 1.8 341 */ logicalAnd(boolean a, boolean b)342 public static boolean logicalAnd(boolean a, boolean b) { 343 return a && b; 344 } 345 346 /** 347 * Returns the result of applying the logical OR operator to the 348 * specified {@code boolean} operands. 349 * 350 * @param a the first operand 351 * @param b the second operand 352 * @return the logical OR of {@code a} and {@code b} 353 * @see java.util.function.BinaryOperator 354 * @since 1.8 355 */ logicalOr(boolean a, boolean b)356 public static boolean logicalOr(boolean a, boolean b) { 357 return a || b; 358 } 359 360 /** 361 * Returns the result of applying the logical XOR operator to the 362 * specified {@code boolean} operands. 363 * 364 * @param a the first operand 365 * @param b the second operand 366 * @return the logical XOR of {@code a} and {@code b} 367 * @see java.util.function.BinaryOperator 368 * @since 1.8 369 */ logicalXor(boolean a, boolean b)370 public static boolean logicalXor(boolean a, boolean b) { 371 return a ^ b; 372 } 373 374 // BEGIN Android-removed: dynamic constants not supported on Android. 375 /** 376 * Returns an {@link Optional} containing the nominal descriptor for this 377 * instance. 378 * 379 * @return an {@link Optional} describing the {@linkplain Boolean} instance 380 * @since 15 381 * 382 @Override 383 public Optional<DynamicConstantDesc<Boolean>> describeConstable() { 384 return Optional.of(value ? ConstantDescs.TRUE : ConstantDescs.FALSE); 385 } 386 */ 387 // END Android-removed: dynamic constants not supported on Android. 388 } 389