home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Boolean.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  125 lines

  1. /*
  2.  * @(#)Boolean.java    1.21 96/02/05  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * The Boolean class provides an object wrapper for Boolean data values, and 
  24.  * serves as a place for boolean-oriented operations.
  25.  * A wrapper is useful because most of Java's utility classes require the use
  26.  * of objects.  Since booleans are not objects in Java, they need to be
  27.  * "wrapped" in a Boolean instance. 
  28.  * @version     1.21, 05 Feb 1996
  29.  * @author    Arthur van Hoff
  30.  */
  31. public final
  32. class Boolean {
  33.     /** 
  34.      *  Assigns this Boolean to be true.
  35.      */
  36.     public static final Boolean TRUE = new Boolean(true);
  37.     /** 
  38.      * Assigns this Boolean to be false.
  39.      */
  40.     public static final Boolean FALSE = new Boolean(false);
  41.  
  42.     /**
  43.      * The value of the Boolean.
  44.      */
  45.     private boolean value;
  46.  
  47.     /**
  48.      * Constructs a Boolean object initialized to the specified boolean 
  49.      * value.
  50.      * @param value the value of the boolean
  51.      */
  52.     public Boolean(boolean value) {
  53.     this.value = value;
  54.     }
  55.  
  56.     /**
  57.      * Constructs a Boolean object initialized to the value specified by the
  58.      * String parameter. 
  59.      * @param s        the String to be converted to a Boolean
  60.      */
  61.     public Boolean(String s) {
  62.     this(toBoolean(s));
  63.     }
  64.  
  65.     /**
  66.      * Returns the value of this Boolean object as a boolean.
  67.      */
  68.     public boolean booleanValue() {
  69.     return value;
  70.     }
  71.  
  72.     /**
  73.      * Returns the boolean value represented by the specified String.
  74.      * @param s        the String to be parsed
  75.      */
  76.     public static Boolean valueOf(String s) {
  77.     return new Boolean(toBoolean(s));
  78.     }
  79.  
  80.     /**
  81.      * Returns a String object representing this Boolean's value.
  82.      */
  83.     public String toString() {
  84.     return value ? "true" : "false";
  85.     }
  86.  
  87.     /**
  88.      * Returns a hashcode for this Boolean.
  89.      */
  90.     public int hashCode() {
  91.     return value ? 1231 : 1237;
  92.     }
  93.  
  94.     /**
  95.      * Compares this object against the specified object.
  96.      * @param obj        the object to compare with
  97.      * @return         true if the objects are the same; false otherwise.
  98.      */
  99.     public boolean equals(Object obj) {
  100.     if ((obj != null) && (obj instanceof Boolean)) {
  101.         return value == ((Boolean)obj).booleanValue();
  102.     } 
  103.     return false;
  104.     }
  105.  
  106.     /**
  107.      * Gets a Boolean from the properties.
  108.      * @param name the property name.
  109.      */
  110.     public static boolean getBoolean(String name) {
  111.     return toBoolean(System.getProperty(name));
  112.     }
  113.  
  114.     private static boolean toBoolean(String name) { 
  115.     return ((name != null) && name.toLowerCase().equals("true"));
  116.     }
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.