Source for java.lang.Boolean

   1: /* Boolean.java -- object wrapper for boolean
   2:    Copyright (C) 1998, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.lang;
  40: 
  41: import java.io.Serializable;
  42: 
  43: /**
  44:  * Instances of class <code>Boolean</code> represent primitive
  45:  * <code>boolean</code> values.
  46:  *
  47:  * @author Paul Fisher
  48:  * @author Eric Blake (ebb9@email.byu.edu)
  49:  * @since 1.0
  50:  * @status updated to 1.5
  51:  */
  52: public final class Boolean implements Serializable, Comparable<Boolean>
  53: {
  54:   /**
  55:    * Compatible with JDK 1.0.2+.
  56:    */
  57:   private static final long serialVersionUID = -3665804199014368530L;
  58: 
  59:   /**
  60:    * This field is a <code>Boolean</code> object representing the
  61:    * primitive value <code>true</code>. This instance is returned
  62:    * by the static <code>valueOf()</code> methods if they return
  63:    * a <code>Boolean</code> representing <code>true</code>.
  64:    */
  65:   public static final Boolean TRUE = new Boolean(true);
  66: 
  67:   /**
  68:    * This field is a <code>Boolean</code> object representing the
  69:    * primitive value <code>false</code>. This instance is returned
  70:    * by the static <code>valueOf()</code> methods if they return
  71:    * a <code>Boolean</code> representing <code>false</code>.
  72:    */
  73:   public static final Boolean FALSE = new Boolean(false);
  74: 
  75:   /**
  76:    * The primitive type <code>boolean</code> is represented by this
  77:    * <code>Class</code> object.
  78:    *
  79:    * @since 1.1
  80:    */
  81:   public static final Class<Boolean> TYPE = (Class<Boolean>) VMClassLoader.getPrimitiveClass('Z');
  82: 
  83:   /**
  84:    * The immutable value of this Boolean.
  85:    * @serial the wrapped value
  86:    */
  87:   private final boolean value;
  88: 
  89:   /**
  90:    * Create a <code>Boolean</code> object representing the value of the
  91:    * argument <code>value</code>. In general the use of the static
  92:    * method <code>valueof(boolean)</code> is more efficient since it will
  93:    * not create a new object.
  94:    *
  95:    * @param value the primitive value of this <code>Boolean</code>
  96:    * @see #valueOf(boolean)
  97:    */
  98:   public Boolean(boolean value)
  99:   {
 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:    * If the String argument is "true", ignoring case, return true.
 241:    * Otherwise, return false.
 242:    *
 243:    * @param b String to parse
 244:    * @since 1.5
 245:    */
 246:   public static boolean parseBoolean(String b)
 247:   {
 248:     return "true".equalsIgnoreCase(b) ? true : false;
 249:   }
 250:   
 251: }