jdk/src/share/classes/java/lang/Boolean.java
changeset 10602 ab8c1e3b237b
parent 5506 202f599c92aa
child 11275 7cb0861d512f
equal deleted inserted replaced
10601:c496d54879f4 10602:ab8c1e3b237b
     1 /*
     1 /*
     2  * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    99      * that represents {@code false}.
    99      * that represents {@code false}.
   100      *
   100      *
   101      * @param   s   the string to be converted to a {@code Boolean}.
   101      * @param   s   the string to be converted to a {@code Boolean}.
   102      */
   102      */
   103     public Boolean(String s) {
   103     public Boolean(String s) {
   104         this(toBoolean(s));
   104         this(parseBoolean(s));
   105     }
   105     }
   106 
   106 
   107     /**
   107     /**
   108      * Parses the string argument as a boolean.  The {@code boolean}
   108      * Parses the string argument as a boolean.  The {@code boolean}
   109      * returned represents the value {@code true} if the string argument
   109      * returned represents the value {@code true} if the string argument
   116      *                 representation to be parsed
   116      *                 representation to be parsed
   117      * @return     the boolean represented by the string argument
   117      * @return     the boolean represented by the string argument
   118      * @since 1.5
   118      * @since 1.5
   119      */
   119      */
   120     public static boolean parseBoolean(String s) {
   120     public static boolean parseBoolean(String s) {
   121         return toBoolean(s);
   121         return ((s != null) && s.equalsIgnoreCase("true"));
   122     }
   122     }
   123 
   123 
   124     /**
   124     /**
   125      * Returns the value of this {@code Boolean} object as a boolean
   125      * Returns the value of this {@code Boolean} object as a boolean
   126      * primitive.
   126      * primitive.
   157      *
   157      *
   158      * @param   s   a string.
   158      * @param   s   a string.
   159      * @return  the {@code Boolean} value represented by the string.
   159      * @return  the {@code Boolean} value represented by the string.
   160      */
   160      */
   161     public static Boolean valueOf(String s) {
   161     public static Boolean valueOf(String s) {
   162         return toBoolean(s) ? TRUE : FALSE;
   162         return parseBoolean(s) ? TRUE : FALSE;
   163     }
   163     }
   164 
   164 
   165     /**
   165     /**
   166      * Returns a {@code String} object representing the specified
   166      * Returns a {@code String} object representing the specified
   167      * boolean.  If the specified boolean is {@code true}, then
   167      * boolean.  If the specified boolean is {@code true}, then
   227      * If there is no property with the specified name, or if the specified
   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.
   228      * name is empty or null, then {@code false} is returned.
   229      *
   229      *
   230      * @param   name   the system property name.
   230      * @param   name   the system property name.
   231      * @return  the {@code boolean} value of the system property.
   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}
   232      * @see     java.lang.System#getProperty(java.lang.String)
   234      * @see     java.lang.System#getProperty(java.lang.String)
   233      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
   235      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
   234      */
   236      */
   235     public static boolean getBoolean(String name) {
   237     public static boolean getBoolean(String name) {
   236         boolean result = false;
   238         boolean result = false;
   237         try {
   239         try {
   238             result = toBoolean(System.getProperty(name));
   240             result = parseBoolean(System.getProperty(name));
   239         } catch (IllegalArgumentException e) {
   241         } catch (IllegalArgumentException | NullPointerException e) {
   240         } catch (NullPointerException e) {
       
   241         }
   242         }
   242         return result;
   243         return result;
   243     }
   244     }
   244 
   245 
   245     /**
   246     /**
   273      * @since 1.7
   274      * @since 1.7
   274      */
   275      */
   275     public static int compare(boolean x, boolean y) {
   276     public static int compare(boolean x, boolean y) {
   276         return (x == y) ? 0 : (x ? 1 : -1);
   277         return (x == y) ? 0 : (x ? 1 : -1);
   277     }
   278     }
   278 
       
   279     private static boolean toBoolean(String name) {
       
   280         return ((name != null) && name.equalsIgnoreCase("true"));
       
   281     }
       
   282 }
   279 }