jdk/src/java.base/share/classes/java/lang/Boolean.java
author rriggs
Tue, 16 Feb 2016 11:36:20 -0500
changeset 35978 752d505da547
parent 31671 362e0c0acece
child 37521 b6e0f285c998
permissions -rw-r--r--
8148775: Spec for j.l.ProcessBuilder.Redirect.DISCARD need to be improved Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 15311
diff changeset
     2
 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3943
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3943
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3943
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3943
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3943
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30678
diff changeset
    28
import jdk.internal.HotSpotIntrinsicCandidate;
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30678
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The Boolean class wraps a value of the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * {@code boolean} in an object. An object of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * {@code Boolean} contains a single field whose type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * {@code boolean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * In addition, this class provides many methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * converting a {@code boolean} to a {@code String} and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@code String} to a {@code boolean}, as well as other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * constants and methods useful when dealing with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * {@code boolean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Arthur van Hoff
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 18546
diff changeset
    43
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class Boolean implements java.io.Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                      Comparable<Boolean>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * The {@code Boolean} object corresponding to the primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final Boolean TRUE = new Boolean(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The {@code Boolean} object corresponding to the primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final Boolean FALSE = new Boolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The Class object representing the primitive type boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 18546
diff changeset
    63
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 10602
diff changeset
    65
    @SuppressWarnings("unchecked")
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 10602
diff changeset
    66
    public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The value of the Boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final boolean value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final long serialVersionUID = -3665804199014368530L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Allocates a {@code Boolean} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * {@code value} argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p><b>Note: It is rarely appropriate to use this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Unless a <i>new</i> instance is required, the static factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * {@link #valueOf(boolean)} is generally a better choice. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * likely to yield significantly better space and time performance.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param   value   the value of the {@code Boolean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public Boolean(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Allocates a {@code Boolean} object representing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * {@code true} if the string argument is not {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * and is equal, ignoring case, to the string {@code "true"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Otherwise, allocate a {@code Boolean} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * value {@code false}. Examples:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * {@code new Boolean("True")} produces a {@code Boolean} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * that represents {@code true}.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * {@code new Boolean("yes")} produces a {@code Boolean} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * that represents {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param   s   the string to be converted to a {@code Boolean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public Boolean(String s) {
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   107
        this(parseBoolean(s));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Parses the string argument as a boolean.  The {@code boolean}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * returned represents the value {@code true} if the string argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * is not {@code null} and is equal, ignoring case, to the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * {@code "true"}. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param      s   the {@code String} containing the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *                 representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return     the boolean represented by the string argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static boolean parseBoolean(String s) {
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   124
        return ((s != null) && s.equalsIgnoreCase("true"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Returns the value of this {@code Boolean} object as a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * primitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return  the primitive {@code boolean} value of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30678
diff changeset
   133
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public boolean booleanValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Returns a {@code Boolean} instance representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * {@code boolean} value.  If the specified {@code boolean} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * is {@code true}, this method returns {@code Boolean.TRUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * if it is {@code false}, this method returns {@code Boolean.FALSE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * If a new {@code Boolean} instance is not required, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * should generally be used in preference to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * {@link #Boolean(boolean)}, as this method is likely to yield
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * significantly better space and time performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param  b a boolean value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return a {@code Boolean} instance representing {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 30678
diff changeset
   152
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static Boolean valueOf(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return (b ? TRUE : FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns a {@code Boolean} with a value represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * specified string.  The {@code Boolean} returned represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * true value if the string argument is not {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * and is equal, ignoring case, to the string {@code "true"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param   s   a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return  the {@code Boolean} value represented by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public static Boolean valueOf(String s) {
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   167
        return parseBoolean(s) ? TRUE : FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns a {@code String} object representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * boolean.  If the specified boolean is {@code true}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the string {@code "true"} will be returned, otherwise the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * string {@code "false"} will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param b the boolean to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the string representation of the specified {@code boolean}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public static String toString(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return b ? "true" : "false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns a {@code String} object representing this Boolean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * value.  If this object represents the value {@code true},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * a string equal to {@code "true"} is returned. Otherwise, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * string equal to {@code "false"} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return  a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return value ? "true" : "false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns a hash code for this {@code Boolean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return  the integer {@code 1231} if this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * {@code true}; returns the integer {@code 1237} if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * object represents {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   203
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public int hashCode() {
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   205
        return Boolean.hashCode(value);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   206
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   207
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   208
    /**
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   209
     * Returns a hash code for a {@code boolean} value; compatible with
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   210
     * {@code Boolean.hashCode()}.
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   211
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 15311
diff changeset
   212
     * @param value the value to hash
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 15311
diff changeset
   213
     * @return a hash code value for a {@code boolean} value.
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   214
     * @since 1.8
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   215
     */
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   216
    public static int hashCode(boolean value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return value ? 1231 : 1237;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 11275
diff changeset
   220
   /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Returns {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * {@code null} and is a {@code Boolean} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * represents the same {@code boolean} value as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param   obj   the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return  {@code true} if the Boolean objects represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *          same value; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (obj instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return value == ((Boolean)obj).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 25859
diff changeset
   237
     * Returns {@code true} if and only if the system property named
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 25859
diff changeset
   238
     * by the argument exists and is equal to the string {@code
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 25859
diff changeset
   239
     * "true"}. (Beginning with version 1.0.2 of the Java&trade;
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 25859
diff changeset
   240
     * platform, the test of this string is case insensitive.) A
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 25859
diff changeset
   241
     * system property is accessible through {@code getProperty}, a
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 25859
diff changeset
   242
     * method defined by the {@code System} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * If there is no property with the specified name, or if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * name is empty or null, then {@code false} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param   name   the system property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return  the {@code boolean} value of the system property.
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   249
     * @throws  SecurityException for the same reasons as
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   250
     *          {@link System#getProperty(String) System.getProperty}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public static boolean getBoolean(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        try {
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   257
            result = parseBoolean(System.getProperty(name));
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 5506
diff changeset
   258
        } catch (IllegalArgumentException | NullPointerException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Compares this {@code Boolean} instance with another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param   b the {@code Boolean} instance to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return  zero if this object represents the same boolean value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          argument; a positive value if this object represents true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          and the argument represents false; and a negative value if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          this object represents false and the argument represents true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws  NullPointerException if the argument is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see     Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public int compareTo(Boolean b) {
3943
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   276
        return compare(this.value, b.value);
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   277
    }
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   278
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   279
    /**
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   280
     * Compares two {@code boolean} values.
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   281
     * The value returned is identical to what would be returned by:
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   282
     * <pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   283
     *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   284
     * </pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   285
     *
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   286
     * @param  x the first {@code boolean} to compare
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   287
     * @param  y the second {@code boolean} to compare
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   288
     * @return the value {@code 0} if {@code x == y};
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   289
     *         a value less than {@code 0} if {@code !x && y}; and
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   290
     *         a value greater than {@code 0} if {@code x && !y}
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   291
     * @since 1.7
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   292
     */
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   293
    public static int compare(boolean x, boolean y) {
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 2
diff changeset
   294
        return (x == y) ? 0 : (x ? 1 : -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
15311
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   296
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   297
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   298
     * Returns the result of applying the logical AND operator to the
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   299
     * specified {@code boolean} operands.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   300
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   301
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   302
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   303
     * @return the logical AND of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   304
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   305
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   306
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   307
    public static boolean logicalAnd(boolean a, boolean b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   308
        return a && b;
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   309
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   310
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   311
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   312
     * Returns the result of applying the logical OR operator to the
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   313
     * specified {@code boolean} operands.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   314
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   315
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   316
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   317
     * @return the logical OR of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   318
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   319
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   320
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   321
    public static boolean logicalOr(boolean a, boolean b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   322
        return a || b;
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   323
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   324
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   325
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   326
     * Returns the result of applying the logical XOR operator to the
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   327
     * specified {@code boolean} operands.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   328
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   329
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   330
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   331
     * @return  the logical XOR of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   332
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   333
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   334
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   335
    public static boolean logicalXor(boolean a, boolean b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   336
        return a ^ b;
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 14503
diff changeset
   337
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}