001/* Boolean.java -- object wrapper for boolean 002 Copyright (C) 1998, 2001, 2002, 2004, 2005 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.lang; 040 041import java.io.Serializable; 042 043/** 044 * Instances of class <code>Boolean</code> represent primitive 045 * <code>boolean</code> values. 046 * 047 * @author Paul Fisher 048 * @author Eric Blake (ebb9@email.byu.edu) 049 * @since 1.0 050 * @status updated to 1.5 051 */ 052public final class Boolean implements Serializable, Comparable<Boolean> 053{ 054 /** 055 * Compatible with JDK 1.0.2+. 056 */ 057 private static final long serialVersionUID = -3665804199014368530L; 058 059 /** 060 * This field is a <code>Boolean</code> object representing the 061 * primitive value <code>true</code>. This instance is returned 062 * by the static <code>valueOf()</code> methods if they return 063 * a <code>Boolean</code> representing <code>true</code>. 064 */ 065 public static final Boolean TRUE = new Boolean(true); 066 067 /** 068 * This field is a <code>Boolean</code> object representing the 069 * primitive value <code>false</code>. This instance is returned 070 * by the static <code>valueOf()</code> methods if they return 071 * a <code>Boolean</code> representing <code>false</code>. 072 */ 073 public static final Boolean FALSE = new Boolean(false); 074 075 /** 076 * The primitive type <code>boolean</code> is represented by this 077 * <code>Class</code> object. 078 * 079 * @since 1.1 080 */ 081 public static final Class<Boolean> TYPE = (Class<Boolean>) VMClassLoader.getPrimitiveClass('Z'); 082 083 /** 084 * The immutable value of this Boolean. 085 * @serial the wrapped value 086 */ 087 private final boolean value; 088 089 /** 090 * Create a <code>Boolean</code> object representing the value of the 091 * argument <code>value</code>. In general the use of the static 092 * method <code>valueof(boolean)</code> is more efficient since it will 093 * not create a new object. 094 * 095 * @param value the primitive value of this <code>Boolean</code> 096 * @see #valueOf(boolean) 097 */ 098 public Boolean(boolean value) 099 { 100 this.value = value; 101 } 102 103 /** 104 * Creates a <code>Boolean</code> object representing the primitive 105 * <code>true</code> if and only if <code>s</code> matches 106 * the string "true" ignoring case, otherwise the object will represent 107 * the primitive <code>false</code>. In general the use of the static 108 * method <code>valueof(String)</code> is more efficient since it will 109 * not create a new object. 110 * 111 * @param s the <code>String</code> representation of <code>true</code> 112 * or false 113 */ 114 public Boolean(String s) 115 { 116 value = "true".equalsIgnoreCase(s); 117 } 118 119 /** 120 * Return the primitive <code>boolean</code> value of this 121 * <code>Boolean</code> object. 122 * 123 * @return true or false, depending on the value of this Boolean 124 */ 125 public boolean booleanValue() 126 { 127 return value; 128 } 129 130 /** 131 * Returns the Boolean <code>TRUE</code> if the given boolean is 132 * <code>true</code>, otherwise it will return the Boolean 133 * <code>FALSE</code>. 134 * 135 * @param b the boolean to wrap 136 * @return the wrapper object 137 * @see #TRUE 138 * @see #FALSE 139 * @since 1.4 140 */ 141 public static Boolean valueOf(boolean b) 142 { 143 return b ? TRUE : FALSE; 144 } 145 146 /** 147 * Returns the Boolean <code>TRUE</code> if and only if the given 148 * String is equal, ignoring case, to the the String "true", otherwise 149 * it will return the Boolean <code>FALSE</code>. 150 * 151 * @param s the string to convert 152 * @return a wrapped boolean from the string 153 */ 154 public static Boolean valueOf(String s) 155 { 156 return "true".equalsIgnoreCase(s) ? TRUE : FALSE; 157 } 158 159 /** 160 * Returns "true" if the value of the give boolean is <code>true</code> and 161 * returns "false" if the value of the given boolean is <code>false</code>. 162 * 163 * @param b the boolean to convert 164 * @return the string representation of the boolean 165 * @since 1.4 166 */ 167 public static String toString(boolean b) 168 { 169 return b ? "true" : "false"; 170 } 171 172 /** 173 * Returns "true" if the value of this object is <code>true</code> and 174 * returns "false" if the value of this object is <code>false</code>. 175 * 176 * @return the string representation of this 177 */ 178 public String toString() 179 { 180 return value ? "true" : "false"; 181 } 182 183 /** 184 * Returns the integer <code>1231</code> if this object represents 185 * the primitive <code>true</code> and the integer <code>1237</code> 186 * otherwise. 187 * 188 * @return the hash code 189 */ 190 public int hashCode() 191 { 192 return value ? 1231 : 1237; 193 } 194 195 /** 196 * If the <code>obj</code> is an instance of <code>Boolean</code> and 197 * has the same primitive value as this object then <code>true</code> 198 * is returned. In all other cases, including if the <code>obj</code> 199 * is <code>null</code>, <code>false</code> is returned. 200 * 201 * @param obj possibly an instance of any <code>Class</code> 202 * @return true if <code>obj</code> equals this 203 */ 204 public boolean equals(Object obj) 205 { 206 return obj instanceof Boolean && value == ((Boolean) obj).value; 207 } 208 209 /** 210 * If the value of the system property <code>name</code> matches 211 * "true" ignoring case then the function returns <code>true</code>. 212 * 213 * @param name the property name to look up 214 * @return true if the property resulted in "true" 215 * @throws SecurityException if accessing the system property is forbidden 216 * @see System#getProperty(String) 217 */ 218 public static boolean getBoolean(String name) 219 { 220 if (name == null || "".equals(name)) 221 return false; 222 return "true".equalsIgnoreCase(System.getProperty(name)); 223 } 224 225 /** 226 * Compares this Boolean to another. 227 * 228 * @param other the Boolean to compare this Boolean to 229 * @return 0 if both Booleans represent the same value, a positive number 230 * if this Boolean represents true and the other false, and a negative 231 * number otherwise. 232 * @since 1.5 233 */ 234 public int compareTo(Boolean other) 235 { 236 return value == other.value ? 0 : (value ? 1 : -1); 237 } 238 239 /** 240 * Compares two unboxed boolean values. 241 * 242 * @param x First value to compare. 243 * @param y Second value to compare. 244 * @return 0 if both Booleans represent the same value, a positive number 245 * if this Boolean represents true and the other false, and a negative 246 * number otherwise. 247 * @since 1.7 248 */ 249 public static int compare(boolean x, boolean y) 250 { 251 return Boolean.valueOf(x).compareTo(Boolean.valueOf(y)); 252 } 253 254 /** 255 * If the String argument is "true", ignoring case, return true. 256 * Otherwise, return false. 257 * 258 * @param b String to parse 259 * @since 1.5 260 */ 261 public static boolean parseBoolean(String b) 262 { 263 return "true".equalsIgnoreCase(b) ? true : false; 264 } 265 266}