Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/Boolean.java
38829 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627/**28* The Boolean class wraps a value of the primitive type29* {@code boolean} in an object. An object of type30* {@code Boolean} contains a single field whose type is31* {@code boolean}.32* <p>33* In addition, this class provides many methods for34* converting a {@code boolean} to a {@code String} and a35* {@code String} to a {@code boolean}, as well as other36* constants and methods useful when dealing with a37* {@code boolean}.38*39* @author Arthur van Hoff40* @since JDK1.041*/42public final class Boolean implements java.io.Serializable,43Comparable<Boolean>44{45/**46* The {@code Boolean} object corresponding to the primitive47* value {@code true}.48*/49public static final Boolean TRUE = new Boolean(true);5051/**52* The {@code Boolean} object corresponding to the primitive53* value {@code false}.54*/55public static final Boolean FALSE = new Boolean(false);5657/**58* The Class object representing the primitive type boolean.59*60* @since JDK1.161*/62@SuppressWarnings("unchecked")63public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");6465/**66* The value of the Boolean.67*68* @serial69*/70private final boolean value;7172/** use serialVersionUID from JDK 1.0.2 for interoperability */73private static final long serialVersionUID = -3665804199014368530L;7475/**76* Allocates a {@code Boolean} object representing the77* {@code value} argument.78*79* <p><b>Note: It is rarely appropriate to use this constructor.80* Unless a <i>new</i> instance is required, the static factory81* {@link #valueOf(boolean)} is generally a better choice. It is82* likely to yield significantly better space and time performance.</b>83*84* @param value the value of the {@code Boolean}.85*/86public Boolean(boolean value) {87this.value = value;88}8990/**91* Allocates a {@code Boolean} object representing the value92* {@code true} if the string argument is not {@code null}93* and is equal, ignoring case, to the string {@code "true"}.94* Otherwise, allocate a {@code Boolean} object representing the95* value {@code false}. Examples:<p>96* {@code new Boolean("True")} produces a {@code Boolean} object97* that represents {@code true}.<br>98* {@code new Boolean("yes")} produces a {@code Boolean} object99* that represents {@code false}.100*101* @param s the string to be converted to a {@code Boolean}.102*/103public Boolean(String s) {104this(parseBoolean(s));105}106107/**108* Parses the string argument as a boolean. The {@code boolean}109* returned represents the value {@code true} if the string argument110* is not {@code null} and is equal, ignoring case, to the string111* {@code "true"}. <p>112* Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>113* Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.114*115* @param s the {@code String} containing the boolean116* representation to be parsed117* @return the boolean represented by the string argument118* @since 1.5119*/120public static boolean parseBoolean(String s) {121return ((s != null) && s.equalsIgnoreCase("true"));122}123124/**125* Returns the value of this {@code Boolean} object as a boolean126* primitive.127*128* @return the primitive {@code boolean} value of this object.129*/130public boolean booleanValue() {131return value;132}133134/**135* Returns a {@code Boolean} instance representing the specified136* {@code boolean} value. If the specified {@code boolean} value137* is {@code true}, this method returns {@code Boolean.TRUE};138* if it is {@code false}, this method returns {@code Boolean.FALSE}.139* If a new {@code Boolean} instance is not required, this method140* should generally be used in preference to the constructor141* {@link #Boolean(boolean)}, as this method is likely to yield142* significantly better space and time performance.143*144* @param b a boolean value.145* @return a {@code Boolean} instance representing {@code b}.146* @since 1.4147*/148public static Boolean valueOf(boolean b) {149return (b ? TRUE : FALSE);150}151152/**153* Returns a {@code Boolean} with a value represented by the154* specified string. The {@code Boolean} returned represents a155* true value if the string argument is not {@code null}156* and is equal, ignoring case, to the string {@code "true"}.157*158* @param s a string.159* @return the {@code Boolean} value represented by the string.160*/161public static Boolean valueOf(String s) {162return parseBoolean(s) ? TRUE : FALSE;163}164165/**166* Returns a {@code String} object representing the specified167* boolean. If the specified boolean is {@code true}, then168* the string {@code "true"} will be returned, otherwise the169* string {@code "false"} will be returned.170*171* @param b the boolean to be converted172* @return the string representation of the specified {@code boolean}173* @since 1.4174*/175public static String toString(boolean b) {176return b ? "true" : "false";177}178179/**180* Returns a {@code String} object representing this Boolean's181* value. If this object represents the value {@code true},182* a string equal to {@code "true"} is returned. Otherwise, a183* string equal to {@code "false"} is returned.184*185* @return a string representation of this object.186*/187public String toString() {188return value ? "true" : "false";189}190191/**192* Returns a hash code for this {@code Boolean} object.193*194* @return the integer {@code 1231} if this object represents195* {@code true}; returns the integer {@code 1237} if this196* object represents {@code false}.197*/198@Override199public int hashCode() {200return Boolean.hashCode(value);201}202203/**204* Returns a hash code for a {@code boolean} value; compatible with205* {@code Boolean.hashCode()}.206*207* @param value the value to hash208* @return a hash code value for a {@code boolean} value.209* @since 1.8210*/211public static int hashCode(boolean value) {212return value ? 1231 : 1237;213}214215/**216* Returns {@code true} if and only if the argument is not217* {@code null} and is a {@code Boolean} object that218* represents the same {@code boolean} value as this object.219*220* @param obj the object to compare with.221* @return {@code true} if the Boolean objects represent the222* same value; {@code false} otherwise.223*/224public boolean equals(Object obj) {225if (obj instanceof Boolean) {226return value == ((Boolean)obj).booleanValue();227}228return false;229}230231/**232* Returns {@code true} if and only if the system property233* named by the argument exists and is equal to the string234* {@code "true"}. (Beginning with version 1.0.2 of the235* Java<small><sup>TM</sup></small> platform, the test of236* this string is case insensitive.) A system property is accessible237* through {@code getProperty}, a method defined by the238* {@code System} class.239* <p>240* If there is no property with the specified name, or if the specified241* name is empty or null, then {@code false} is returned.242*243* @param name the system property name.244* @return the {@code boolean} value of the system property.245* @throws SecurityException for the same reasons as246* {@link System#getProperty(String) System.getProperty}247* @see java.lang.System#getProperty(java.lang.String)248* @see java.lang.System#getProperty(java.lang.String, java.lang.String)249*/250public static boolean getBoolean(String name) {251boolean result = false;252try {253result = parseBoolean(System.getProperty(name));254} catch (IllegalArgumentException | NullPointerException e) {255}256return result;257}258259/**260* Compares this {@code Boolean} instance with another.261*262* @param b the {@code Boolean} instance to be compared263* @return zero if this object represents the same boolean value as the264* argument; a positive value if this object represents true265* and the argument represents false; and a negative value if266* this object represents false and the argument represents true267* @throws NullPointerException if the argument is {@code null}268* @see Comparable269* @since 1.5270*/271public int compareTo(Boolean b) {272return compare(this.value, b.value);273}274275/**276* Compares two {@code boolean} values.277* The value returned is identical to what would be returned by:278* <pre>279* Boolean.valueOf(x).compareTo(Boolean.valueOf(y))280* </pre>281*282* @param x the first {@code boolean} to compare283* @param y the second {@code boolean} to compare284* @return the value {@code 0} if {@code x == y};285* a value less than {@code 0} if {@code !x && y}; and286* a value greater than {@code 0} if {@code x && !y}287* @since 1.7288*/289public static int compare(boolean x, boolean y) {290return (x == y) ? 0 : (x ? 1 : -1);291}292293/**294* Returns the result of applying the logical AND operator to the295* specified {@code boolean} operands.296*297* @param a the first operand298* @param b the second operand299* @return the logical AND of {@code a} and {@code b}300* @see java.util.function.BinaryOperator301* @since 1.8302*/303public static boolean logicalAnd(boolean a, boolean b) {304return a && b;305}306307/**308* Returns the result of applying the logical OR operator to the309* specified {@code boolean} operands.310*311* @param a the first operand312* @param b the second operand313* @return the logical OR of {@code a} and {@code b}314* @see java.util.function.BinaryOperator315* @since 1.8316*/317public static boolean logicalOr(boolean a, boolean b) {318return a || b;319}320321/**322* Returns the result of applying the logical XOR operator to the323* specified {@code boolean} operands.324*325* @param a the first operand326* @param b the second operand327* @return the logical XOR of {@code a} and {@code b}328* @see java.util.function.BinaryOperator329* @since 1.8330*/331public static boolean logicalXor(boolean a, boolean b) {332return a ^ b;333}334}335336337