src/share/classes/java/lang/Boolean.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  84      * @param   value   the value of the {@code Boolean}.
  85      */
  86     public Boolean(boolean value) {
  87         this.value = value;
  88     }
  89 
  90     /**
  91      * Allocates a {@code Boolean} object representing the value
  92      * {@code true} if the string argument is not {@code null}
  93      * and is equal, ignoring case, to the string {@code "true"}.
  94      * Otherwise, allocate a {@code Boolean} object representing the
  95      * value {@code false}. Examples:<p>
  96      * {@code new Boolean("True")} produces a {@code Boolean} object
  97      * that represents {@code true}.<br>
  98      * {@code new Boolean("yes")} produces a {@code Boolean} object
  99      * that represents {@code false}.
 100      *
 101      * @param   s   the string to be converted to a {@code Boolean}.
 102      */
 103     public Boolean(String s) {
 104         this(toBoolean(s));
 105     }
 106 
 107     /**
 108      * Parses the string argument as a boolean.  The {@code boolean}
 109      * returned represents the value {@code true} if the string argument
 110      * is not {@code null} and is equal, ignoring case, to the string
 111      * {@code "true"}. <p>
 112      * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
 113      * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
 114      *
 115      * @param      s   the {@code String} containing the boolean
 116      *                 representation to be parsed
 117      * @return     the boolean represented by the string argument
 118      * @since 1.5
 119      */
 120     public static boolean parseBoolean(String s) {
 121         return toBoolean(s);
 122     }
 123 
 124     /**
 125      * Returns the value of this {@code Boolean} object as a boolean
 126      * primitive.
 127      *
 128      * @return  the primitive {@code boolean} value of this object.
 129      */
 130     public boolean booleanValue() {
 131         return value;
 132     }
 133 
 134     /**
 135      * Returns a {@code Boolean} instance representing the specified
 136      * {@code boolean} value.  If the specified {@code boolean} value
 137      * is {@code true}, this method returns {@code Boolean.TRUE};
 138      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
 139      * If a new {@code Boolean} instance is not required, this method
 140      * should generally be used in preference to the constructor
 141      * {@link #Boolean(boolean)}, as this method is likely to yield
 142      * significantly better space and time performance.
 143      *
 144      * @param  b a boolean value.
 145      * @return a {@code Boolean} instance representing {@code b}.
 146      * @since  1.4
 147      */
 148     public static Boolean valueOf(boolean b) {
 149         return (b ? TRUE : FALSE);
 150     }
 151 
 152     /**
 153      * Returns a {@code Boolean} with a value represented by the
 154      * specified string.  The {@code Boolean} returned represents a
 155      * true value if the string argument is not {@code null}
 156      * and is equal, ignoring case, to the string {@code "true"}.
 157      *
 158      * @param   s   a string.
 159      * @return  the {@code Boolean} value represented by the string.
 160      */
 161     public static Boolean valueOf(String s) {
 162         return toBoolean(s) ? TRUE : FALSE;
 163     }
 164 
 165     /**
 166      * Returns a {@code String} object representing the specified
 167      * boolean.  If the specified boolean is {@code true}, then
 168      * the string {@code "true"} will be returned, otherwise the
 169      * string {@code "false"} will be returned.
 170      *
 171      * @param b the boolean to be converted
 172      * @return the string representation of the specified {@code boolean}
 173      * @since 1.4
 174      */
 175     public static String toString(boolean b) {
 176         return b ? "true" : "false";
 177     }
 178 
 179     /**
 180      * Returns a {@code String} object representing this Boolean's
 181      * value.  If this object represents the value {@code true},
 182      * a string equal to {@code "true"} is returned. Otherwise, a


 212         if (obj instanceof Boolean) {
 213             return value == ((Boolean)obj).booleanValue();
 214         }
 215         return false;
 216     }
 217 
 218     /**
 219      * Returns {@code true} if and only if the system property
 220      * named by the argument exists and is equal to the string
 221      * {@code "true"}. (Beginning with version 1.0.2 of the
 222      * Java<small><sup>TM</sup></small> platform, the test of
 223      * this string is case insensitive.) A system property is accessible
 224      * through {@code getProperty}, a method defined by the
 225      * {@code System} class.
 226      * <p>
 227      * If there is no property with the specified name, or if the specified
 228      * name is empty or null, then {@code false} is returned.
 229      *
 230      * @param   name   the system property name.
 231      * @return  the {@code boolean} value of the system property.


 232      * @see     java.lang.System#getProperty(java.lang.String)
 233      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 234      */
 235     public static boolean getBoolean(String name) {
 236         boolean result = false;
 237         try {
 238             result = toBoolean(System.getProperty(name));
 239         } catch (IllegalArgumentException e) {
 240         } catch (NullPointerException e) {
 241         }
 242         return result;
 243     }
 244 
 245     /**
 246      * Compares this {@code Boolean} instance with another.
 247      *
 248      * @param   b the {@code Boolean} instance to be compared
 249      * @return  zero if this object represents the same boolean value as the
 250      *          argument; a positive value if this object represents true
 251      *          and the argument represents false; and a negative value if
 252      *          this object represents false and the argument represents true
 253      * @throws  NullPointerException if the argument is {@code null}
 254      * @see     Comparable
 255      * @since  1.5
 256      */
 257     public int compareTo(Boolean b) {
 258         return compare(this.value, b.value);
 259     }
 260 
 261     /**
 262      * Compares two {@code boolean} values.
 263      * The value returned is identical to what would be returned by:
 264      * <pre>
 265      *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
 266      * </pre>
 267      *
 268      * @param  x the first {@code boolean} to compare
 269      * @param  y the second {@code boolean} to compare
 270      * @return the value {@code 0} if {@code x == y};
 271      *         a value less than {@code 0} if {@code !x && y}; and
 272      *         a value greater than {@code 0} if {@code x && !y}
 273      * @since 1.7
 274      */
 275     public static int compare(boolean x, boolean y) {
 276         return (x == y) ? 0 : (x ? 1 : -1);
 277     }
 278 
 279     private static boolean toBoolean(String name) {
 280         return ((name != null) && name.equalsIgnoreCase("true"));
 281     }
 282 }
   1 /*
   2  * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  84      * @param   value   the value of the {@code Boolean}.
  85      */
  86     public Boolean(boolean value) {
  87         this.value = value;
  88     }
  89 
  90     /**
  91      * Allocates a {@code Boolean} object representing the value
  92      * {@code true} if the string argument is not {@code null}
  93      * and is equal, ignoring case, to the string {@code "true"}.
  94      * Otherwise, allocate a {@code Boolean} object representing the
  95      * value {@code false}. Examples:<p>
  96      * {@code new Boolean("True")} produces a {@code Boolean} object
  97      * that represents {@code true}.<br>
  98      * {@code new Boolean("yes")} produces a {@code Boolean} object
  99      * that represents {@code false}.
 100      *
 101      * @param   s   the string to be converted to a {@code Boolean}.
 102      */
 103     public Boolean(String s) {
 104         this(parseBoolean(s));
 105     }
 106 
 107     /**
 108      * Parses the string argument as a boolean.  The {@code boolean}
 109      * returned represents the value {@code true} if the string argument
 110      * is not {@code null} and is equal, ignoring case, to the string
 111      * {@code "true"}. <p>
 112      * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
 113      * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
 114      *
 115      * @param      s   the {@code String} containing the boolean
 116      *                 representation to be parsed
 117      * @return     the boolean represented by the string argument
 118      * @since 1.5
 119      */
 120     public static boolean parseBoolean(String s) {
 121         return ((s != null) && s.equalsIgnoreCase("true"));
 122     }
 123 
 124     /**
 125      * Returns the value of this {@code Boolean} object as a boolean
 126      * primitive.
 127      *
 128      * @return  the primitive {@code boolean} value of this object.
 129      */
 130     public boolean booleanValue() {
 131         return value;
 132     }
 133 
 134     /**
 135      * Returns a {@code Boolean} instance representing the specified
 136      * {@code boolean} value.  If the specified {@code boolean} value
 137      * is {@code true}, this method returns {@code Boolean.TRUE};
 138      * if it is {@code false}, this method returns {@code Boolean.FALSE}.
 139      * If a new {@code Boolean} instance is not required, this method
 140      * should generally be used in preference to the constructor
 141      * {@link #Boolean(boolean)}, as this method is likely to yield
 142      * significantly better space and time performance.
 143      *
 144      * @param  b a boolean value.
 145      * @return a {@code Boolean} instance representing {@code b}.
 146      * @since  1.4
 147      */
 148     public static Boolean valueOf(boolean b) {
 149         return (b ? TRUE : FALSE);
 150     }
 151 
 152     /**
 153      * Returns a {@code Boolean} with a value represented by the
 154      * specified string.  The {@code Boolean} returned represents a
 155      * true value if the string argument is not {@code null}
 156      * and is equal, ignoring case, to the string {@code "true"}.
 157      *
 158      * @param   s   a string.
 159      * @return  the {@code Boolean} value represented by the string.
 160      */
 161     public static Boolean valueOf(String s) {
 162         return parseBoolean(s) ? TRUE : FALSE;
 163     }
 164 
 165     /**
 166      * Returns a {@code String} object representing the specified
 167      * boolean.  If the specified boolean is {@code true}, then
 168      * the string {@code "true"} will be returned, otherwise the
 169      * string {@code "false"} will be returned.
 170      *
 171      * @param b the boolean to be converted
 172      * @return the string representation of the specified {@code boolean}
 173      * @since 1.4
 174      */
 175     public static String toString(boolean b) {
 176         return b ? "true" : "false";
 177     }
 178 
 179     /**
 180      * Returns a {@code String} object representing this Boolean's
 181      * value.  If this object represents the value {@code true},
 182      * a string equal to {@code "true"} is returned. Otherwise, a


 212         if (obj instanceof Boolean) {
 213             return value == ((Boolean)obj).booleanValue();
 214         }
 215         return false;
 216     }
 217 
 218     /**
 219      * Returns {@code true} if and only if the system property
 220      * named by the argument exists and is equal to the string
 221      * {@code "true"}. (Beginning with version 1.0.2 of the
 222      * Java<small><sup>TM</sup></small> platform, the test of
 223      * this string is case insensitive.) A system property is accessible
 224      * through {@code getProperty}, a method defined by the
 225      * {@code System} class.
 226      * <p>
 227      * If there is no property with the specified name, or if the specified
 228      * name is empty or null, then {@code false} is returned.
 229      *
 230      * @param   name   the system property name.
 231      * @return  the {@code boolean} value of the system property.
 232      * @throws  SecurityException for the same reasons as
 233      *          {@link System#getProperty(String) System.getProperty}
 234      * @see     java.lang.System#getProperty(java.lang.String)
 235      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 236      */
 237     public static boolean getBoolean(String name) {
 238         boolean result = false;
 239         try {
 240             result = parseBoolean(System.getProperty(name));
 241         } catch (IllegalArgumentException | NullPointerException e) {

 242         }
 243         return result;
 244     }
 245 
 246     /**
 247      * Compares this {@code Boolean} instance with another.
 248      *
 249      * @param   b the {@code Boolean} instance to be compared
 250      * @return  zero if this object represents the same boolean value as the
 251      *          argument; a positive value if this object represents true
 252      *          and the argument represents false; and a negative value if
 253      *          this object represents false and the argument represents true
 254      * @throws  NullPointerException if the argument is {@code null}
 255      * @see     Comparable
 256      * @since  1.5
 257      */
 258     public int compareTo(Boolean b) {
 259         return compare(this.value, b.value);
 260     }
 261 
 262     /**
 263      * Compares two {@code boolean} values.
 264      * The value returned is identical to what would be returned by:
 265      * <pre>
 266      *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
 267      * </pre>
 268      *
 269      * @param  x the first {@code boolean} to compare
 270      * @param  y the second {@code boolean} to compare
 271      * @return the value {@code 0} if {@code x == y};
 272      *         a value less than {@code 0} if {@code !x && y}; and
 273      *         a value greater than {@code 0} if {@code x && !y}
 274      * @since 1.7
 275      */
 276     public static int compare(boolean x, boolean y) {
 277         return (x == y) ? 0 : (x ? 1 : -1);
 278     }




 279 }