001 /*
002 * Copyright 1994-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.lang;
027
028 /**
029 * The Boolean class wraps a value of the primitive type
030 * {@code boolean} in an object. An object of type
031 * {@code Boolean} contains a single field whose type is
032 * {@code boolean}.
033 * <p>
034 * In addition, this class provides many methods for
035 * converting a {@code boolean} to a {@code String} and a
036 * {@code String} to a {@code boolean}, as well as other
037 * constants and methods useful when dealing with a
038 * {@code boolean}.
039 *
040 * @author Arthur van Hoff
041 * @version 1.60, 05/05/07
042 * @since JDK1.0
043 */
044 public final class Boolean implements java.io.Serializable,
045 Comparable<Boolean> {
046 /**
047 * The {@code Boolean} object corresponding to the primitive
048 * value {@code true}.
049 */
050 public static final Boolean TRUE = new Boolean(true);
051
052 /**
053 * The {@code Boolean} object corresponding to the primitive
054 * value {@code false}.
055 */
056 public static final Boolean FALSE = new Boolean(false);
057
058 /**
059 * The Class object representing the primitive type boolean.
060 *
061 * @since JDK1.1
062 */
063 public static final Class<Boolean> TYPE = Class
064 .getPrimitiveClass("boolean");
065
066 /**
067 * The value of the Boolean.
068 *
069 * @serial
070 */
071 private final boolean value;
072
073 /** use serialVersionUID from JDK 1.0.2 for interoperability */
074 private static final long serialVersionUID = -3665804199014368530L;
075
076 /**
077 * Allocates a {@code Boolean} object representing the
078 * {@code value} argument.
079 *
080 * <p><b>Note: It is rarely appropriate to use this constructor.
081 * Unless a <i>new</i> instance is required, the static factory
082 * {@link #valueOf(boolean)} is generally a better choice. It is
083 * likely to yield significantly better space and time performance.</b>
084 *
085 * @param value the value of the {@code Boolean}.
086 */
087 public Boolean(boolean value) {
088 this .value = value;
089 }
090
091 /**
092 * Allocates a {@code Boolean} object representing the value
093 * {@code true} if the string argument is not {@code null}
094 * and is equal, ignoring case, to the string {@code "true"}.
095 * Otherwise, allocate a {@code Boolean} object representing the
096 * value {@code false}. Examples:<p>
097 * {@code new Boolean("True")} produces a {@code Boolean} object
098 * that represents {@code true}.<br>
099 * {@code new Boolean("yes")} produces a {@code Boolean} object
100 * that represents {@code false}.
101 *
102 * @param s the string to be converted to a {@code Boolean}.
103 */
104 public Boolean(String s) {
105 this (toBoolean(s));
106 }
107
108 /**
109 * Parses the string argument as a boolean. The {@code boolean}
110 * returned represents the value {@code true} if the string argument
111 * is not {@code null} and is equal, ignoring case, to the string
112 * {@code "true"}. <p>
113 * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
114 * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
115 *
116 * @param s the {@code String} containing the boolean
117 * representation to be parsed
118 * @return the boolean represented by the string argument
119 * @since 1.5
120 */
121 public static boolean parseBoolean(String s) {
122 return toBoolean(s);
123 }
124
125 /**
126 * Returns the value of this {@code Boolean} object as a boolean
127 * primitive.
128 *
129 * @return the primitive {@code boolean} value of this object.
130 */
131 public boolean booleanValue() {
132 return value;
133 }
134
135 /**
136 * Returns a {@code Boolean} instance representing the specified
137 * {@code boolean} value. If the specified {@code boolean} value
138 * is {@code true}, this method returns {@code Boolean.TRUE};
139 * if it is {@code false}, this method returns {@code Boolean.FALSE}.
140 * If a new {@code Boolean} instance is not required, this method
141 * should generally be used in preference to the constructor
142 * {@link #Boolean(boolean)}, as this method is likely to yield
143 * significantly better space and time performance.
144 *
145 * @param b a boolean value.
146 * @return a {@code Boolean} instance representing {@code b}.
147 * @since 1.4
148 */
149 public static Boolean valueOf(boolean b) {
150 return (b ? TRUE : FALSE);
151 }
152
153 /**
154 * Returns a {@code Boolean} with a value represented by the
155 * specified string. The {@code Boolean} returned represents a
156 * true value if the string argument is not {@code null}
157 * and is equal, ignoring case, to the string {@code "true"}.
158 *
159 * @param s a string.
160 * @return the {@code Boolean} value represented by the string.
161 */
162 public static Boolean valueOf(String s) {
163 return toBoolean(s) ? TRUE : FALSE;
164 }
165
166 /**
167 * Returns a {@code String} object representing the specified
168 * boolean. If the specified boolean is {@code true}, then
169 * the string {@code "true"} will be returned, otherwise the
170 * string {@code "false"} will be returned.
171 *
172 * @param b the boolean to be converted
173 * @return the string representation of the specified {@code boolean}
174 * @since 1.4
175 */
176 public static String toString(boolean b) {
177 return b ? "true" : "false";
178 }
179
180 /**
181 * Returns a {@code String} object representing this Boolean's
182 * value. If this object represents the value {@code true},
183 * a string equal to {@code "true"} is returned. Otherwise, a
184 * string equal to {@code "false"} is returned.
185 *
186 * @return a string representation of this object.
187 */
188 public String toString() {
189 return value ? "true" : "false";
190 }
191
192 /**
193 * Returns a hash code for this {@code Boolean} object.
194 *
195 * @return the integer {@code 1231} if this object represents
196 * {@code true}; returns the integer {@code 1237} if this
197 * object represents {@code false}.
198 */
199 public int hashCode() {
200 return value ? 1231 : 1237;
201 }
202
203 /**
204 * Returns {@code true} if and only if the argument is not
205 * {@code null} and is a {@code Boolean} object that
206 * represents the same {@code boolean} value as this object.
207 *
208 * @param obj the object to compare with.
209 * @return {@code true} if the Boolean objects represent the
210 * same value; {@code false} otherwise.
211 */
212 public boolean equals(Object obj) {
213 if (obj instanceof Boolean) {
214 return value == ((Boolean) obj).booleanValue();
215 }
216 return false;
217 }
218
219 /**
220 * Returns {@code true} if and only if the system property
221 * named by the argument exists and is equal to the string
222 * {@code "true"}. (Beginning with version 1.0.2 of the
223 * Java<small><sup>TM</sup></small> platform, the test of
224 * this string is case insensitive.) A system property is accessible
225 * through {@code getProperty}, a method defined by the
226 * {@code System} class.
227 * <p>
228 * If there is no property with the specified name, or if the specified
229 * name is empty or null, then {@code false} is returned.
230 *
231 * @param name the system property name.
232 * @return the {@code boolean} value of the system property.
233 * @see java.lang.System#getProperty(java.lang.String)
234 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
235 */
236 public static boolean getBoolean(String name) {
237 boolean result = false;
238 try {
239 result = toBoolean(System.getProperty(name));
240 } catch (IllegalArgumentException e) {
241 } catch (NullPointerException e) {
242 }
243 return result;
244 }
245
246 /**
247 * Compares this {@code Boolean} instance with another.
248 *
249 * @param b the {@code Boolean} instance to be compared
250 * @return zero if this object represents the same boolean value as the
251 * argument; a positive value if this object represents true
252 * and the argument represents false; and a negative value if
253 * this object represents false and the argument represents true
254 * @throws NullPointerException if the argument is {@code null}
255 * @see Comparable
256 * @since 1.5
257 */
258 public int compareTo(Boolean b) {
259 return (b.value == value ? 0 : (value ? 1 : -1));
260 }
261
262 private static boolean toBoolean(String name) {
263 return ((name != null) && name.equalsIgnoreCase("true"));
264 }
265 }
|