1. /*
  2. * @(#)Boolean.java 1.38 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. /**
  12. * The Boolean class wraps a value of the primitive type
  13. * <code>boolean</code> in an object. An object of type
  14. * <code>Boolean</code> contains a single field whose type is
  15. * <code>boolean</code>.
  16. * <p>
  17. * In addition, this class provides many methods for
  18. * converting a <code>boolean</code> to a <code>String</code> and a
  19. * <code>String</code> to a <code>boolean</code>, as well as other
  20. * constants and methods useful when dealing with a
  21. * <code>boolean</code>.
  22. *
  23. * @author Arthur van Hoff
  24. * @version 1.38, 02/02/00
  25. * @since JDK1.0
  26. */
  27. public final
  28. class Boolean implements java.io.Serializable {
  29. /**
  30. * The <code>Boolean</code> object corresponding to the primitive
  31. * value <code>true</code>.
  32. */
  33. public static final Boolean TRUE = new Boolean(true);
  34. /**
  35. * The <code>Boolean</code> object corresponding to the primitive
  36. * value <code>false</code>.
  37. */
  38. public static final Boolean FALSE = new Boolean(false);
  39. /**
  40. * The Class object representing the primitive type boolean.
  41. *
  42. * @since JDK1.1
  43. */
  44. public static final Class TYPE = Class.getPrimitiveClass("boolean");
  45. /**
  46. * The value of the Boolean.
  47. *
  48. * @serial
  49. */
  50. private boolean value;
  51. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  52. private static final long serialVersionUID = -3665804199014368530L;
  53. /**
  54. * Allocates a <code>Boolean</code> object representing the
  55. * <code>value</code> argument.
  56. *
  57. * @param value the value of the <code>Boolean</code>.
  58. */
  59. public Boolean(boolean value) {
  60. this.value = value;
  61. }
  62. /**
  63. * Allocates a <code>Boolean</code> object representing the value
  64. * <code>true</code> if the string argument is not <code>null</code>
  65. * and is equal, ignoring case, to the string <code>"true"</code>.
  66. * Otherwise, allocate a <code>Boolean</code> object representing the
  67. * value <code>false</code>. Examples:<p>
  68. * <tt>new Boolean("True")</tt> produces a <tt>Boolean</tt> object
  69. * that represents <tt>true</tt>.<br>
  70. * <tt>new Boolean("yes")</tt> produces a <tt>Boolean</tt> object
  71. * that represents <tt>false</tt>.
  72. *
  73. * @param s the string to be converted to a <code>Boolean</code>.
  74. */
  75. public Boolean(String s) {
  76. this(toBoolean(s));
  77. }
  78. /**
  79. * Returns the value of this <tt>Boolean</tt> object as a boolean
  80. * primitive.
  81. *
  82. * @return the primitive <code>boolean</code> value of this object.
  83. */
  84. public boolean booleanValue() {
  85. return value;
  86. }
  87. /**
  88. * Returns a <code>Boolean</code> with a value represented by the
  89. * specified String. The <code>Boolean</code> returned represents the
  90. * value <code>true</code> if the string argument is not <code>null</code>
  91. * and is equal, ignoring case, to the string <code>"true"</code>. <p>
  92. * Example: <tt>Boolean.valueOf("True")</tt> returns <tt>true</tt>.<br>
  93. * Example: <tt>Boolean.valueOf("yes")</tt> returns <tt>false</tt>.
  94. *
  95. * @param s a string.
  96. * @return the <code>Boolean</code> value represented by the string.
  97. */
  98. public static Boolean valueOf(String s) {
  99. return toBoolean(s) ? TRUE : FALSE;
  100. }
  101. /**
  102. * Returns a String object representing this Boolean's value.
  103. * If this object represents the value <code>true</code>, a string equal
  104. * to <code>"true"</code> is returned. Otherwise, a string equal to
  105. * <code>"false"</code> is returned.
  106. *
  107. * @return a string representation of this object.
  108. */
  109. public String toString() {
  110. return value ? "true" : "false";
  111. }
  112. /**
  113. * Returns a hash code for this <tt>Boolean</tt> object.
  114. *
  115. * @return the integer <tt>1231</tt> if this object represents
  116. * <tt>true</tt> returns the integer <tt>1237</tt> if this
  117. * object represents <tt>false</tt>.
  118. */
  119. public int hashCode() {
  120. return value ? 1231 : 1237;
  121. }
  122. /**
  123. * Returns <code>true</code> if and only if the argument is not
  124. * <code>null</code> and is a <code>Boolean </code>object that
  125. * represents the same <code>boolean</code> value as this object.
  126. *
  127. * @param obj the object to compare with.
  128. * @return <code>true</code> if the Boolean objects represent the
  129. * same value; <code>false</code> otherwise.
  130. */
  131. public boolean equals(Object obj) {
  132. if (obj instanceof Boolean) {
  133. return value == ((Boolean)obj).booleanValue();
  134. }
  135. return false;
  136. }
  137. /**
  138. * Returns <code>true</code> if and only if the system property
  139. * named by the argument exists and is equal to the string
  140. * <code>"true"</code>. (Beginning with version 1.0.2 of the
  141. * Java<font size="-2"><sup>TM</sup></font> platform, the test of
  142. * this string is case insensitive.) A system property is accessible
  143. * through <code>getProperty</code>, a method defined by the
  144. * <code>System</code> class.
  145. * <p>
  146. * If there is no property with the specified name, or if the specified
  147. * name is empty or null, then <code>false</code> is returned.
  148. *
  149. * @param name the system property name.
  150. * @return the <code>boolean</code> value of the system property.
  151. * @see java.lang.System#getProperty(java.lang.String)
  152. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  153. */
  154. public static boolean getBoolean(String name) {
  155. boolean result = false;
  156. try {
  157. result = toBoolean(System.getProperty(name));
  158. } catch (IllegalArgumentException e) {
  159. } catch (NullPointerException e) {
  160. }
  161. return result;
  162. }
  163. private static boolean toBoolean(String name) {
  164. return ((name != null) && name.equalsIgnoreCase("true"));
  165. }
  166. }