1. /*
  2. * @(#)Boolean.java 1.33 01/11/29
  3. *
  4. * Copyright 2002 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.29, 07/23/98
  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. * @param value the value of the <code>Boolean</code>.
  55. */
  56. public Boolean(boolean value) {
  57. this.value = value;
  58. }
  59. /**
  60. * Allocates a <code>Boolean</code> object representing the value
  61. * <code>true</code> if the string argument is not <code>null</code>
  62. * and is equal, ignoring case, to the string <code>"true"</code>.
  63. * Otherwise, allocate a <code>Boolean</code> object representing the
  64. * value <code>false</code>. Examples:<p>
  65. * <tt>new Boolean("True")</tt> produces a <tt>Boolean</tt> object
  66. * that represents <tt>true</tt>.<br>
  67. * <tt>new Boolean("yes")</tt> produces a <tt>Boolean</tt> object
  68. * that represents <tt>false</tt>.
  69. *
  70. * @param s the string to be converted to a <code>Boolean</code>.
  71. */
  72. public Boolean(String s) {
  73. this(toBoolean(s));
  74. }
  75. /**
  76. * Returns the value of this <tt>Boolean</tt> object as a boolean
  77. * primitive.
  78. *
  79. * @return the primitive <code>boolean</code> value of this object.
  80. */
  81. public boolean booleanValue() {
  82. return value;
  83. }
  84. /**
  85. * Returns the boolean value represented by the specified String.
  86. * A new <code>Boolean</code> object is constructed. This
  87. * <code>Boolean</code> represents the value <code>true</code> if the
  88. * string argument is not <code>null</code> and is equal, ignoring
  89. * case, to the string <code>"true"</code>. <p>
  90. * Example: <tt>Boolean.valueOf("True")</tt> returns <tt>true</tt>.<br>
  91. * Example: <tt>Boolean.valueOf("yes")</tt> returns <tt>false</tt>.
  92. *
  93. * @param s a string.
  94. * @return the <code>Boolean</code> value represented by the string.
  95. */
  96. public static Boolean valueOf(String s) {
  97. return new Boolean(toBoolean(s));
  98. }
  99. /**
  100. * Returns a String object representing this Boolean's value.
  101. * If this object represents the value <code>true</code>, a string equal
  102. * to <code>"true"</code> is returned. Otherwise, a string equal to
  103. * <code>"false"</code> is returned.
  104. *
  105. * @return a string representation of this object.
  106. */
  107. public String toString() {
  108. return value ? "true" : "false";
  109. }
  110. /**
  111. * Returns a hash code for this <tt>Boolean</tt> object.
  112. *
  113. * @return the integer <tt>1231</tt> if this object represents
  114. * <tt>true</tt> returns the integer <tt>1237</tt> if this
  115. * object represents <tt>false</tt>.
  116. */
  117. public int hashCode() {
  118. return value ? 1231 : 1237;
  119. }
  120. /**
  121. * Returns <code>true</code> if and only if the argument is not
  122. * <code>null</code> and is a <code>Boolean </code>object that
  123. * represents the same <code>boolean</code> value as this object.
  124. *
  125. * @param obj the object to compare with.
  126. * @return <code>true</code> if the Boolean objects represent the
  127. * same value; <code>false</code> otherwise.
  128. */
  129. public boolean equals(Object obj) {
  130. if ((obj != null) && (obj instanceof Boolean)) {
  131. return value == ((Boolean)obj).booleanValue();
  132. }
  133. return false;
  134. }
  135. /**
  136. * Returns <code>true</code> if and only if the system property
  137. * named by the argument exists and is equal to the string
  138. * <code>"true"</code>. (Beginning with version 1.0.2 of the
  139. * Java<font size="-2"><sup>TM</sup></font> platform, the test of
  140. * this string is case insensitive.) A system property is accessible
  141. * through <code>getProperty</code>, a method defined by the
  142. * <code>System</code> class.
  143. *
  144. * @param name the system property name.
  145. * @return the <code>boolean</code> value of the system property.
  146. * @see java.lang.System#getProperty(java.lang.String)
  147. * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  148. */
  149. public static boolean getBoolean(String name) {
  150. return toBoolean(System.getProperty(name));
  151. }
  152. private static boolean toBoolean(String name) {
  153. return ((name != null) && name.toLowerCase().equals("true"));
  154. }
  155. }