home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Boolean.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  186 lines

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