• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package java.lang;
19 
20 import java.io.Serializable;
21 
22 /**
23  * The wrapper for the primitive type {@code boolean}.
24  *
25  * @since 1.0
26  */
27 public final class Boolean implements Serializable, Comparable<Boolean> {
28 
29     private static final long serialVersionUID = -3665804199014368530L;
30 
31     /**
32      * The boolean value of the receiver.
33      */
34     private final boolean value;
35 
36     /**
37      * The {@link Class} object that represents the primitive type {@code
38      * boolean}.
39      */
40     @SuppressWarnings("unchecked")
41     public static final Class<Boolean> TYPE
42              = (Class<Boolean>) boolean[].class.getComponentType();
43     // Note: Boolean.TYPE can't be set to "boolean.class", since *that* is
44     // defined to be "java.lang.Boolean.TYPE";
45 
46     /**
47      * The {@code Boolean} object that represents the primitive value
48      * {@code true}.
49      */
50     public static final Boolean TRUE = new Boolean(true);
51 
52     /**
53      * The {@code Boolean} object that represents the primitive value
54      * {@code false}.
55      */
56     public static final Boolean FALSE = new Boolean(false);
57 
58     /**
59      * Constructs a new {@code Boolean} with its boolean value specified by
60      * {@code string}. If {@code string} is not {@code null} and is equal to
61      * "true" using a non-case sensitive comparison, the result will be a
62      * Boolean representing the primitive value {@code true}, otherwise it will
63      * be a Boolean representing the primitive value {@code false}.
64      *
65      * @param string
66      *            the string representing a boolean value.
67      */
Boolean(String string)68     public Boolean(String string) {
69         this(parseBoolean(string));
70     }
71 
72     /**
73      * Constructs a new {@code Boolean} with the specified primitive boolean
74      * value.
75      *
76      * @param value
77      *            the primitive boolean value, {@code true} or {@code false}.
78      */
Boolean(boolean value)79     public Boolean(boolean value) {
80         this.value = value;
81     }
82 
83     /**
84      * Gets the primitive value of this boolean, either {@code true} or
85      * {@code false}.
86      *
87      * @return this object's primitive value, {@code true} or {@code false}.
88      */
booleanValue()89     public boolean booleanValue() {
90         return value;
91     }
92 
93     /**
94      * Compares this instance with the specified object and indicates if they
95      * are equal. In order to be equal, {@code o} must be an instance of
96      * {@code Boolean} and have the same boolean value as this object.
97      *
98      * @param o
99      *            the object to compare this boolean with.
100      * @return {@code true} if the specified object is equal to this
101      *         {@code Boolean}; {@code false} otherwise.
102      */
103     @Override
104     @FindBugsSuppressWarnings("RC_REF_COMPARISON_BAD_PRACTICE_BOOLEAN")
equals(Object o)105     public boolean equals(Object o) {
106         return (o == this) || ((o instanceof Boolean) && (((Boolean) o).value == value));
107     }
108 
109     /**
110      * Compares this object to the specified boolean object to determine their
111      * relative order.
112      *
113      * @param that
114      *            the boolean object to compare this object to.
115      * @return 0 if the value of this boolean and the value of {@code that} are
116      *         equal; a positive value if the value of this boolean is
117      *         {@code true} and the value of {@code that} is {@code false}; a
118      *         negative value if the value if this boolean is {@code false} and
119      *         the value of {@code that} is {@code true}.
120      * @see java.lang.Comparable
121      * @since 1.5
122      */
compareTo(Boolean that)123     public int compareTo(Boolean that) {
124         return compare(value, that.value);
125     }
126 
127     /**
128      * Compares two {@code boolean} values.
129      * @return 0 if lhs = rhs, less than 0 if lhs &lt; rhs, and greater than 0 if lhs &gt; rhs.
130      *         (Where true &gt; false.)
131      * @since 1.7
132      * @hide 1.7
133      */
compare(boolean lhs, boolean rhs)134     public static int compare(boolean lhs, boolean rhs) {
135         return lhs == rhs ? 0 : lhs ? 1 : -1;
136     }
137 
138     /**
139      * Returns an integer hash code for this boolean.
140      *
141      * @return this boolean's hash code, which is {@code 1231} for {@code true}
142      *         values and {@code 1237} for {@code false} values.
143      */
144     @Override
hashCode()145     public int hashCode() {
146         return value ? 1231 : 1237;
147     }
148 
149     /**
150      * Returns a string containing a concise, human-readable description of this
151      * boolean.
152      *
153      * @return "true" if the value of this boolean is {@code true}, "false"
154      *         otherwise.
155      */
156     @Override
toString()157     public String toString() {
158         return String.valueOf(value);
159     }
160 
161     /**
162      * Returns the {@code boolean} value of the system property identified by
163      * {@code string}.
164      *
165      * @param string
166      *            the name of the requested system property.
167      * @return {@code true} if the system property named by {@code string}
168      *         exists and it is equal to "true" using case insensitive
169      *         comparison, {@code false} otherwise.
170      * @see System#getProperty(String)
171      */
getBoolean(String string)172     public static boolean getBoolean(String string) {
173         if (string == null || string.length() == 0) {
174             return false;
175         }
176         return (parseBoolean(System.getProperty(string)));
177     }
178 
179     /**
180      * Parses the specified string as a {@code boolean}.
181      *
182      * @param s
183      *            the string representation of a boolean value.
184      * @return {@code true} if {@code s} is not {@code null} and is equal to
185      *         {@code "true"} using case insensitive comparison, {@code false}
186      *         otherwise.
187      * @since 1.5
188      */
parseBoolean(String s)189     public static boolean parseBoolean(String s) {
190         return "true".equalsIgnoreCase(s);
191     }
192 
193     /**
194      * Converts the specified boolean to its string representation.
195      *
196      * @param value
197      *            the boolean to convert.
198      * @return "true" if {@code value} is {@code true}, "false" otherwise.
199      */
toString(boolean value)200     public static String toString(boolean value) {
201         return String.valueOf(value);
202     }
203 
204     /**
205      * Parses the specified string as a boolean value.
206      *
207      * @param string
208      *            the string representation of a boolean value.
209      * @return {@code Boolean.TRUE} if {@code string} is equal to "true" using
210      *         case insensitive comparison, {@code Boolean.FALSE} otherwise.
211      * @see #parseBoolean(String)
212      */
valueOf(String string)213     public static Boolean valueOf(String string) {
214         return parseBoolean(string) ? Boolean.TRUE : Boolean.FALSE;
215     }
216 
217     /**
218      * Returns a {@code Boolean} instance for the specified boolean value.
219      * <p>
220      * If it is not necessary to get a new {@code Boolean} instance, it is
221      * recommended to use this method instead of the constructor, since it
222      * returns its static instances, which results in better performance.
223      *
224      * @param b
225      *            the boolean to convert to a {@code Boolean}.
226      * @return {@code Boolean.TRUE} if {@code b} is equal to {@code true},
227      *         {@code Boolean.FALSE} otherwise.
228      */
valueOf(boolean b)229     public static Boolean valueOf(boolean b) {
230         return b ? Boolean.TRUE : Boolean.FALSE;
231     }
232 }
233