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