rt/emul/mini/src/main/java/java/lang/Boolean.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 14:33:42 +0200
branchclosure
changeset 1596 c5720b98ab4e
parent 1097 8e42a376da73
child 1922 2d4597793958
permissions -rw-r--r--
Special toString() method for Boolean
jaroslav@86
     1
/*
jaroslav@86
     2
 * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@86
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@86
     4
 *
jaroslav@86
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@86
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@86
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@86
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@86
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@86
    10
 *
jaroslav@86
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@86
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@86
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@86
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@86
    15
 * accompanied this code).
jaroslav@86
    16
 *
jaroslav@86
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@86
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@86
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@86
    20
 *
jaroslav@86
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@86
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@86
    23
 * questions.
jaroslav@86
    24
 */
jaroslav@86
    25
jaroslav@86
    26
package java.lang;
jaroslav@86
    27
jaroslav@791
    28
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@1097
    29
import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
jaroslav@791
    30
jaroslav@86
    31
/**
jaroslav@86
    32
 * The Boolean class wraps a value of the primitive type
jaroslav@86
    33
 * {@code boolean} in an object. An object of type
jaroslav@86
    34
 * {@code Boolean} contains a single field whose type is
jaroslav@86
    35
 * {@code boolean}.
jaroslav@86
    36
 * <p>
jaroslav@86
    37
 * In addition, this class provides many methods for
jaroslav@86
    38
 * converting a {@code boolean} to a {@code String} and a
jaroslav@86
    39
 * {@code String} to a {@code boolean}, as well as other
jaroslav@86
    40
 * constants and methods useful when dealing with a
jaroslav@86
    41
 * {@code boolean}.
jaroslav@86
    42
 *
jaroslav@86
    43
 * @author  Arthur van Hoff
jaroslav@86
    44
 * @since   JDK1.0
jaroslav@86
    45
 */
jaroslav@1097
    46
@JavaScriptPrototype(container = "Boolean.prototype", prototype = "new Boolean")
jaroslav@86
    47
public final class Boolean implements java.io.Serializable,
jaroslav@86
    48
                                      Comparable<Boolean>
