emul/mini/src/main/java/java/lang/Boolean.java
branchmodel
changeset 877 3392f250c784
parent 104 1376481f15e7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/java/lang/Boolean.java	Fri Mar 22 16:59:47 2013 +0100
     1.3 @@ -0,0 +1,282 @@
     1.4 +/*
     1.5 + * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +/**
    1.32 + * The Boolean class wraps a value of the primitive type
    1.33 + * {@code boolean} in an object. An object of type
    1.34 + * {@code Boolean} contains a single field whose type is
    1.35 + * {@code boolean}.
    1.36 + * <p>
    1.37 + * In addition, this class provides many methods for
    1.38 + * converting a {@code boolean} to a {@code String} and a
    1.39 + * {@code String} to a {@code boolean}, as well as other
    1.40 + * constants and methods useful when dealing with a
    1.41 + * {@code boolean}.
    1.42 + *
    1.43 + * @author  Arthur van Hoff
    1.44 + * @since   JDK1.0
    1.45 + */
    1.46 +public final class Boolean implements java.io.Serializable,
    1.47 +                                      Comparable<Boolean>
    1.48 +{
    1.49 +    /**
    1.50 +     * The {@code Boolean} object corresponding to the primitive
    1.51 +     * value {@code true}.
    1.52 +     */
    1.53 +    public static final Boolean TRUE = new Boolean(true);
    1.54 +
    1.55 +    /**
    1.56 +     * The {@code Boolean} object corresponding to the primitive
    1.57 +     * value {@code false}.
    1.58 +     */
    1.59 +    public static final Boolean FALSE = new Boolean(false);
    1.60 +
    1.61 +    /**
    1.62 +     * The Class object representing the primitive type boolean.
    1.63 +     *
    1.64 +     * @since   JDK1.1
    1.65 +     */
    1.66 +    public static final Class<Boolean> TYPE = Class.getPrimitiveClass("boolean");
    1.67 +
    1.68 +    /**
    1.69 +     * The value of the Boolean.
    1.70 +     *
    1.71 +     * @serial
    1.72 +     */
    1.73 +    private final boolean value;
    1.74 +
    1.75 +    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    1.76 +    private static final long serialVersionUID = -3665804199014368530L;
    1.77 +
    1.78 +    /**
    1.79 +     * Allocates a {@code Boolean} object representing the
    1.80 +     * {@code value} argument.
    1.81 +     *
    1.82 +     * <p><b>Note: It is rarely appropriate to use this constructor.
    1.83 +     * Unless a <i>new</i> instance is required, the static factory
    1.84 +     * {@link #valueOf(boolean)} is generally a better choice. It is
    1.85 +     * likely to yield significantly better space and time performance.</b>
    1.86 +     *
    1.87 +     * @param   value   the value of the {@code Boolean}.
    1.88 +     */
    1.89 +    public Boolean(boolean value) {
    1.90 +        this.value = value;
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Allocates a {@code Boolean} object representing the value
    1.95 +     * {@code true} if the string argument is not {@code null}
    1.96 +     * and is equal, ignoring case, to the string {@code "true"}.
    1.97 +     * Otherwise, allocate a {@code Boolean} object representing the
    1.98 +     * value {@code false}. Examples:<p>
    1.99 +     * {@code new Boolean("True")} produces a {@code Boolean} object
   1.100 +     * that represents {@code true}.<br>
   1.101 +     * {@code new Boolean("yes")} produces a {@code Boolean} object
   1.102 +     * that represents {@code false}.
   1.103 +     *
   1.104 +     * @param   s   the string to be converted to a {@code Boolean}.
   1.105 +     */
   1.106 +    public Boolean(String s) {
   1.107 +        this(toBoolean(s));
   1.108 +    }
   1.109 +
   1.110 +    /**
   1.111 +     * Parses the string argument as a boolean.  The {@code boolean}
   1.112 +     * returned represents the value {@code true} if the string argument
   1.113 +     * is not {@code null} and is equal, ignoring case, to the string
   1.114 +     * {@code "true"}. <p>
   1.115 +     * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.<br>
   1.116 +     * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
   1.117 +     *
   1.118 +     * @param      s   the {@code String} containing the boolean
   1.119 +     *                 representation to be parsed
   1.120 +     * @return     the boolean represented by the string argument
   1.121 +     * @since 1.5
   1.122 +     */
   1.123 +    public static boolean parseBoolean(String s) {
   1.124 +        return toBoolean(s);
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Returns the value of this {@code Boolean} object as a boolean
   1.129 +     * primitive.
   1.130 +     *
   1.131 +     * @return  the primitive {@code boolean} value of this object.
   1.132 +     */
   1.133 +    public boolean booleanValue() {
   1.134 +        return value;
   1.135 +    }
   1.136 +
   1.137 +    /**
   1.138 +     * Returns a {@code Boolean} instance representing the specified
   1.139 +     * {@code boolean} value.  If the specified {@code boolean} value
   1.140 +     * is {@code true}, this method returns {@code Boolean.TRUE};
   1.141 +     * if it is {@code false}, this method returns {@code Boolean.FALSE}.
   1.142 +     * If a new {@code Boolean} instance is not required, this method
   1.143 +     * should generally be used in preference to the constructor
   1.144 +     * {@link #Boolean(boolean)}, as this method is likely to yield
   1.145 +     * significantly better space and time performance.
   1.146 +     *
   1.147 +     * @param  b a boolean value.
   1.148 +     * @return a {@code Boolean} instance representing {@code b}.
   1.149 +     * @since  1.4
   1.150 +     */
   1.151 +    public static Boolean valueOf(boolean b) {
   1.152 +        return (b ? TRUE : FALSE);
   1.153 +    }
   1.154 +
   1.155 +    /**
   1.156 +     * Returns a {@code Boolean} with a value represented by the
   1.157 +     * specified string.  The {@code Boolean} returned represents a
   1.158 +     * true value if the string argument is not {@code null}
   1.159 +     * and is equal, ignoring case, to the string {@code "true"}.
   1.160 +     *
   1.161 +     * @param   s   a string.
   1.162 +     * @return  the {@code Boolean} value represented by the string.
   1.163 +     */
   1.164 +    public static Boolean valueOf(String s) {
   1.165 +        return toBoolean(s) ? TRUE : FALSE;
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Returns a {@code String} object representing the specified
   1.170 +     * boolean.  If the specified boolean is {@code true}, then
   1.171 +     * the string {@code "true"} will be returned, otherwise the
   1.172 +     * string {@code "false"} will be returned.
   1.173 +     *
   1.174 +     * @param b the boolean to be converted
   1.175 +     * @return the string representation of the specified {@code boolean}
   1.176 +     * @since 1.4
   1.177 +     */
   1.178 +    public static String toString(boolean b) {
   1.179 +        return b ? "true" : "false";
   1.180 +    }
   1.181 +
   1.182 +    /**
   1.183 +     * Returns a {@code String} object representing this Boolean's
   1.184 +     * value.  If this object represents the value {@code true},
   1.185 +     * a string equal to {@code "true"} is returned. Otherwise, a
   1.186 +     * string equal to {@code "false"} is returned.
   1.187 +     *
   1.188 +     * @return  a string representation of this object.
   1.189 +     */
   1.190 +    public String toString() {
   1.191 +        return value ? "true" : "false";
   1.192 +    }
   1.193 +
   1.194 +    /**
   1.195 +     * Returns a hash code for this {@code Boolean} object.
   1.196 +     *
   1.197 +     * @return  the integer {@code 1231} if this object represents
   1.198 +     * {@code true}; returns the integer {@code 1237} if this
   1.199 +     * object represents {@code false}.
   1.200 +     */
   1.201 +    public int hashCode() {
   1.202 +        return value ? 1231 : 1237;
   1.203 +    }
   1.204 +
   1.205 +    /**
   1.206 +     * Returns {@code true} if and only if the argument is not
   1.207 +     * {@code null} and is a {@code Boolean} object that
   1.208 +     * represents the same {@code boolean} value as this object.
   1.209 +     *
   1.210 +     * @param   obj   the object to compare with.
   1.211 +     * @return  {@code true} if the Boolean objects represent the
   1.212 +     *          same value; {@code false} otherwise.
   1.213 +     */
   1.214 +    public boolean equals(Object obj) {
   1.215 +        if (obj instanceof Boolean) {
   1.216 +            return value == ((Boolean)obj).booleanValue();
   1.217 +        }
   1.218 +        return false;
   1.219 +    }
   1.220 +
   1.221 +    /**
   1.222 +     * Returns {@code true} if and only if the system property
   1.223 +     * named by the argument exists and is equal to the string
   1.224 +     * {@code "true"}. (Beginning with version 1.0.2 of the
   1.225 +     * Java<small><sup>TM</sup></small> platform, the test of
   1.226 +     * this string is case insensitive.) A system property is accessible
   1.227 +     * through {@code getProperty}, a method defined by the
   1.228 +     * {@code System} class.
   1.229 +     * <p>
   1.230 +     * If there is no property with the specified name, or if the specified
   1.231 +     * name is empty or null, then {@code false} is returned.
   1.232 +     *
   1.233 +     * @param   name   the system property name.
   1.234 +     * @return  the {@code boolean} value of the system property.
   1.235 +     * @see     java.lang.System#getProperty(java.lang.String)
   1.236 +     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
   1.237 +     */
   1.238 +    public static boolean getBoolean(String name) {
   1.239 +        boolean result = false;
   1.240 +        try {
   1.241 +            result = toBoolean(AbstractStringBuilder.getProperty(name));
   1.242 +        } catch (IllegalArgumentException e) {
   1.243 +        } catch (NullPointerException e) {
   1.244 +        }
   1.245 +        return result;
   1.246 +    }
   1.247 +
   1.248 +    /**
   1.249 +     * Compares this {@code Boolean} instance with another.
   1.250 +     *
   1.251 +     * @param   b the {@code Boolean} instance to be compared
   1.252 +     * @return  zero if this object represents the same boolean value as the
   1.253 +     *          argument; a positive value if this object represents true
   1.254 +     *          and the argument represents false; and a negative value if
   1.255 +     *          this object represents false and the argument represents true
   1.256 +     * @throws  NullPointerException if the argument is {@code null}
   1.257 +     * @see     Comparable
   1.258 +     * @since  1.5
   1.259 +     */
   1.260 +    public int compareTo(Boolean b) {
   1.261 +        return compare(this.value, b.value);
   1.262 +    }
   1.263 +
   1.264 +    /**
   1.265 +     * Compares two {@code boolean} values.
   1.266 +     * The value returned is identical to what would be returned by:
   1.267 +     * <pre>
   1.268 +     *    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
   1.269 +     * </pre>
   1.270 +     *
   1.271 +     * @param  x the first {@code boolean} to compare
   1.272 +     * @param  y the second {@code boolean} to compare
   1.273 +     * @return the value {@code 0} if {@code x == y};
   1.274 +     *         a value less than {@code 0} if {@code !x && y}; and
   1.275 +     *         a value greater than {@code 0} if {@code x && !y}
   1.276 +     * @since 1.7
   1.277 +     */
   1.278 +    public static int compare(boolean x, boolean y) {
   1.279 +        return (x == y) ? 0 : (x ? 1 : -1);
   1.280 +    }
   1.281 +
   1.282 +    private static boolean toBoolean(String name) {
   1.283 +        return ((name != null) && name.equalsIgnoreCase("true"));
   1.284 +    }
   1.285 +}