1 /*
  2  * Copyright (c) 1994, 2025, 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 jdk.internal.vm.annotation.IntrinsicCandidate;
 29 
 30 import java.lang.constant.Constable;
 31 import java.lang.constant.ConstantDescs;
 32 import java.lang.constant.DynamicConstantDesc;
 33 import java.util.Optional;
 34 
 35 /**
 36  * The {@code Boolean} class is the {@linkplain
 37  * java.lang##wrapperClass wrapper class} for values of the primitive
 38  * type {@code boolean}. An object of type {@code Boolean} contains a
 39  * single field whose type is {@code boolean}.
 40  *
 41  * <p>In addition, this class provides many methods for
 42  * converting a {@code boolean} to a {@code String} and a
 43  * {@code String} to a {@code boolean}, as well as other
 44  * constants and methods useful when dealing with a
 45  * {@code boolean}.
 46  *
 47  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 48  * class; programmers should treat instances that are
 49  * {@linkplain #equals(Object) equal} as interchangeable and should not
 50  * use instances for synchronization, or unpredictable behavior may
 51  * occur. For example, in a future release, synchronization may fail.
 52  *
 53  * @author  Arthur van Hoff
 54  * @since   1.0
 55  */
 56 @jdk.internal.ValueBased
 57 public final class Boolean implements java.io.Serializable,
 58                                       Comparable<Boolean>, Constable
 59 {
 60     /**
 61      * The {@code Boolean} object corresponding to the primitive
 62      * value {@code true}.
 63      */
 64     public static final Boolean TRUE = new Boolean(true);
 65 
 66     /**
 67      * The {@code Boolean} object corresponding to the primitive
 68      * value {@code false}.
 69      */
 70     public static final Boolean FALSE = new Boolean(false);
 71 
 72     /**
 73      * The Class object representing the primitive type boolean.
 74      *
 75      * @since   1.1
 76      */
 77     public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
 78 
 79     /**
 80      * The value of the Boolean.
 81      *
 82      * @serial
 83      */
 84     private final boolean value;
 85 
 86     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 87     @java.io.Serial
 88     private static final long serialVersionUID = -3665804199014368530L;
 89 
 90     /**
 91      * Allocates a {@code Boolean} object representing the
 92      * {@code value} argument.
 93      *
 94      * @param   value   the value of the {@code Boolean}.
 95      *
 96      * @deprecated
 97      * It is rarely appropriate to use this constructor. The static factory
 98      * {@link #valueOf(boolean)} is generally a better choice, as it is
 99      * likely to yield significantly better space and time performance.
100      * Also consider using the final fields {@link #TRUE} and {@link #FALSE}
101      * if possible.
102      */
103     @Deprecated(since="9")
104     public Boolean(boolean value) {
105         this.value = value;
106     }
107 
108     /**
109      * Allocates a {@code Boolean} object representing the value
110      * {@code true} if the string argument is not {@code null}
111      * and is equal, ignoring case, to the string {@code "true"}.
112      * Otherwise, allocates a {@code Boolean} object representing the
113      * value {@code false}.
114      *
115      * @param   s   the string to be converted to a {@code Boolean}.
116      *
117      * @deprecated
118      * It is rarely appropriate to use this constructor.
119      * Use {@link #parseBoolean(String)} to convert a string to a
120      * {@code boolean} primitive, or use {@link #valueOf(String)}
121      * to convert a string to a {@code Boolean} object.
122      */
123     @Deprecated(since="9")
124     public Boolean(String s) {
125         this(parseBoolean(s));
126     }
127 
128     /**
129      * Parses the string argument as a boolean.  The {@code boolean}
130      * returned represents the value {@code true} if the string argument
131      * is not {@code null} and is equal, ignoring case, to the string
132      * {@code "true"}.
133      * Otherwise, a false value is returned, including for a null
134      * argument.<p>
135      * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
136      * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
137      *
138      * @param      s   the {@code String} containing the boolean
139      *                 representation to be parsed
140      * @return     the boolean represented by the string argument
141      * @since 1.5
142      */
143     public static boolean parseBoolean(String s) {
144         return "true".equalsIgnoreCase(s);
145     }
146 
147     /**
148      * Returns the value of this {@code Boolean} object as a boolean
149      * primitive.
150      *
151      * @return  the primitive {@code boolean} value of this object.
152      */
153     @IntrinsicCandidate
154     public boolean booleanValue() {
155         return value;
156     }
157 
158     /**
159      * Returns a {@code Boolean} instance representing the specified
160      * {@code boolean} value.  If the specified {@code boolean} value
161      * is {@code true}, this method returns {@code Boolean.TRUE};
162      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
163      * If a new {@code Boolean} instance is not required, this method
164      * should generally be used in preference to the constructor
165      * {@link #Boolean(boolean)}, as this method is likely to yield
166      * significantly better space and time performance.
167      *
168      * @param  b a boolean value.
169      * @return a {@code Boolean} instance representing {@code b}.
170      * @since  1.4
171      */
172     @IntrinsicCandidate
173     public static Boolean valueOf(boolean b) {
174         return (b ? TRUE : FALSE);
175     }
176 
177     /**
178      * Returns a {@code Boolean} with a value represented by the
179      * specified string.  The {@code Boolean} returned represents a
180      * true value if the string argument is not {@code null}
181      * and is equal, ignoring case, to the string {@code "true"}.
182      * Otherwise, a false value is returned, including for a null
183      * argument.
184      *
185      * @param   s   a string.
186      * @return  the {@code Boolean} value represented by the string.
187      */
188     public static Boolean valueOf(String s) {
189         return parseBoolean(s) ? TRUE : FALSE;
190     }
191 
192     /**
193      * Returns a {@code String} object representing the specified
194      * boolean.  If the specified boolean is {@code true}, then
195      * the string {@code "true"} will be returned, otherwise the
196      * string {@code "false"} will be returned.
197      *
198      * @param b the boolean to be converted
199      * @return the string representation of the specified {@code boolean}
200      * @since 1.4
201      */
202     public static String toString(boolean b) {
203         return String.valueOf(b);
204     }
205 
206     /**
207      * Returns a {@code String} object representing this Boolean's
208      * value.  If this object represents the value {@code true},
209      * a string equal to {@code "true"} is returned. Otherwise, a
210      * string equal to {@code "false"} is returned.
211      *
212      * @return  a string representation of this object.
213      */
214     @Override
215     public String toString() {
216         return String.valueOf(value);
217     }
218 
219     /**
220      * Returns a hash code for this {@code Boolean} object.
221      *
222      * @return  the integer {@code 1231} if this object represents
223      * {@code true}; returns the integer {@code 1237} if this
224      * object represents {@code false}.
225      */
226     @Override
227     public int hashCode() {
228         return Boolean.hashCode(value);
229     }
230 
231     /**
232      * Returns a hash code for a {@code boolean} value; compatible with
233      * {@code Boolean.hashCode()}.
234      *
235      * @param value the value to hash
236      * @return a hash code value for a {@code boolean} value.
237      * @since 1.8
238      */
239     public static int hashCode(boolean value) {
240         return value ? 1231 : 1237;
241     }
242 
243     /**
244      * Returns {@code true} if and only if the argument is not
245      * {@code null} and is a {@code Boolean} object that
246      * represents the same {@code boolean} value as this object.
247      *
248      * @param   obj   the object to compare with.
249      * @return  {@code true} if the Boolean objects represent the
250      *          same value; {@code false} otherwise.
251      */
252     public boolean equals(Object obj) {
253         if (obj instanceof Boolean b) {
254             return value == b.booleanValue();
255         }
256         return false;
257     }
258 
259     /**
260      * Returns {@code true} if and only if the system property named
261      * by the argument exists and is equal to, ignoring case, the
262      * string {@code "true"}.
263      * A system property is accessible through {@code getProperty}, a
264      * method defined by the {@code System} class.  <p> If there is no
265      * property with the specified name, or if the specified name is
266      * empty or null, then {@code false} is returned.
267      *
268      * @param   name   the system property name.
269      * @return  the {@code boolean} value of the system property.
270      * @see     java.lang.System#getProperty(java.lang.String)
271      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
272      */
273     public static boolean getBoolean(String name) {
274         return name != null && !name.isEmpty() && parseBoolean(System.getProperty(name));
275     }
276 
277     /**
278      * Compares this {@code Boolean} instance with another.
279      *
280      * @param   b the {@code Boolean} instance to be compared
281      * @return  zero if this object represents the same boolean value as the
282      *          argument; a positive value if this object represents true
283      *          and the argument represents false; and a negative value if
284      *          this object represents false and the argument represents true
285      * @throws  NullPointerException if the argument is {@code null}
286      * @see     Comparable
287      * @since  1.5
288      */
289     public int compareTo(Boolean b) {
290         return compare(this.value, b.value);
291     }
292 
293     /**
294      * Compares two {@code boolean} values.
295      * The value returned is identical to what would be returned by:
296      * <pre>
297      *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
298      * </pre>
299      *
300      * @param  x the first {@code boolean} to compare
301      * @param  y the second {@code boolean} to compare
302      * @return the value {@code 0} if {@code x == y};
303      *         a value less than {@code 0} if {@code !x && y}; and
304      *         a value greater than {@code 0} if {@code x && !y}
305      * @since 1.7
306      */
307     public static int compare(boolean x, boolean y) {
308         return (x == y) ? 0 : (x ? 1 : -1);
309     }
310 
311     /**
312      * Returns the result of applying the logical AND operator to the
313      * specified {@code boolean} operands.
314      *
315      * @param a the first operand
316      * @param b the second operand
317      * @return the logical AND of {@code a} and {@code b}
318      * @see java.util.function.BinaryOperator
319      * @since 1.8
320      */
321     public static boolean logicalAnd(boolean a, boolean b) {
322         return a && b;
323     }
324 
325     /**
326      * Returns the result of applying the logical OR operator to the
327      * specified {@code boolean} operands.
328      *
329      * @param a the first operand
330      * @param b the second operand
331      * @return the logical OR of {@code a} and {@code b}
332      * @see java.util.function.BinaryOperator
333      * @since 1.8
334      */
335     public static boolean logicalOr(boolean a, boolean b) {
336         return a || b;
337     }
338 
339     /**
340      * Returns the result of applying the logical XOR operator to the
341      * specified {@code boolean} operands.
342      *
343      * @param a the first operand
344      * @param b the second operand
345      * @return  the logical XOR of {@code a} and {@code b}
346      * @see java.util.function.BinaryOperator
347      * @since 1.8
348      */
349     public static boolean logicalXor(boolean a, boolean b) {
350         return a ^ b;
351     }
352 
353     /**
354      * Returns an {@link Optional} containing the nominal descriptor for this
355      * instance.
356      *
357      * @return an {@link Optional} describing the {@linkplain Boolean} instance
358      * @since 15
359      */
360     @Override
361     public Optional<DynamicConstantDesc<Boolean>> describeConstable() {
362         return Optional.of(value ? ConstantDescs.TRUE : ConstantDescs.FALSE);
363     }
364 }