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      * <p><b>Note: It is rarely appropriate to use this constructor.
  83      * Unless a <i>new</i> instance is required, the static factory
  84      * {@link #valueOf(boolean)} is generally a better choice. It is
  85      * likely to yield significantly better space and time performance.</b>
  86      * @deprecated use {@link #valueOf(boolean)} instead. 
  87      *
  88      * @param   value   the value of the {@code Boolean}.
  89      */
  90     @Deprecated
  91     public Boolean(boolean value) {
  92         this.value = value;
  93     }
  94 
  95     /**
  96      * Allocates a {@code Boolean} object representing the value
  97      * {@code true} if the string argument is not {@code null}
  98      * and is equal, ignoring case, to the string {@code "true"}.
  99      * Otherwise, allocate a {@code Boolean} object representing the
 100      * value {@code false}. Examples:<p>
 101      * {@code new Boolean("True")} produces a {@code Boolean} object
 102      * that represents {@code true}.<br>
 103      * {@code new Boolean("yes")} produces a {@code Boolean} object
 104      * that represents {@code false}.
 105      * @deprecated use {@link #valueOf(String)} instead. 
 106      *
 107      * @param   s   the string to be converted to a {@code Boolean}.
 108      */
 109     @Deprecated
 110     public Boolean(String s) {
 111         this(parseBoolean(s));
 112     }
 113 
 114     /**
 115      * Parses the string argument as a boolean.  The {@code boolean}
 116      * returned represents the value {@code true} if the string argument
 117      * is not {@code null} and is equal, ignoring case, to the string
 118      * {@code "true"}. <p>
 119      * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
 120      * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
 121      *
 122      * @param      s   the {@code String} containing the boolean
 123      *                 representation to be parsed
 124      * @return     the boolean represented by the string argument
 125      * @since 1.5
 126      */
 127     public static boolean parseBoolean(String s) {
 128         return ((s != null) && s.equalsIgnoreCase("true"));
 129     }
 130 
 131     /**
 132      * Returns the value of this {@code Boolean} object as a boolean
 133      * primitive.
 134      *
 135      * @return  the primitive {@code boolean} value of this object.
 136      */
 137     @HotSpotIntrinsicCandidate
 138     public boolean booleanValue() {
 139         return value;
 140     }
 141 
 142     /**
 143      * Returns a {@code Boolean} instance representing the specified
 144      * {@code boolean} value.  If the specified {@code boolean} value
 145      * is {@code true}, this method returns {@code Boolean.TRUE};
 146      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
 147      * If a new {@code Boolean} instance is not required, this method
 148      * should generally be used in preference to the constructor
 149      * {@link #Boolean(boolean)}, as this method is likely to yield
 150      * significantly better space and time performance.
 151      *
 152      * @param  b a boolean value.
 153      * @return a {@code Boolean} instance representing {@code b}.
 154      * @since  1.4
 155      */
 156     @HotSpotIntrinsicCandidate
 157     public static Boolean valueOf(boolean b) {
 158         return (b ? TRUE : FALSE);
 159     }
 160 
 161     /**
 162      * Returns a {@code Boolean} with a value represented by the
 163      * specified string.  The {@code Boolean} returned represents a
 164      * true value if the string argument is not {@code null}
 165      * and is equal, ignoring case, to the string {@code "true"}.
 166      *
 167      * @param   s   a string.
 168      * @return  the {@code Boolean} value represented by the string.
 169      */
 170     public static Boolean valueOf(String s) {
 171         return parseBoolean(s) ? TRUE : FALSE;
 172     }
 173 
 174     /**
 175      * Returns a {@code String} object representing the specified
 176      * boolean.  If the specified boolean is {@code true}, then
 177      * the string {@code "true"} will be returned, otherwise the
 178      * string {@code "false"} will be returned.
 179      *
 180      * @param b the boolean to be converted
 181      * @return the string representation of the specified {@code boolean}
 182      * @since 1.4
 183      */
 184     public static String toString(boolean b) {
 185         return b ? "true" : "false";
 186     }
 187 
 188     /**
 189      * Returns a {@code String} object representing this Boolean's
 190      * value.  If this object represents the value {@code true},
 191      * a string equal to {@code "true"} is returned. Otherwise, a
 192      * string equal to {@code "false"} is returned.
 193      *
 194      * @return  a string representation of this object.
 195      */
 196     public String toString() {
 197         return value ? "true" : "false";
 198     }
 199 
 200     /**
 201      * Returns a hash code for this {@code Boolean} object.
 202      *
 203      * @return  the integer {@code 1231} if this object represents
 204      * {@code true}; returns the integer {@code 1237} if this
 205      * object represents {@code false}.
 206      */
 207     @Override
 208     public int hashCode() {
 209         return Boolean.hashCode(value);
 210     }
 211 
 212     /**
 213      * Returns a hash code for a {@code boolean} value; compatible with
 214      * {@code Boolean.hashCode()}.
 215      *
 216      * @param value the value to hash
 217      * @return a hash code value for a {@code boolean} value.
 218      * @since 1.8
 219      */
 220     public static int hashCode(boolean value) {
 221         return value ? 1231 : 1237;
 222     }
 223 
 224    /**
 225      * Returns {@code true} if and only if the argument is not
 226      * {@code null} and is a {@code Boolean} object that
 227      * represents the same {@code boolean} value as this object.
 228      *
 229      * @param   obj   the object to compare with.
 230      * @return  {@code true} if the Boolean objects represent the
 231      *          same value; {@code false} otherwise.
 232      */
 233     public boolean equals(Object obj) {
 234         if (obj instanceof Boolean) {
 235             return value == ((Boolean)obj).booleanValue();
 236         }
 237         return false;
 238     }
 239 
 240     /**
 241      * Returns {@code true} if and only if the system property named
 242      * by the argument exists and is equal to the string {@code
 243      * "true"}. (Beginning with version 1.0.2 of the Java&trade;
 244      * platform, the test of this string is case insensitive.) A
 245      * system property is accessible through {@code getProperty}, a
 246      * method defined by the {@code System} class.
 247      * <p>
 248      * If there is no property with the specified name, or if the specified
 249      * name is empty or null, then {@code false} is returned.
 250      *
 251      * @param   name   the system property name.
 252      * @return  the {@code boolean} value of the system property.
 253      * @throws  SecurityException for the same reasons as
 254      *          {@link System#getProperty(String) System.getProperty}
 255      * @see     java.lang.System#getProperty(java.lang.String)
 256      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 257      */
 258     public static boolean getBoolean(String name) {
 259         boolean result = false;
 260         try {
 261             result = parseBoolean(System.getProperty(name));
 262         } catch (IllegalArgumentException | NullPointerException e) {
 263         }
 264         return result;
 265     }
 266 
 267     /**
 268      * Compares this {@code Boolean} instance with another.
 269      *
 270      * @param   b the {@code Boolean} instance to be compared
 271      * @return  zero if this object represents the same boolean value as the
 272      *          argument; a positive value if this object represents true
 273      *          and the argument represents false; and a negative value if
 274      *          this object represents false and the argument represents true
 275      * @throws  NullPointerException if the argument is {@code null}
 276      * @see     Comparable
 277      * @since  1.5
 278      */
 279     public int compareTo(Boolean b) {
 280         return compare(this.value, b.value);
 281     }
 282 
 283     /**
 284      * Compares two {@code boolean} values.
 285      * The value returned is identical to what would be returned by:
 286      * <pre>
 287      *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
 288      * </pre>
 289      *
 290      * @param  x the first {@code boolean} to compare
 291      * @param  y the second {@code boolean} to compare
 292      * @return the value {@code 0} if {@code x == y};
 293      *         a value less than {@code 0} if {@code !x && y}; and
 294      *         a value greater than {@code 0} if {@code x && !y}
 295      * @since 1.7
 296      */
 297     public static int compare(boolean x, boolean y) {
 298         return (x == y) ? 0 : (x ? 1 : -1);
 299     }
 300 
 301     /**
 302      * Returns the result of applying the logical AND operator to the
 303      * specified {@code boolean} operands.
 304      *
 305      * @param a the first operand
 306      * @param b the second operand
 307      * @return the logical AND of {@code a} and {@code b}
 308      * @see java.util.function.BinaryOperator
 309      * @since 1.8
 310      */
 311     public static boolean logicalAnd(boolean a, boolean b) {
 312         return a && b;
 313     }
 314 
 315     /**
 316      * Returns the result of applying the logical OR operator to the
 317      * specified {@code boolean} operands.
 318      *
 319      * @param a the first operand
 320      * @param b the second operand
 321      * @return the logical OR of {@code a} and {@code b}
 322      * @see java.util.function.BinaryOperator
 323      * @since 1.8
 324      */
 325     public static boolean logicalOr(boolean a, boolean b) {
 326         return a || b;
 327     }
 328 
 329     /**
 330      * Returns the result of applying the logical XOR operator to the
 331      * specified {@code boolean} operands.
 332      *
 333      * @param a the first operand
 334      * @param b the second operand
 335      * @return  the logical XOR of {@code a} and {@code b}
 336      * @see java.util.function.BinaryOperator
 337      * @since 1.8
 338      */
 339     public static boolean logicalXor(boolean a, boolean b) {
 340         return a ^ b;
 341     }
 342 }
--- EOF ---