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