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