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