KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > Boolean


1 /*
2  * @(#)Boolean.java 1.51 04/05/11
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang;
9
10 /**
11  * The Boolean class wraps a value of the primitive type
12  * <code>boolean</code> in an object. An object of type
13  * <code>Boolean</code> contains a single field whose type is
14  * <code>boolean</code>.
15  * <p>
16  * In addition, this class provides many methods for
17  * converting a <code>boolean</code> to a <code>String</code> and a
18  * <code>String</code> to a <code>boolean</code>, as well as other
19  * constants and methods useful when dealing with a
20  * <code>boolean</code>.
21  *
22  * @author Arthur van Hoff
23  * @version 1.51, 05/11/04
24  * @since JDK1.0
25  */

26 public final class Boolean implements java.io.Serializable JavaDoc,
27                                       Comparable JavaDoc<Boolean JavaDoc>
28 {
29     /**
30      * The <code>Boolean</code> object corresponding to the primitive
31      * value <code>true</code>.
32      */

33     public static final Boolean JavaDoc TRUE = new Boolean JavaDoc(true);
34
35     /**
36      * The <code>Boolean</code> object corresponding to the primitive
37      * value <code>false</code>.
38      */

39     public static final Boolean JavaDoc FALSE = new Boolean JavaDoc(false);
40
41     /**
42      * The Class object representing the primitive type boolean.
43      *
44      * @since JDK1.1
45      */

46     public static final Class JavaDoc<Boolean JavaDoc> TYPE = Class.getPrimitiveClass("boolean");
47
48     /**
49      * The value of the Boolean.
50      *
51      * @serial
52      */

53     private final boolean value;
54
55     /** use serialVersionUID from JDK 1.0.2 for interoperability */
56     private static final long serialVersionUID = -3665804199014368530L;
57
58     /**
59      * Allocates a <code>Boolean</code> object representing the
60      * <code>value</code> argument.
61      *
62      * <p><b>Note: It is rarely appropriate to use this constructor.
63      * Unless a <i>new</i> instance is required, the static factory
64      * {@link #valueOf(boolean)} is generally a better choice. It is
65      * likely to yield significantly better space and time performance.</b>
66      *
67      * @param value the value of the <code>Boolean</code>.
68      */

69     public Boolean(boolean value) {
70     this.value = value;
71     }
72
73     /**
74      * Allocates a <code>Boolean</code> object representing the value
75      * <code>true</code> if the string argument is not <code>null</code>
76      * and is equal, ignoring case, to the string <code>"true"</code>.
77      * Otherwise, allocate a <code>Boolean</code> object representing the
78      * value <code>false</code>. Examples:<p>
79      * <tt>new&nbsp;Boolean("True")</tt> produces a <tt>Boolean</tt> object
80      * that represents <tt>true</tt>.<br>
81      * <tt>new&nbsp;Boolean("yes")</tt> produces a <tt>Boolean</tt> object
82      * that represents <tt>false</tt>.
83      *
84      * @param s the string to be converted to a <code>Boolean</code>.
85      */

86     public Boolean(String JavaDoc s) {
87     this(toBoolean(s));
88     }
89
90     /**
91      * Parses the string argument as a boolean. The <code>boolean</code>
92      * returned represents the value <code>true</code> if the string argument
93      * is not <code>null</code> and is equal, ignoring case, to the string
94      * <code>"true"</code>.
95      *
96      * @param s the <code>String</code> containing the boolean
97      * representation to be parsed
98      * @return the boolean represented by the string argument
99      * @since 1.5
100      */

101     public static boolean parseBoolean(String JavaDoc s) {
102         return toBoolean(s);
103     }
104
105     /**
106      * Returns the value of this <tt>Boolean</tt> object as a boolean
107      * primitive.
108      *
109      * @return the primitive <code>boolean</code> value of this object.
110      */

111     public boolean booleanValue() {
112     return value;
113     }
114
115     /**
116      * Returns a <tt>Boolean</tt> instance representing the specified
117      * <tt>boolean</tt> value. If the specified <tt>boolean</tt> value
118      * is <tt>true</tt>, this method returns <tt>Boolean.TRUE</tt>;
119      * if it is <tt>false</tt>, this method returns <tt>Boolean.FALSE</tt>.
120      * If a new <tt>Boolean</tt> instance is not required, this method
121      * should generally be used in preference to the constructor
122      * {@link #Boolean(boolean)}, as this method is likely to yield
123      * significantly better space and time performance.
124      *
125      * @param b a boolean value.
126      * @return a <tt>Boolean</tt> instance representing <tt>b</tt>.
127      * @since 1.4
128      */

129     public static Boolean JavaDoc valueOf(boolean b) {
130         return (b ? TRUE : FALSE);
131     }
132
133     /**
134      * Returns a <code>Boolean</code> with a value represented by the
135      * specified String. The <code>Boolean</code> returned represents the
136      * value <code>true</code> if the string argument is not <code>null</code>
137      * and is equal, ignoring case, to the string <code>"true"</code>. <p>
138      * Example: <tt>Boolean.valueOf("True")</tt> returns <tt>true</tt>.<br>
139      * Example: <tt>Boolean.valueOf("yes")</tt> returns <tt>false</tt>.
140      *
141      * @param s a string.
142      * @return the <code>Boolean</code> value represented by the string.
143      */

144     public static Boolean JavaDoc valueOf(String JavaDoc s) {
145     return toBoolean(s) ? TRUE : FALSE;
146     }
147
148     /**
149      * Returns a <tt>String</tt> object representing the specified
150      * boolean. If the specified boolean is <code>true</code>, then
151      * the string <tt>"true"</tt> will be returned, otherwise the
152      * string <tt>"false"</tt> will be returned.
153      *
154      * @param b the boolean to be converted
155      * @return the string representation of the specified <code>boolean</code>
156      * @since 1.4
157      */

158     public static String JavaDoc toString(boolean b) {
159         return b ? "true" : "false";
160     }
161
162     /**
163      * Returns a <tt>String</tt> object representing this Boolean's
164      * value. If this object represents the value <code>true</code>,
165      * a string equal to <code>"true"</code> is returned. Otherwise, a
166      * string equal to <code>"false"</code> is returned.
167      *
168      * @return a string representation of this object.
169      */

170     public String JavaDoc toString() {
171     return value ? "true" : "false";
172     }
173
174     /**
175      * Returns a hash code for this <tt>Boolean</tt> object.
176      *
177      * @return the integer <tt>1231</tt> if this object represents
178      * <tt>true</tt>; returns the integer <tt>1237</tt> if this
179      * object represents <tt>false</tt>.
180      */

181     public int hashCode() {
182     return value ? 1231 : 1237;
183     }
184
185     /**
186      * Returns <code>true</code> if and only if the argument is not
187      * <code>null</code> and is a <code>Boolean </code>object that
188      * represents the same <code>boolean</code> value as this object.
189      *
190      * @param obj the object to compare with.
191      * @return <code>true</code> if the Boolean objects represent the
192      * same value; <code>false</code> otherwise.
193      */

194     public boolean equals(Object JavaDoc obj) {
195     if (obj instanceof Boolean JavaDoc) {
196         return value == ((Boolean JavaDoc)obj).booleanValue();
197     }
198     return false;
199     }
200
201     /**
202      * Returns <code>true</code> if and only if the system property
203      * named by the argument exists and is equal to the string
204      * <code>"true"</code>. (Beginning with version 1.0.2 of the
205      * Java<font size="-2"><sup>TM</sup></font> platform, the test of
206      * this string is case insensitive.) A system property is accessible
207      * through <code>getProperty</code>, a method defined by the
208      * <code>System</code> class.
209      * <p>
210      * If there is no property with the specified name, or if the specified
211      * name is empty or null, then <code>false</code> is returned.
212      *
213      * @param name the system property name.
214      * @return the <code>boolean</code> value of the system property.
215      * @see java.lang.System#getProperty(java.lang.String)
216      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
217      */

218     public static boolean getBoolean(String JavaDoc name) {
219         boolean result = false;
220         try {
221             result = toBoolean(System.getProperty(name));
222         } catch (IllegalArgumentException JavaDoc e) {
223         } catch (NullPointerException JavaDoc e) {
224         }
225         return result;
226     }
227
228     /**
229      * Compares this <tt>Boolean</tt> instance with another.
230      *
231      * @param b the <tt>Boolean</tt> instance to be compared
232      * @return zero if this object represents the same boolean value as the
233      * argument; a positive value if this object represents true
234      * and the argument represents false; and a negative value if
235      * this object represents false and the argument represents true
236      * @throws NullPointerException if the argument is <tt>null</tt>
237      * @see Comparable
238      * @since 1.5
239      */

240     public int compareTo(Boolean JavaDoc b) {
241         return (b.value == value ? 0 : (value ? 1 : -1));
242     }
243
244     private static boolean toBoolean(String JavaDoc name) {
245     return ((name != null) && name.equalsIgnoreCase("true"));
246     }
247 }
248
Popular Tags