home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Boolean.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.3 KB  |  166 lines

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