emul/mini/src/main/java/java/lang/Boolean.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Jan 2013 08:47:05 +0100
changeset 592 5e13b1ac2886
parent 104 1376481f15e7
permissions -rw-r--r--
In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
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@86
    28
/**
jaroslav@86
    29
 * The Boolean class wraps a value of the primitive type
jaroslav@86
    30
 * {@code boolean} in an object. An object of type
jaroslav@86
    31
 * {@code Boolean} contains a single field whose type is
jaroslav@86
    32
 * {@code boolean}.
jaroslav@86
    33
 * <p>
jaroslav@86
    34
 * In addition, this class provides many methods for
jaroslav@86
    35
 * converting a {@code boolean} to a {@code String} and a
jaroslav@86
    36
 * {@code String} to a {@code boolean}, as well as other
jaroslav@86
    37
 * constants and methods useful when dealing with a
jaroslav@86
    38
 * {@code boolean}.
jaroslav@86
    39
 *
jaroslav@86
    40
 * @author  Arthur van Hoff
jaroslav@86
    41
 * @since   JDK1.0
jaroslav@86
    42
 */
jaroslav@86
    43
public final class Boolean implements java.io.Serializable,
jaroslav@86
    44
                                      Comparable<Boolean>
jaroslav@86
    45
{
jaroslav@86
    46
    /**
jaroslav@86
    47
     * The {@code Boolean} object corresponding to the primitive
jaroslav@86
    48
     * value {@code true}.
jaroslav@86
    49
     */
jaroslav@86
    50
    public static final Boolean TRUE = new Boolean(true);
jaroslav@86
    51
jaroslav@86
    52
    /**
jaroslav@86
    53
     * The {@code Boolean} object corresponding to the primitive
jaroslav@86
    54
     * value {@code false}.
jaroslav@86
    55
     */
jaroslav@86
    56
    public static final Boolean FALSE = new Boolean(false);
jaroslav@86
    57
jaroslav@86
    58
    /**
jaroslav@86
    59
     * The Class object representing the primitive type boolean.
jaroslav@86
    60
     *
jaroslav@86
    61
     * @since   JDK1.1
jaroslav@86
    62
     */
jaroslav@86
    63
    public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
jaroslav@86
    64
jaroslav@86
    65
    /**
jaroslav@86
    66
     * The value of the Boolean.
jaroslav@86
    67
     *
jaroslav@86
    68
     * @serial
jaroslav@86
    69
     */
jaroslav@86
    70
    private final boolean value;
jaroslav@86
    71
jaroslav@86
    72
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@86
    73
    private static final long serialVersionUID = -3665804199014368530L;
jaroslav@86
    74
jaroslav@86
    75
    /**
jaroslav@86
    76
     * Allocates a {@code Boolean} object representing the
jaroslav@86
    77
     * {@code value} argument.
jaroslav@86
    78
     *
jaroslav@86
    79
     * <p><b>Note: It is rarely appropriate to use this constructor.
jaroslav@86
    80
     * Unless a <i>new</i> instance is required, the static factory
jaroslav@86
    81
     * {@link #valueOf(boolean)} is generally a better choice. It is
jaroslav@86
    82
     * likely to yield significantly better space and time performance.</b>
jaroslav@86
    83
     *
jaroslav@86
    84
     * @param   value   the value of the {@code Boolean}.
jaroslav@86
    85
     */
jaroslav@86
    86
    public Boolean(boolean value) {
jaroslav@86
    87
        this.value = value;
jaroslav@86
    88
    }
jaroslav@86
    89
jaroslav@86
    90
    /**
jaroslav@86
    91
     * Allocates a {@code Boolean} object representing the value
jaroslav@86
    92
     * {@code true} if the string argument is not {@code null}
jaroslav@86
    93
     * and is equal, ignoring case, to the string {@code "true"}.
jaroslav@86
    94
     * Otherwise, allocate a {@code Boolean} object representing the
jaroslav@86
    95
     * value {@code false}. Examples:<p>
jaroslav@86
    96
     * {@code new Boolean("True")} produces a {@code Boolean} object
jaroslav@86
    97
     * that represents {@code true}.<br>
jaroslav@86
    98
     * {@code new Boolean("yes")} produces a {@code Boolean} object
jaroslav@86
    99
     * that represents {@code false}.
jaroslav@86
   100
     *
jaroslav@86
   101
     * @param   s   the string to be converted to a {@code Boolean}.
jaroslav@86
   102
     */
jaroslav@86
   103
    public Boolean(String s) {
jaroslav@86
   104
        this(toBoolean(s));
jaroslav@86
   105
    }
jaroslav@86
   106
jaroslav@86
   107
    /**
jaroslav@86
   108
     * Parses the string argument as a boolean.  The {@code boolean}
jaroslav@86
   109
     * returned represents the value {@code true} if the string argument
jaroslav@86
   110
     * is not {@code null} and is equal, ignoring case, to the string
jaroslav@86
   111
     * {@code "true"}. <p>
jaroslav@86
   112
     * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
jaroslav@86
   113
     * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
jaroslav@86
   114
     *
jaroslav@86
   115
     * @param      s   the {@code String} containing the boolean
jaroslav@86
   116
     *                 representation to be parsed
jaroslav@86
   117
     * @return     the boolean represented by the string argument
jaroslav@86
   118
     * @since 1.5
jaroslav@86
   119
     */
jaroslav@86
   120
    public static boolean parseBoolean(String s) {
jaroslav@86
   121
        return toBoolean(s);
jaroslav@86
   122
    }
jaroslav@86
   123
jaroslav@86
   124
    /**
jaroslav@86
   125
     * Returns the value of this {@code Boolean} object as a boolean
jaroslav@86
   126
     * primitive.
jaroslav@86
   127
     *
jaroslav@86
   128
     * @return  the primitive {@code boolean} value of this object.
jaroslav@86
   129
     */
jaroslav@86
   130
    public boolean booleanValue() {
jaroslav@86
   131
        return value;
jaroslav@86
   132
    }
jaroslav@86
   133
jaroslav@86
   134
    /**
jaroslav@86
   135
     * Returns a {@code Boolean} instance representing the specified
jaroslav@86
   136
     * {@code boolean} value.  If the specified {@code boolean} value
jaroslav@86
   137
     * is {@code true}, this method returns {@code Boolean.TRUE};
jaroslav@86
   138
     * if it is {@code false}, this method returns {@code Boolean.FALSE}.
jaroslav@86
   139
     * If a new {@code Boolean} instance is not required, this method
jaroslav@86
   140
     * should generally be used in preference to the constructor
jaroslav@86
   141
     * {@link #Boolean(boolean)}, as this method is likely to yield
jaroslav@86
   142
     * significantly better space and time performance.
jaroslav@86
   143
     *
jaroslav@86
   144
     * @param  b a boolean value.
jaroslav@86
   145
     * @return a {@code Boolean} instance representing {@code b}.
jaroslav@86
   146
     * @since  1.4
jaroslav@86
   147
     */
jaroslav@86
   148
    public static Boolean valueOf(boolean b) {
jaroslav@86
   149
        return (b ? TRUE : FALSE);
jaroslav@86
   150
    }
jaroslav@86
   151
jaroslav@86
   152
    /**
jaroslav@86
   153
     * Returns a {@code Boolean} with a value represented by the
jaroslav@86
   154
     * specified string.  The {@code Boolean} returned represents a
jaroslav@86
   155
     * true value if the string argument is not {@code null}
jaroslav@86
   156
     * and is equal, ignoring case, to the string {@code "true"}.
jaroslav@86
   157
     *
jaroslav@86
   158
     * @param   s   a string.
jaroslav@86
   159
     * @return  the {@code Boolean} value represented by the string.
jaroslav@86
   160
     */
jaroslav@86
   161
    public static Boolean valueOf(String s) {
jaroslav@86
   162
        return toBoolean(s) ? TRUE : FALSE;
jaroslav@86
   163
    }
jaroslav@86
   164
jaroslav@86
   165
    /**
jaroslav@86
   166
     * Returns a {@code String} object representing the specified
jaroslav@86
   167
     * boolean.  If the specified boolean is {@code true}, then
jaroslav@86
   168
     * the string {@code "true"} will be returned, otherwise the
jaroslav@86
   169
     * string {@code "false"} will be returned.
jaroslav@86
   170
     *
jaroslav@86
   171
     * @param b the boolean to be converted
jaroslav@86
   172
     * @return the string representation of the specified {@code boolean}
jaroslav@86
   173
     * @since 1.4
jaroslav@86
   174
     */
jaroslav@86
   175
    public static String toString(boolean b) {
jaroslav@86
   176
        return b ? "true" : "false";
jaroslav@86
   177
    }
jaroslav@86
   178
jaroslav@86
   179
    /**
jaroslav@86
   180
     * Returns a {@code String} object representing this Boolean's
jaroslav@86
   181
     * value.  If this object represents the value {@code true},
jaroslav@86
   182
     * a string equal to {@code "true"} is returned. Otherwise, a
jaroslav@86
   183
     * string equal to {@code "false"} is returned.
jaroslav@86
   184
     *
jaroslav@86
   185
     * @return  a string representation of this object.
jaroslav@86
   186
     */
jaroslav@86
   187
    public String toString() {
jaroslav@86
   188
        return value ? "true" : "false";
jaroslav@86
   189
    }
jaroslav@86
   190
jaroslav@86
   191
    /**
jaroslav@86
   192
     * Returns a hash code for this {@code Boolean} object.
jaroslav@86
   193
     *
jaroslav@86
   194
     * @return  the integer {@code 1231} if this object represents
jaroslav@86
   195
     * {@code true}; returns the integer {@code 1237} if this
jaroslav@86
   196
     * object represents {@code false}.
jaroslav@86
   197
     */
jaroslav@86
   198
    public int hashCode() {
jaroslav@86
   199
        return value ? 1231 : 1237;
jaroslav@86
   200
    }
jaroslav@86
   201
jaroslav@86
   202
    /**
jaroslav@86
   203
     * Returns {@code true} if and only if the argument is not
jaroslav@86
   204
     * {@code null} and is a {@code Boolean} object that
jaroslav@86
   205
     * represents the same {@code boolean} value as this object.
jaroslav@86
   206
     *
jaroslav@86
   207
     * @param   obj   the object to compare with.
jaroslav@86
   208
     * @return  {@code true} if the Boolean objects represent the
jaroslav@86
   209
     *          same value; {@code false} otherwise.
jaroslav@86
   210
     */
jaroslav@86
   211
    public boolean equals(Object obj) {
jaroslav@86
   212
        if (obj instanceof Boolean) {
jaroslav@86
   213
            return value == ((Boolean)obj).booleanValue();
jaroslav@86
   214
        }
jaroslav@86
   215
        return false;
jaroslav@86
   216
    }
jaroslav@86
   217
jaroslav@86
   218
    /**
jaroslav@86
   219
     * Returns {@code true} if and only if the system property
jaroslav@86
   220
     * named by the argument exists and is equal to the string
jaroslav@86
   221
     * {@code "true"}. (Beginning with version 1.0.2 of the
jaroslav@86
   222
     * Java<small><sup>TM</sup></small> platform, the test of
jaroslav@86
   223
     * this string is case insensitive.) A system property is accessible
jaroslav@86
   224
     * through {@code getProperty}, a method defined by the
jaroslav@86
   225
     * {@code System} class.
jaroslav@86
   226
     * <p>
jaroslav@86
   227
     * If there is no property with the specified name, or if the specified
jaroslav@86
   228
     * name is empty or null, then {@code false} is returned.
jaroslav@86
   229
     *
jaroslav@86
   230
     * @param   name   the system property name.
jaroslav@86
   231
     * @return  the {@code boolean} value of the system property.
jaroslav@86
   232
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@86
   233
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@86
   234
     */
jaroslav@86
   235
    public static boolean getBoolean(String name) {
jaroslav@86
   236
        boolean result = false;
jaroslav@86
   237
        try {
jaroslav@104
   238
            result = toBoolean(AbstractStringBuilder.getProperty(name));
jaroslav@86
   239
        } catch (IllegalArgumentException e) {
jaroslav@86
   240
        } catch (NullPointerException e) {
jaroslav@86
   241
        }
jaroslav@86
   242
        return result;
jaroslav@86
   243
    }
jaroslav@86
   244
jaroslav@86
   245
    /**
jaroslav@86
   246
     * Compares this {@code Boolean} instance with another.
jaroslav@86
   247
     *
jaroslav@86
   248
     * @param   b the {@code Boolean} instance to be compared
jaroslav@86
   249
     * @return  zero if this object represents the same boolean value as the
jaroslav@86
   250
     *          argument; a positive value if this object represents true
jaroslav@86
   251
     *          and the argument represents false; and a negative value if
jaroslav@86
   252
     *          this object represents false and the argument represents true
jaroslav@86
   253
     * @throws  NullPointerException if the argument is {@code null}
jaroslav@86
   254
     * @see     Comparable
jaroslav@86
   255
     * @since  1.5
jaroslav@86
   256
     */
jaroslav@86
   257
    public int compareTo(Boolean b) {
jaroslav@86
   258
        return compare(this.value, b.value);
jaroslav@86
   259
    }
jaroslav@86
   260
jaroslav@86
   261
    /**
jaroslav@86
   262
     * Compares two {@code boolean} values.
jaroslav@86
   263
     * The value returned is identical to what would be returned by:
jaroslav@86
   264
     * <pre>
jaroslav@86
   265
     *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
jaroslav@86
   266
     * </pre>
jaroslav@86
   267
     *
jaroslav@86
   268
     * @param  x the first {@code boolean} to compare
jaroslav@86
   269
     * @param  y the second {@code boolean} to compare
jaroslav@86
   270
     * @return the value {@code 0} if {@code x == y};
jaroslav@86
   271
     *         a value less than {@code 0} if {@code !x && y}; and
jaroslav@86
   272
     *         a value greater than {@code 0} if {@code x && !y}
jaroslav@86
   273
     * @since 1.7
jaroslav@86
   274
     */
jaroslav@86
   275
    public static int compare(boolean x, boolean y) {
jaroslav@86
   276
        return (x == y) ? 0 : (x ? 1 : -1);
jaroslav@86
   277
    }
jaroslav@86
   278
jaroslav@86
   279
    private static boolean toBoolean(String name) {
jaroslav@86
   280
        return ((name != null) && name.equalsIgnoreCase("true"));
jaroslav@86
   281
    }
jaroslav@86
   282
}