Home | History | Annotate | Download | only in lang
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.lang;
     19 
     20 import java.io.Serializable;
     21 
     22 /**
     23  * The wrapper for the primitive type {@code boolean}.
     24  *
     25  * @since 1.0
     26  */
     27 public final class Boolean implements Serializable, Comparable<Boolean> {
     28 
     29     private static final long serialVersionUID = -3665804199014368530L;
     30 
     31     /**
     32      * The boolean value of the receiver.
     33      */
     34     private final boolean value;
     35 
     36     /**
     37      * The {@link Class} object that represents the primitive type {@code
     38      * boolean}.
     39      */
     40     @SuppressWarnings("unchecked")
     41     public static final Class<Boolean> TYPE
     42              = (Class<Boolean>) boolean[].class.getComponentType();
     43     // Note: Boolean.TYPE can't be set to "boolean.class", since *that* is
     44     // defined to be "java.lang.Boolean.TYPE";
     45 
     46     /**
     47      * The {@code Boolean} object that represents the primitive value
     48      * {@code true}.
     49      */
     50     public static final Boolean TRUE = new Boolean(true);
     51 
     52     /**
     53      * The {@code Boolean} object that represents the primitive value
     54      * {@code false}.
     55      */
     56     public static final Boolean FALSE = new Boolean(false);
     57 
     58     /**
     59      * Constructs a new {@code Boolean} with its boolean value specified by
     60      * {@code string}. If {@code string} is not {@code null} and is equal to
     61      * "true" using a non-case sensitive comparison, the result will be a
     62      * Boolean representing the primitive value {@code true}, otherwise it will
     63      * be a Boolean representing the primitive value {@code false}.
     64      *
     65      * @param string
     66      *            the string representing a boolean value.
     67      */
     68     public Boolean(String string) {
     69         this(parseBoolean(string));
     70     }
     71 
     72     /**
     73      * Constructs a new {@code Boolean} with the specified primitive boolean
     74      * value.
     75      *
     76      * @param value
     77      *            the primitive boolean value, {@code true} or {@code false}.
     78      */
     79     public Boolean(boolean value) {
     80         this.value = value;
     81     }
     82 
     83     /**
     84      * Gets the primitive value of this boolean, either {@code true} or
     85      * {@code false}.
     86      *
     87      * @return this object's primitive value, {@code true} or {@code false}.
     88      */
     89     public boolean booleanValue() {
     90         return value;
     91     }
     92 
     93     /**
     94      * Compares this instance with the specified object and indicates if they
     95      * are equal. In order to be equal, {@code o} must be an instance of
     96      * {@code Boolean} and have the same boolean value as this object.
     97      *
     98      * @param o
     99      *            the object to compare this boolean with.
    100      * @return {@code true} if the specified object is equal to this
    101      *         {@code Boolean}; {@code false} otherwise.
    102      */
    103     @Override
    104     @FindBugsSuppressWarnings("RC_REF_COMPARISON_BAD_PRACTICE_BOOLEAN")
    105     public boolean equals(Object o) {
    106         return (o == this) || ((o instanceof Boolean) && (((Boolean) o).value == value));
    107     }
    108 
    109     /**
    110      * Compares this object to the specified boolean object to determine their
    111      * relative order.
    112      *
    113      * @param that
    114      *            the boolean object to compare this object to.
    115      * @return 0 if the value of this boolean and the value of {@code that} are
    116      *         equal; a positive value if the value of this boolean is
    117      *         {@code true} and the value of {@code that} is {@code false}; a
    118      *         negative value if the value if this boolean is {@code false} and
    119      *         the value of {@code that} is {@code true}.
    120      * @see java.lang.Comparable
    121      * @since 1.5
    122      */
    123     public int compareTo(Boolean that) {
    124         return compare(value, that.value);
    125     }
    126 
    127     /**
    128      * Compares two {@code boolean} values.
    129      * @return 0 if lhs = rhs, less than 0 if lhs &lt; rhs, and greater than 0 if lhs &gt; rhs.
    130      *         (Where true &gt; false.)
    131      * @since 1.7
    132      */
    133     public static int compare(boolean lhs, boolean rhs) {
    134         return lhs == rhs ? 0 : lhs ? 1 : -1;
    135     }
    136 
    137     /**
    138      * Returns an integer hash code for this boolean.
    139      *
    140      * @return this boolean's hash code, which is {@code 1231} for {@code true}
    141      *         values and {@code 1237} for {@code false} values.
    142      */
    143     @Override
    144     public int hashCode() {
    145         return value ? 1231 : 1237;
    146     }
    147 
    148     /**
    149      * Returns a string containing a concise, human-readable description of this
    150      * boolean.
    151      *
    152      * @return "true" if the value of this boolean is {@code true}, "false"
    153      *         otherwise.
    154      */
    155     @Override
    156     public String toString() {
    157         return String.valueOf(value);
    158     }
    159 
    160     /**
    161      * Returns the {@code boolean} value of the system property identified by
    162      * {@code string}.
    163      *
    164      * @param string
    165      *            the name of the requested system property.
    166      * @return {@code true} if the system property named by {@code string}
    167      *         exists and it is equal to "true" using case insensitive
    168      *         comparison, {@code false} otherwise.
    169      * @see System#getProperty(String)
    170      */
    171     public static boolean getBoolean(String string) {
    172         if (string == null || string.length() == 0) {
    173             return false;
    174         }
    175         return (parseBoolean(System.getProperty(string)));
    176     }
    177 
    178     /**
    179      * Parses the specified string as a {@code boolean}.
    180      *
    181      * @param s
    182      *            the string representation of a boolean value.
    183      * @return {@code true} if {@code s} is not {@code null} and is equal to
    184      *         {@code "true"} using case insensitive comparison, {@code false}
    185      *         otherwise.
    186      * @since 1.5
    187      */
    188     public static boolean parseBoolean(String s) {
    189         return "true".equalsIgnoreCase(s);
    190     }
    191 
    192     /**
    193      * Converts the specified boolean to its string representation.
    194      *
    195      * @param value
    196      *            the boolean to convert.
    197      * @return "true" if {@code value} is {@code true}, "false" otherwise.
    198      */
    199     public static String toString(boolean value) {
    200         return String.valueOf(value);
    201     }
    202 
    203     /**
    204      * Parses the specified string as a boolean value.
    205      *
    206      * @param string
    207      *            the string representation of a boolean value.
    208      * @return {@code Boolean.TRUE} if {@code string} is equal to "true" using
    209      *         case insensitive comparison, {@code Boolean.FALSE} otherwise.
    210      * @see #parseBoolean(String)
    211      */
    212     public static Boolean valueOf(String string) {
    213         return parseBoolean(string) ? Boolean.TRUE : Boolean.FALSE;
    214     }
    215 
    216     /**
    217      * Returns a {@code Boolean} instance for the specified boolean value.
    218      * <p>
    219      * If it is not necessary to get a new {@code Boolean} instance, it is
    220      * recommended to use this method instead of the constructor, since it
    221      * returns its static instances, which results in better performance.
    222      *
    223      * @param b
    224      *            the boolean to convert to a {@code Boolean}.
    225      * @return {@code Boolean.TRUE} if {@code b} is equal to {@code true},
    226      *         {@code Boolean.FALSE} otherwise.
    227      */
    228     public static Boolean valueOf(boolean b) {
    229         return b ? Boolean.TRUE : Boolean.FALSE;
    230     }
    231 }
    232