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