Boolean.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Boolean.java -- object wrapper for boolean
  2. Copyright (C) 1998, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. import java.io.Serializable;
  33. /**
  34. * Instances of class <code>Boolean</code> represent primitive
  35. * <code>boolean</code> values.
  36. *
  37. * @author Paul Fisher
  38. * @author Eric Blake (ebb9@email.byu.edu)
  39. * @since 1.0
  40. * @status updated to 1.5
  41. */
  42. public final class Boolean implements Serializable, Comparable<Boolean>
  43. {
  44. /**
  45. * Compatible with JDK 1.0.2+.
  46. */
  47. private static final long serialVersionUID = -3665804199014368530L;
  48. /**
  49. * This field is a <code>Boolean</code> object representing the
  50. * primitive value <code>true</code>. This instance is returned
  51. * by the static <code>valueOf()</code> methods if they return
  52. * a <code>Boolean</code> representing <code>true</code>.
  53. */
  54. public static final Boolean TRUE = new Boolean(true);
  55. /**
  56. * This field is a <code>Boolean</code> object representing the
  57. * primitive value <code>false</code>. This instance is returned
  58. * by the static <code>valueOf()</code> methods if they return
  59. * a <code>Boolean</code> representing <code>false</code>.
  60. */
  61. public static final Boolean FALSE = new Boolean(false);
  62. /**
  63. * The primitive type <code>boolean</code> is represented by this
  64. * <code>Class</code> object.
  65. *
  66. * @since 1.1
  67. */
  68. public static final Class<Boolean> TYPE = (Class<Boolean>) VMClassLoader.getPrimitiveClass('Z');
  69. /**
  70. * The immutable value of this Boolean.
  71. * @serial the wrapped value
  72. */
  73. private final boolean value;
  74. /**
  75. * Create a <code>Boolean</code> object representing the value of the
  76. * argument <code>value</code>. In general the use of the static
  77. * method <code>valueof(boolean)</code> is more efficient since it will
  78. * not create a new object.
  79. *
  80. * @param value the primitive value of this <code>Boolean</code>
  81. * @see #valueOf(boolean)
  82. */
  83. public Boolean(boolean value)
  84. {
  85. this.value = value;
  86. }
  87. /**
  88. * Creates a <code>Boolean</code> object representing the primitive
  89. * <code>true</code> if and only if <code>s</code> matches
  90. * the string "true" ignoring case, otherwise the object will represent
  91. * the primitive <code>false</code>. In general the use of the static
  92. * method <code>valueof(String)</code> is more efficient since it will
  93. * not create a new object.
  94. *
  95. * @param s the <code>String</code> representation of <code>true</code>
  96. * or false
  97. */
  98. public Boolean(String s)
  99. {
  100. value = "true".equalsIgnoreCase(s);
  101. }
  102. /**
  103. * Return the primitive <code>boolean</code> value of this
  104. * <code>Boolean</code> object.
  105. *
  106. * @return true or false, depending on the value of this Boolean
  107. */
  108. public boolean booleanValue()
  109. {
  110. return value;
  111. }
  112. /**
  113. * Returns the Boolean <code>TRUE</code> if the given boolean is
  114. * <code>true</code>, otherwise it will return the Boolean
  115. * <code>FALSE</code>.
  116. *
  117. * @param b the boolean to wrap
  118. * @return the wrapper object
  119. * @see #TRUE
  120. * @see #FALSE
  121. * @since 1.4
  122. */
  123. public static Boolean valueOf(boolean b)
  124. {
  125. return b ? TRUE : FALSE;
  126. }
  127. /**
  128. * Returns the Boolean <code>TRUE</code> if and only if the given
  129. * String is equal, ignoring case, to the the String "true", otherwise
  130. * it will return the Boolean <code>FALSE</code>.
  131. *
  132. * @param s the string to convert
  133. * @return a wrapped boolean from the string
  134. */
  135. public static Boolean valueOf(String s)
  136. {
  137. return "true".equalsIgnoreCase(s) ? TRUE : FALSE;
  138. }
  139. /**
  140. * Returns "true" if the value of the give boolean is <code>true</code> and
  141. * returns "false" if the value of the given boolean is <code>false</code>.
  142. *
  143. * @param b the boolean to convert
  144. * @return the string representation of the boolean
  145. * @since 1.4
  146. */
  147. public static String toString(boolean b)
  148. {
  149. return b ? "true" : "false";
  150. }
  151. /**
  152. * Returns "true" if the value of this object is <code>true</code> and
  153. * returns "false" if the value of this object is <code>false</code>.
  154. *
  155. * @return the string representation of this
  156. */
  157. public String toString()
  158. {
  159. return value ? "true" : "false";
  160. }
  161. /**
  162. * Returns the integer <code>1231</code> if this object represents
  163. * the primitive <code>true</code> and the integer <code>1237</code>
  164. * otherwise.
  165. *
  166. * @return the hash code
  167. */
  168. public int hashCode()
  169. {
  170. return value ? 1231 : 1237;
  171. }
  172. /**
  173. * If the <code>obj</code> is an instance of <code>Boolean</code> and
  174. * has the same primitive value as this object then <code>true</code>
  175. * is returned. In all other cases, including if the <code>obj</code>
  176. * is <code>null</code>, <code>false</code> is returned.
  177. *
  178. * @param obj possibly an instance of any <code>Class</code>
  179. * @return true if <code>obj</code> equals this
  180. */
  181. public boolean equals(Object obj)
  182. {
  183. return obj instanceof Boolean && value == ((Boolean) obj).value;
  184. }
  185. /**
  186. * If the value of the system property <code>name</code> matches
  187. * "true" ignoring case then the function returns <code>true</code>.
  188. *
  189. * @param name the property name to look up
  190. * @return true if the property resulted in "true"
  191. * @throws SecurityException if accessing the system property is forbidden
  192. * @see System#getProperty(String)
  193. */
  194. public static boolean getBoolean(String name)
  195. {
  196. if (name == null || "".equals(name))
  197. return false;
  198. return "true".equalsIgnoreCase(System.getProperty(name));
  199. }
  200. /**
  201. * Compares this Boolean to another.
  202. *
  203. * @param other the Boolean to compare this Boolean to
  204. * @return 0 if both Booleans represent the same value, a positive number
  205. * if this Boolean represents true and the other false, and a negative
  206. * number otherwise.
  207. * @since 1.5
  208. */
  209. public int compareTo(Boolean other)
  210. {
  211. return value == other.value ? 0 : (value ? 1 : -1);
  212. }
  213. /**
  214. * Compares two unboxed boolean values.
  215. *
  216. * @param x First value to compare.
  217. * @param y Second value to compare.
  218. * @return 0 if both Booleans represent the same value, a positive number
  219. * if this Boolean represents true and the other false, and a negative
  220. * number otherwise.
  221. * @since 1.7
  222. */
  223. public static int compare(boolean x, boolean y)
  224. {
  225. return Boolean.valueOf(x).compareTo(Boolean.valueOf(y));
  226. }
  227. /**
  228. * If the String argument is "true", ignoring case, return true.
  229. * Otherwise, return false.
  230. *
  231. * @param b String to parse
  232. * @since 1.5
  233. */
  234. public static boolean parseBoolean(String b)
  235. {
  236. return "true".equalsIgnoreCase(b) ? true : false;
  237. }
  238. }