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. package java.lang;
  8. /**
  9. * The Boolean class wraps a value of the primitive type
  10. * <code>boolean</code> in an object. An object of type
  11. * <code>Boolean</code> contains a single field whose type is
  12. * <code>boolean</code>.
  13. * <p>
  14. * In addition, this class provides many methods for
  15. * converting a <code>boolean</code> to a <code>String</code> and a
  16. * <code>String</code> to a <code>boolean</code>, as well as other
  17. * constants and methods useful when dealing with a
  18. * <code>boolean</code>.
  19. *
  20. * @author Arthur van Hoff
  21. * @version 1.51, 05/11/04
  22. * @since JDK1.0
  23. */
  24. public final class Boolean implements java.io.Serializable,
  25. Comparable<Boolean>
  26. {
  27. /**
  28. * The <code>Boolean</code> object corresponding to the primitive
  29. * value <code>true</code>.
  30. */
  31. public static final Boolean TRUE = new Boolean(true);
  32. /**
  33. * The <code>Boolean</code> object corresponding to the primitive
  34. * value <code>false</code>.
  35. */
  36. public static final Boolean FALSE = new Boolean(false);
  37. /**
  38. * The Class object representing the primitive type boolean.
  39. *
  40. * @since JDK1.1
  41. */
  42. public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
  43. /**
  44. * The value of the Boolean.
  45. *
  46. * @serial
  47. */
  48. private final boolean value;
  49. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  50. private static final long serialVersionUID = -3665804199014368530L;
  51. /**
  52. * Allocates a <code>Boolean</code> object representing the
  53. * <code>value</code> argument.
  54. *
  55. * <p><b>Note: It is rarely appropriate to use this constructor.
  56. * Unless a <i>new</i> instance is required, the static factory
  57. * {@link #valueOf(boolean)} is generally a better choice. It is
  58. * likely to yield significantly better space and time performance.</b>
  59. *
  60. * @param value the value of the <code>Boolean</code>.
  61. */
  62. public Boolean(boolean value) {
  63. this.value = value;
  64. }
  65. /**
  66. * Allocates a <code>Boolean</code> object representing the value
  67. * <code>true</code> if the string argument is not <code>null</code>
  68. * and is equal, ignoring case, to the string <code>"true"</code>.
  69. * Otherwise, allocate a <code>Boolean</code> object representing the
  70. * value <code>false</code>. Examples:<p>
  71. * <tt>new Boolean("True")</tt> produces a <tt>Boolean</tt> object
  72. * that represents <tt>true</tt>.<br>
  73. * <tt>new Boolean("yes")</tt> produces a <tt>Boolean</tt> object
  74. * that represents <tt>false</tt>.
  75. *
  76. * @param s the string to be converted to a <code>Boolean</code>.
  77. */
  78. public Boolean(String s) {
  79. this(toBoolean(s));
  80. }
  81. /**
  82. * Parses the string argument as a boolean. The <code>boolean</code>
  83. * returned represents the value <code>true</code> if the string argument
  84. * is not <code>null</code> and is equal, ignoring case, to the string
  85. * <code>"true"</code>.
  86. *
  87. * @param s the <code>String</code> containing the boolean
  88. * representation to be parsed
  89. * @return the boolean represented by the string argument
  90. * @since 1.5
  91. */
  92. public static boolean parseBoolean(String s) {
  93. return toBoolean(s);
  94. }
  95. /**
  96. * Returns the value of this <tt>Boolean</tt> object as a boolean
  97. * primitive.
  98. *
  99. * @return the primitive <code>boolean</code> value of this object.
  100. */
  101. public boolean booleanValue() {
  102. return value;
  103. }
  104. /**
  105. * Returns a <tt>Boolean</tt> instance representing the specified
  106. * <tt>boolean</tt> value. If the specified <tt>boolean</tt> value
  107. * is <tt>true</tt>, this method returns <tt>Boolean.TRUE</tt>
  108. * if it is <tt>false</tt>, this method returns <tt>Boolean.FALSE</tt>.
  109. * If a new <tt>Boolean</tt> instance is not required, this method
  110. * should generally be used in preference to the constructor
  111. * {@link #Boolean(boolean)}, as this method is likely to yield
  112. * significantly better space and time performance.
  113. *
  114. * @param b a boolean value.
  115. * @return a <tt>Boolean</tt> instance representing <tt>b</tt>.
  116. * @since 1.4
  117. */
  118. public static Boolean valueOf(boolean b) {
  119. return (b ? TRUE : FALSE);
  120. }
  121. /**
  122. * Returns a <code>Boolean</code> with a value represented by the
  123. * specified String. The <code>Boolean</code> returned represents the
  124. * value <code>true</code> if the string argument is not <code>null</code>
  125. * and is equal, ignoring case, to the string <code>"true"</code>. <p>
  126. * Example: <tt>Boolean.valueOf("True")</tt> returns <tt>true</tt>.<br>
  127. * Example: <tt>Boolean.valueOf("yes")</tt> returns <tt>false</tt>.
  128. *
  129. * @param s a string.
  130. * @return the <code>Boolean</code> value represented by the string.
  131. */
  132. public static Boolean valueOf(String s) {
  133. return toBoolean(s) ? TRUE : FALSE;
  134. }
  135. /**
  136. * Returns a <tt>String</tt> object representing the specified
  137. * boolean. If the specified boolean is <code>true</code>, then
  138. * the string <tt>"true"</tt> will be returned, otherwise the
  139. * string <tt>"false"</tt> will be returned.
  140. *
  141. * @param b the boolean to be converted
  142. * @return the string representation of the specified <code>boolean</code>
  143. * @since 1.4
  144. */
  145. public static String toString(boolean b) {
  146. return b ? "true" : "false";
  147. }
  148. /**
  149. * Returns a <tt>String</tt> object representing this Boolean's
  150. * value. If this object represents the value <code>true</code>,
  151. * a string equal to <code>"true"</code> is returned. Otherwise, a
  152. * string equal to <code>"false"</code> is returned.
  153. *
  154. * @return a string representation of this object.
  155. */
  156. public String toString() {
  157. return value ? "true" : "false";
  158. }
  159. /**
  160. * Returns a hash code for this <tt>Boolean</tt> object.
  161. *
  162. * @return the integer <tt>1231</tt> if this object represents
  163. * <tt>true</tt> returns the integer <tt>1237</tt> if this
  164. * object represents <tt>false</tt>.
  165. */
  166. public int hashCode() {
  167. return value ? 1231 : 1237;
  168. }
  169. /**
  170. * Returns <code>true</code> if and only if the argument is not
  171. * <code>null</code> and is a <code>Boolean </code>object that
  172. * represents the same <code>boolean</code> value as this object.
  173. *
  174. * @param obj the object to compare with.
  175. * @return <code>true</code> if the Boolean objects represent the
  176. * same value; <code>false</code> otherwise.
  177. */
  178. public boolean equals(Object obj) {
  179. if (obj instanceof Boolean) {
  180. return value == ((Boolean)obj).booleanValue();
  181. }
  182. return false;
  183. }
  184. /**
  185. * Returns <code>true</code> if and only if the system property
  186. * named by the argument exists and is equal to the string
  187. * <code>"true"</code>. (Beginning with version 1.0.2 of the
  188. * Java<font size="-2"><sup>TM</sup></font> platform, the test of
  189. * this string is case insensitive.) A system property is accessible
  190. * through <code>getProperty</code>, a method defined by the
  191. * <code>System</code> class.
  192. * <p>
  193. * If there is no property with the specified name, or if the specified
  194. * name is empty or null, then <code>false</code> is returned.
  195. *
  196. * @param name the system property name.
  197. * @return the <code>boolean</code> value of the system property.
  198. * @see java.lang.System#getProperty(java.lang.String)
  199. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  200. */
  201. public static boolean getBoolean(String name) {
  202. boolean result = false;
  203. try {
  204. result = toBoolean(System.getProperty(name));
  205. } catch (IllegalArgumentException e) {
  206. } catch (NullPointerException e) {
  207. }
  208. return result;
  209. }
  210. /**
  211. * Compares this <tt>Boolean</tt> instance with another.
  212. *
  213. * @param b the <tt>Boolean</tt> instance to be compared
  214. * @return zero if this object represents the same boolean value as the
  215. * argument; a positive value if this object represents true
  216. * and the argument represents false; and a negative value if
  217. * this object represents false and the argument represents true
  218. * @throws NullPointerException if the argument is <tt>null</tt>
  219. * @see Comparable
  220. * @since 1.5
  221. */
  222. public int compareTo(Boolean b) {
  223. return (b.value == value ? 0 : (value ? 1 : -1));
  224. }
  225. private static boolean toBoolean(String name) {
  226. return ((name != null) && name.equalsIgnoreCase("true"));
  227. }
  228. }