rt/emul/mini/src/main/java/java/lang/Boolean.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 28 Feb 2013 10:39:40 +0100
changeset 791 af4001c85438
parent 772 d382dacfd73f
child 1097 8e42a376da73
permissions -rw-r--r--
All primitive type wrappers (including Character and Boolean) now have proper valueOf implementation
     1 /*
     2  * Copyright (c) 1994, 2006, 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 org.apidesign.bck2brwsr.core.JavaScriptBody;
    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   JDK1.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   JDK1.1
    64      */
    65     public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
    66 
    67     /**
    68      * The value of the Boolean.
    69      *
    70      * @serial
    71      */
    72     private final boolean value;
    73 
    74     /** use serialVersionUID from JDK 1.0.2 for interoperability */
    75     private static final long serialVersionUID = -3665804199014368530L;
    76 
    77     /**
    78      * Allocates a {@code Boolean} object representing the
    79      * {@code value} argument.
    80      *
    81      * <p><b>Note: It is rarely appropriate to use this constructor.
    82      * Unless a <i>new</i> instance is required, the static factory
    83      * {@link #valueOf(boolean)} is generally a better choice. It is
    84      * likely to yield significantly better space and time performance.</b>
    85      *
    86      * @param   value   the value of the {@code Boolean}.
    87      */
    88     public Boolean(boolean value) {
    89         this.value = value;
    90     }
    91 
    92     /**
    93      * Allocates a {@code Boolean} object representing the value
    94      * {@code true} if the string argument is not {@code null}
    95      * and is equal, ignoring case, to the string {@code "true"}.
    96      * Otherwise, allocate a {@code Boolean} object representing the
    97      * value {@code false}. Examples:<p>
    98      * {@code new Boolean("True")} produces a {@code Boolean} object
    99      * that represents {@code true}.<br>
   100      * {@code new Boolean("yes")} produces a {@code Boolean} object
   101      * that represents {@code false}.
   102      *
   103      * @param   s   the string to be converted to a {@code Boolean}.
   104      */
   105     public Boolean(String s) {
   106         this(toBoolean(s));
   107     }
   108 
   109     /**
   110      * Parses the string argument as a boolean.  The {@code boolean}
   111      * returned represents the value {@code true} if the string argument
   112      * is not {@code null} and is equal, ignoring case, to the string
   113      * {@code "true"}. <p>
   114      * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
   115      * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
   116      *
   117      * @param      s   the {@code String} containing the boolean
   118      *                 representation to be parsed
   119      * @return     the boolean represented by the string argument
   120      * @since 1.5
   121      */
   122     public static boolean parseBoolean(String s) {
   123         return toBoolean(s);
   124     }
   125 
   126     /**
   127      * Returns the value of this {@code Boolean} object as a boolean
   128      * primitive.
   129      *
   130      * @return  the primitive {@code boolean} value of this object.
   131      */
   132     public boolean booleanValue() {
   133         return value;
   134     }
   135 
   136     /**
   137      * Returns a {@code Boolean} instance representing the specified
   138      * {@code boolean} value.  If the specified {@code boolean} value
   139      * is {@code true}, this method returns {@code Boolean.TRUE};
   140      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
   141      * If a new {@code Boolean} instance is not required, this method
   142      * should generally be used in preference to the constructor
   143      * {@link #Boolean(boolean)}, as this method is likely to yield
   144      * significantly better space and time performance.
   145      *
   146      * @param  b a boolean value.
   147      * @return a {@code Boolean} instance representing {@code b}.
   148      * @since  1.4
   149      */
   150     public static Boolean valueOf(boolean b) {
   151         return (b ? TRUE : FALSE);
   152     }
   153 
   154     /**
   155      * Returns a {@code Boolean} with a value represented by the
   156      * specified string.  The {@code Boolean} returned represents a
   157      * true value if the string argument is not {@code null}
   158      * and is equal, ignoring case, to the string {@code "true"}.
   159      *
   160      * @param   s   a string.
   161      * @return  the {@code Boolean} value represented by the string.
   162      */
   163     public static Boolean valueOf(String s) {
   164         return toBoolean(s) ? TRUE : FALSE;
   165     }
   166 
   167     /**
   168      * Returns a {@code String} object representing the specified
   169      * boolean.  If the specified boolean is {@code true}, then
   170      * the string {@code "true"} will be returned, otherwise the
   171      * string {@code "false"} will be returned.
   172      *
   173      * @param b the boolean to be converted
   174      * @return the string representation of the specified {@code boolean}
   175      * @since 1.4
   176      */
   177     public static String toString(boolean b) {
   178         return b ? "true" : "false";
   179     }
   180 
   181     /**
   182      * Returns a {@code String} object representing this Boolean's
   183      * value.  If this object represents the value {@code true},
   184      * a string equal to {@code "true"} is returned. Otherwise, a
   185      * string equal to {@code "false"} is returned.
   186      *
   187      * @return  a string representation of this object.
   188      */
   189     public String toString() {
   190         return value ? "true" : "false";
   191     }
   192 
   193     /**
   194      * Returns a hash code for this {@code Boolean} object.
   195      *
   196      * @return  the integer {@code 1231} if this object represents
   197      * {@code true}; returns the integer {@code 1237} if this
   198      * object represents {@code false}.
   199      */
   200     public int hashCode() {
   201         return value ? 1231 : 1237;
   202     }
   203 
   204     /**
   205      * Returns {@code true} if and only if the argument is not
   206      * {@code null} and is a {@code Boolean} object that
   207      * represents the same {@code boolean} value as this object.
   208      *
   209      * @param   obj   the object to compare with.
   210      * @return  {@code true} if the Boolean objects represent the
   211      *          same value; {@code false} otherwise.
   212      */
   213     public boolean equals(Object obj) {
   214         if (obj instanceof Boolean) {
   215             return value == ((Boolean)obj).booleanValue();
   216         }
   217         return false;
   218     }
   219 
   220     /**
   221      * Returns {@code true} if and only if the system property
   222      * named by the argument exists and is equal to the string
   223      * {@code "true"}. (Beginning with version 1.0.2 of the
   224      * Java<small><sup>TM</sup></small> platform, the test of
   225      * this string is case insensitive.) A system property is accessible
   226      * through {@code getProperty}, a method defined by the
   227      * {@code System} class.
   228      * <p>
   229      * If there is no property with the specified name, or if the specified
   230      * name is empty or null, then {@code false} is returned.
   231      *
   232      * @param   name   the system property name.
   233      * @return  the {@code boolean} value of the system property.
   234      * @see     java.lang.System#getProperty(java.lang.String)
   235      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
   236      */
   237     public static boolean getBoolean(String name) {
   238         boolean result = false;
   239         try {
   240             result = toBoolean(AbstractStringBuilder.getProperty(name));
   241         } catch (IllegalArgumentException e) {
   242         } catch (NullPointerException e) {
   243         }
   244         return result;
   245     }
   246 
   247     /**
   248      * Compares this {@code Boolean} instance with another.
   249      *
   250      * @param   b the {@code Boolean} instance to be compared
   251      * @return  zero if this object represents the same boolean value as the
   252      *          argument; a positive value if this object represents true
   253      *          and the argument represents false; and a negative value if
   254      *          this object represents false and the argument represents true
   255      * @throws  NullPointerException if the argument is {@code null}
   256      * @see     Comparable
   257      * @since  1.5
   258      */
   259     public int compareTo(Boolean b) {
   260         return compare(this.value, b.value);
   261     }
   262 
   263     /**
   264      * Compares two {@code boolean} values.
   265      * The value returned is identical to what would be returned by:
   266      * <pre>
   267      *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
   268      * </pre>
   269      *
   270      * @param  x the first {@code boolean} to compare
   271      * @param  y the second {@code boolean} to compare
   272      * @return the value {@code 0} if {@code x == y};
   273      *         a value less than {@code 0} if {@code !x && y}; and
   274      *         a value greater than {@code 0} if {@code x && !y}
   275      * @since 1.7
   276      */
   277     public static int compare(boolean x, boolean y) {
   278         return (x == y) ? 0 : (x ? 1 : -1);
   279     }
   280 
   281     private static boolean toBoolean(String name) {
   282         return ((name != null) && name.equalsIgnoreCase("true"));
   283     }
   284     static {
   285         // as last step of initialization, initialize valueOf method
   286         initValueOf();
   287     }
   288     @JavaScriptBody(args = {  }, body = 
   289         "vm.java_lang_Boolean(false)" +
   290         ".valueOf = function() { return this._value(); };"
   291     )
   292     private native static void initValueOf();
   293 }