jaroslav@86
    49
{
jaroslav@86
    50
    /**
jaroslav@86
    51
     * The {@code Boolean} object corresponding to the primitive
jaroslav@86
    52
     * value {@code true}.
jaroslav@86
    53
     */
jaroslav@86
    54
    public static final Boolean TRUE = new Boolean(true);
jaroslav@86
    55
jaroslav@86
    56
    /**
jaroslav@86
    57
     * The {@code Boolean} object corresponding to the primitive
jaroslav@86
    58
     * value {@code false}.
jaroslav@86
    59
     */
jaroslav@86
    60
    public static final Boolean FALSE = new Boolean(false);
jaroslav@86
    61
jaroslav@86
    62
    /**
jaroslav@86
    63
     * The Class object representing the primitive type boolean.
jaroslav@86
    64
     *
jaroslav@86
    65
     * @since   JDK1.1
jaroslav@86
    66
     */
jaroslav@86
    67
    public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
jaroslav@86
    68
jaroslav@86
    69
    /**
jaroslav@86
    70
     * The value of the Boolean.
jaroslav@86
    71
     *
jaroslav@86
    72
     * @serial
jaroslav@86
    73
     */
jaroslav@86
    74
    private final boolean value;
jaroslav@86
    75
jaroslav@86
    76
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@86
    77
    private static final long serialVersionUID = -3665804199014368530L;
jaroslav@86
    78
jaroslav@86
    79
    /**
jaroslav@86
    80
     * Allocates a {@code Boolean} object representing the
jaroslav@86
    81
     * {@code value} argument.
jaroslav@86
    82
     *
jaroslav@86
    83
     * <p><b>Note: It is rarely appropriate to use this constructor.
jaroslav@86
    84
     * Unless a <i>new</i> instance is required, the static factory
jaroslav@86
    85
     * {@link #valueOf(boolean)} is generally a better choice. It is
jaroslav@86
    86
     * likely to yield significantly better space and time performance.</b>
jaroslav@86
    87
     *
jaroslav@86
    88
     * @param   value   the value of the {@code Boolean}.
jaroslav@86
    89
     */
jaroslav@86
    90
    public Boolean(boolean value) {
jaroslav@86
    91
        this.value = value;
jaroslav@86
    92
    }
jaroslav@86
    93
jaroslav@86
    94
    /**
jaroslav@86
    95
     * Allocates a {@code Boolean} object representing the value
jaroslav@86
    96
     * {@code true} if the string argument is not {@code null}
jaroslav@86
    97
     * and is equal, ignoring case, to the string {@code "true"}.
jaroslav@86
    98
     * Otherwise, allocate a {@code Boolean} object representing the
jaroslav@86
    99
     * value {@code false}. Examples:<p>
jaroslav@86
   100
     * {@code new Boolean("True")} produces a {@code Boolean} object
jaroslav@86
   101
     * that represents {@code true}.<br>
jaroslav@86
   102
     * {@code new Boolean("yes")} produces a {@code Boolean} object
jaroslav@86
   103
     * that represents {@code false}.
jaroslav@86
   104
     *
jaroslav@86
   105
     * @param   s   the string to be converted to a {@code Boolean}.
jaroslav@86
   106
     */
jaroslav@86
   107
    public Boolean(String s) {
jaroslav@86
   108
        this(toBoolean(s));
jaroslav@86
   109
    }
jaroslav@86
   110
jaroslav@86
   111
    /**
jaroslav@86
   112
     * Parses the string argument as a boolean.  The {@code boolean}
jaroslav@86
   113
     * returned represents the value {@code true} if the string argument
jaroslav@86
   114
     * is not {@code null} and is equal, ignoring case, to the string
jaroslav@86
   115
     * {@code "true"}. <p>
jaroslav@86
   116
     * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
jaroslav@86
   117
     * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
jaroslav@86
   118
     *
jaroslav@86
   119
     * @param      s   the {@code String} containing the boolean
jaroslav@86
   120
     *                 representation to be parsed
jaroslav@86
   121
     * @return     the boolean represented by the string argument
jaroslav@86
   122
     * @since 1.5
jaroslav@86
   123
     */
jaroslav@86
   124
    public static boolean parseBoolean(String s) {
jaroslav@86
   125
        return toBoolean(s);
jaroslav@86
   126
    }
jaroslav@86
   127
jaroslav@86
   128
    /**
jaroslav@86
   129
     * Returns the value of this {@code Boolean} object as a boolean
jaroslav@86
   130
     * primitive.
jaroslav@86
   131
     *
jaroslav@86
   132
     * @return  the primitive {@code boolean} value of this object.
jaroslav@86
   133
     */
jaroslav@1097
   134
    @JavaScriptBody(args = {}, body = "return this.valueOf();")
jaroslav@86
   135
    public boolean booleanValue() {
jaroslav@86
   136
        return value;
jaroslav@86
   137
    }
jaroslav@86
   138
jaroslav@86
   139
    /**
jaroslav@86
   140
     * Returns a {@code Boolean} instance representing the specified
jaroslav@86
   141
     * {@code boolean} value.  If the specified {@code boolean} value
jaroslav@86
   142
     * is {@code true}, this method returns {@code Boolean.TRUE};
jaroslav@86
   143
     * if it is {@code false}, this method returns {@code Boolean.FALSE}.
jaroslav@86
   144
     * If a new {@code Boolean} instance is not required, this method
jaroslav@86
   145
     * should generally be used in preference to the constructor
jaroslav@86
   146
     * {@link #Boolean(boolean)}, as this method is likely to yield
jaroslav@86
   147
     * significantly better space and time performance.
jaroslav@86
   148
     *
jaroslav@86
   149
     * @param  b a boolean value.
jaroslav@86
   150
     * @return a {@code Boolean} instance representing {@code b}.
jaroslav@86
   151
     * @since  1.4
jaroslav@86
   152
     */
jaroslav@86
   153
    public static Boolean valueOf(boolean b) {
jaroslav@86
   154
        return (b ? TRUE : FALSE);
jaroslav@86
   155
    }
jaroslav@86
   156
jaroslav@86
   157
    /**
jaroslav@86
   158
     * Returns a {@code Boolean} with a value represented by the
jaroslav@86
   159
     * specified string.  The {@code Boolean} returned represents a
jaroslav@86
   160
     * true value if the string argument is not {@code null}
jaroslav@86
   161
     * and is equal, ignoring case, to the string {@code "true"}.
jaroslav@86
   162
     *
jaroslav@86
   163
     * @param   s   a string.
jaroslav@86
   164
     * @return  the {@code Boolean} value represented by the string.
jaroslav@86
   165
     */
jaroslav@86
   166
    public static Boolean valueOf(String s) {
jaroslav@86
   167
        return toBoolean(s) ? TRUE : FALSE;
jaroslav@86
   168
    }
jaroslav@86
   169
jaroslav@86
   170
    /**
jaroslav@86
   171
     * Returns a {@code String} object representing the specified
jaroslav@86
   172
     * boolean.  If the specified boolean is {@code true}, then
jaroslav@86
   173
     * the string {@code "true"} will be returned, otherwise the
jaroslav@86
   174
     * string {@code "false"} will be returned.
jaroslav@86
   175
     *
jaroslav@86
   176
     * @param b the boolean to be converted
jaroslav@86
   177
     * @return the string representation of the specified {@code boolean}
jaroslav@86
   178
     * @since 1.4
jaroslav@86
   179
     */
jaroslav@86
   180
    public static String toString(boolean b) {
jaroslav@86
   181
        return b ? "true" : "false";
jaroslav@86
   182
    }
jaroslav@86
   183
jaroslav@86
   184
    /**
jaroslav@86
   185
     * Returns a {@code String} object representing this Boolean's
jaroslav@86
   186
     * value.  If this object represents the value {@code true},
jaroslav@86
   187
     * a string equal to {@code "true"} is returned. Otherwise, a
jaroslav@86
   188
     * string equal to {@code "false"} is returned.
jaroslav@86
   189
     *
jaroslav@86
   190
     * @return  a string representation of this object.
jaroslav@86
   191
     */
jaroslav@86
   192
    public String toString() {
jaroslav@86
   193
        return value ? "true" : "false";
jaroslav@86
   194
    }
jaroslav@86
   195
jaroslav@86
   196
    /**
jaroslav@86
   197
     * Returns a hash code for this {@code Boolean} object.
jaroslav@86
   198
     *
jaroslav@86
   199
     * @return  the integer {@code 1231} if this object represents
jaroslav@86
   200
     * {@code true}; returns the integer {@code 1237} if this
jaroslav@86
   201
     * object represents {@code false}.
jaroslav@86
   202
     */
jaroslav@86
   203
    public int hashCode() {
jaroslav@86
   204
        return value ? 1231 : 1237;
jaroslav@86
   205
    }
jaroslav@86
   206
jaroslav@86
   207
    /**
jaroslav@86
   208
     * Returns {@code true} if and only if the argument is not
jaroslav@86
   209
     * {@code null} and is a {@code Boolean} object that
jaroslav@86
   210
     * represents the same {@code boolean} value as this object.
jaroslav@86
   211
     *
jaroslav@86
   212
     * @param   obj   the object to compare with.
jaroslav@86
   213
     * @return  {@code true} if the Boolean objects represent the
jaroslav@86
   214
     *          same value; {@code false} otherwise.
jaroslav@86
   215
     */
jaroslav@86
   216
    public boolean equals(Object obj) {
jaroslav@86
   217
        if (obj instanceof Boolean) {
jaroslav@86
   218
            return value == ((Boolean)obj).booleanValue();
jaroslav@86
   219
        }
jaroslav@86
   220
        return false;
jaroslav@86
   221
    }
jaroslav@86
   222
jaroslav@86
   223
    /**
jaroslav@86
   224
     * Returns {@code true} if and only if the system property
jaroslav@86
   225
     * named by the argument exists and is equal to the string
jaroslav@86
   226
     * {@code "true"}. (Beginning with version 1.0.2 of the
jaroslav@86
   227
     * Java<small><sup>TM</sup></small> platform, the test of
jaroslav@86
   228
     * this string is case insensitive.) A system property is accessible
jaroslav@86
   229
     * through {@code getProperty}, a method defined by the
jaroslav@86
   230
     * {@code System} class.
jaroslav@86
   231
     * <p>
jaroslav@86
   232
     * If there is no property with the specified name, or if the specified
jaroslav@86
   233
     * name is empty or null, then {@code false} is returned.
jaroslav@86
   234
     *
jaroslav@86
   235
     * @param   name   the system property name.
jaroslav@86
   236
     * @return  the {@code boolean} value of the system property.
jaroslav@86
   237
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@86
   238
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@86
   239
     */
jaroslav@86
   240
    public static boolean getBoolean(String name) {
jaroslav@86
   241
        boolean result = false;
jaroslav@86
   242
        try {
jaroslav@104
   243
            result = toBoolean(AbstractStringBuilder.getProperty(name));
jaroslav@86
   244
        } catch (IllegalArgumentException e) {
jaroslav@86
   245
        } catch (NullPointerException e) {
jaroslav@86
   246
        }
jaroslav@86
   247
        return result;
jaroslav@86
   248
    }
jaroslav@86
   249
jaroslav@86
   250
    /**
jaroslav@86
   251
     * Compares this {@code Boolean} instance with another.
jaroslav@86
   252
     *
jaroslav@86
   253
     * @param   b the {@code Boolean} instance to be compared
jaroslav@86
   254
     * @return  zero if this object represents the same boolean value as the
jaroslav@86
   255
     *          argument; a positive value if this object represents true
jaroslav@86
   256
     *          and the argument represents false; and a negative value if
jaroslav@86
   257
     *          this object represents false and the argument represents true
jaroslav@86
   258
     * @throws  NullPointerException if the argument is {@code null}
jaroslav@86
   259
     * @see     Comparable
jaroslav@86
   260
     * @since  1.5
jaroslav@86
   261
     */
jaroslav@86
   262
    public int compareTo(Boolean b) {
jaroslav@86
   263
        return compare(this.value, b.value);
jaroslav@86
   264
    }
jaroslav@86
   265
jaroslav@86
   266
    /**
jaroslav@86
   267
     * Compares two {@code boolean} values.
jaroslav@86
   268
     * The value returned is identical to what would be returned by:
jaroslav@86
   269
     * <pre>
jaroslav@86
   270
     *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
jaroslav@86
   271
     * </pre>
jaroslav@86
   272
     *
jaroslav@86
   273
     * @param  x the first {@code boolean} to compare
jaroslav@86
   274
     * @param  y the second {@code boolean} to compare
jaroslav@86
   275
     * @return the value {@code 0} if {@code x == y};
jaroslav@86
   276
     *         a value less than {@code 0} if {@code !x && y}; and
jaroslav@86
   277
     *         a value greater than {@code 0} if {@code x && !y}
jaroslav@86
   278
     * @since 1.7
jaroslav@86
   279
     */
jaroslav@86
   280
    public static int compare(boolean x, boolean y) {
jaroslav@86
   281
        return (x == y) ? 0 : (x ? 1 : -1);
jaroslav@86
   282
    }
jaroslav@86
   283
jaroslav@86
   284
    private static boolean toBoolean(String name) {
jaroslav@86
   285
        return ((name != null) && name.equalsIgnoreCase("true"));
jaroslav@86
   286
    }
jaroslav@791
   287
    static {
jaroslav@791
   288
        // as last step of initialization, initialize valueOf method
jaroslav@791
   289
        initValueOf();
jaroslav@791
   290
    }
jaroslav@791
   291
    @JavaScriptBody(args = {  }, body = 
jaroslav@1596
   292
        "var bc = vm.java_lang_Boolean(false);\n" +
jaroslav@1596
   293
        "bc.valueOf = function() { return this._value() ? true : false; };\n" +
jaroslav@1596
   294
        "bc.toString = function() { return this.valueOf().toString(); };\n"
jaroslav@791
   295
    )
jaroslav@791
   296
    private native static void initValueOf();
jaroslav@86
   297
}