jaroslav@86: /* jaroslav@86: * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@86: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@86: * jaroslav@86: * This code is free software; you can redistribute it and/or modify it jaroslav@86: * under the terms of the GNU General Public License version 2 only, as jaroslav@86: * published by the Free Software Foundation. Oracle designates this jaroslav@86: * particular file as subject to the "Classpath" exception as provided jaroslav@86: * by Oracle in the LICENSE file that accompanied this code. jaroslav@86: * jaroslav@86: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@86: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@86: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@86: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@86: * accompanied this code). jaroslav@86: * jaroslav@86: * You should have received a copy of the GNU General Public License version jaroslav@86: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@86: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@86: * jaroslav@86: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@86: * or visit www.oracle.com if you need additional information or have any jaroslav@86: * questions. jaroslav@86: */ jaroslav@86: jaroslav@86: package java.lang; jaroslav@86: jaroslav@86: /** jaroslav@86: * The Boolean class wraps a value of the primitive type jaroslav@86: * {@code boolean} in an object. An object of type jaroslav@86: * {@code Boolean} contains a single field whose type is jaroslav@86: * {@code boolean}. jaroslav@86: *
jaroslav@86: * In addition, this class provides many methods for
jaroslav@86: * converting a {@code boolean} to a {@code String} and a
jaroslav@86: * {@code String} to a {@code boolean}, as well as other
jaroslav@86: * constants and methods useful when dealing with a
jaroslav@86: * {@code boolean}.
jaroslav@86: *
jaroslav@86: * @author Arthur van Hoff
jaroslav@86: * @since JDK1.0
jaroslav@86: */
jaroslav@86: public final class Boolean implements java.io.Serializable,
jaroslav@86: Comparable Note: It is rarely appropriate to use this constructor.
jaroslav@86: * Unless a new instance is required, the static factory
jaroslav@86: * {@link #valueOf(boolean)} is generally a better choice. It is
jaroslav@86: * likely to yield significantly better space and time performance.
jaroslav@86: *
jaroslav@86: * @param value the value of the {@code Boolean}.
jaroslav@86: */
jaroslav@86: public Boolean(boolean value) {
jaroslav@86: this.value = value;
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Allocates a {@code Boolean} object representing the value
jaroslav@86: * {@code true} if the string argument is not {@code null}
jaroslav@86: * and is equal, ignoring case, to the string {@code "true"}.
jaroslav@86: * Otherwise, allocate a {@code Boolean} object representing the
jaroslav@86: * value {@code false}. Examples:
jaroslav@86: * {@code new Boolean("True")} produces a {@code Boolean} object
jaroslav@86: * that represents {@code true}.
jaroslav@86: * Example: {@code Boolean.parseBoolean("True")} returns {@code true}.
jaroslav@86: * If there is no property with the specified name, or if the specified
jaroslav@86: * name is empty or null, then {@code false} is returned.
jaroslav@86: *
jaroslav@86: * @param name the system property name.
jaroslav@86: * @return the {@code boolean} value of the system property.
jaroslav@86: * @see java.lang.System#getProperty(java.lang.String)
jaroslav@86: * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@86: */
jaroslav@86: public static boolean getBoolean(String name) {
jaroslav@86: boolean result = false;
jaroslav@86: try {
jaroslav@104: result = toBoolean(AbstractStringBuilder.getProperty(name));
jaroslav@86: } catch (IllegalArgumentException e) {
jaroslav@86: } catch (NullPointerException e) {
jaroslav@86: }
jaroslav@86: return result;
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Compares this {@code Boolean} instance with another.
jaroslav@86: *
jaroslav@86: * @param b the {@code Boolean} instance to be compared
jaroslav@86: * @return zero if this object represents the same boolean value as the
jaroslav@86: * argument; a positive value if this object represents true
jaroslav@86: * and the argument represents false; and a negative value if
jaroslav@86: * this object represents false and the argument represents true
jaroslav@86: * @throws NullPointerException if the argument is {@code null}
jaroslav@86: * @see Comparable
jaroslav@86: * @since 1.5
jaroslav@86: */
jaroslav@86: public int compareTo(Boolean b) {
jaroslav@86: return compare(this.value, b.value);
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Compares two {@code boolean} values.
jaroslav@86: * The value returned is identical to what would be returned by:
jaroslav@86: *
jaroslav@86: * {@code new Boolean("yes")} produces a {@code Boolean} object
jaroslav@86: * that represents {@code false}.
jaroslav@86: *
jaroslav@86: * @param s the string to be converted to a {@code Boolean}.
jaroslav@86: */
jaroslav@86: public Boolean(String s) {
jaroslav@86: this(toBoolean(s));
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Parses the string argument as a boolean. The {@code boolean}
jaroslav@86: * returned represents the value {@code true} if the string argument
jaroslav@86: * is not {@code null} and is equal, ignoring case, to the string
jaroslav@86: * {@code "true"}.
jaroslav@86: * Example: {@code Boolean.parseBoolean("yes")} returns {@code false}.
jaroslav@86: *
jaroslav@86: * @param s the {@code String} containing the boolean
jaroslav@86: * representation to be parsed
jaroslav@86: * @return the boolean represented by the string argument
jaroslav@86: * @since 1.5
jaroslav@86: */
jaroslav@86: public static boolean parseBoolean(String s) {
jaroslav@86: return toBoolean(s);
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns the value of this {@code Boolean} object as a boolean
jaroslav@86: * primitive.
jaroslav@86: *
jaroslav@86: * @return the primitive {@code boolean} value of this object.
jaroslav@86: */
jaroslav@86: public boolean booleanValue() {
jaroslav@86: return value;
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns a {@code Boolean} instance representing the specified
jaroslav@86: * {@code boolean} value. If the specified {@code boolean} value
jaroslav@86: * is {@code true}, this method returns {@code Boolean.TRUE};
jaroslav@86: * if it is {@code false}, this method returns {@code Boolean.FALSE}.
jaroslav@86: * If a new {@code Boolean} instance is not required, this method
jaroslav@86: * should generally be used in preference to the constructor
jaroslav@86: * {@link #Boolean(boolean)}, as this method is likely to yield
jaroslav@86: * significantly better space and time performance.
jaroslav@86: *
jaroslav@86: * @param b a boolean value.
jaroslav@86: * @return a {@code Boolean} instance representing {@code b}.
jaroslav@86: * @since 1.4
jaroslav@86: */
jaroslav@86: public static Boolean valueOf(boolean b) {
jaroslav@86: return (b ? TRUE : FALSE);
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns a {@code Boolean} with a value represented by the
jaroslav@86: * specified string. The {@code Boolean} returned represents a
jaroslav@86: * true value if the string argument is not {@code null}
jaroslav@86: * and is equal, ignoring case, to the string {@code "true"}.
jaroslav@86: *
jaroslav@86: * @param s a string.
jaroslav@86: * @return the {@code Boolean} value represented by the string.
jaroslav@86: */
jaroslav@86: public static Boolean valueOf(String s) {
jaroslav@86: return toBoolean(s) ? TRUE : FALSE;
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns a {@code String} object representing the specified
jaroslav@86: * boolean. If the specified boolean is {@code true}, then
jaroslav@86: * the string {@code "true"} will be returned, otherwise the
jaroslav@86: * string {@code "false"} will be returned.
jaroslav@86: *
jaroslav@86: * @param b the boolean to be converted
jaroslav@86: * @return the string representation of the specified {@code boolean}
jaroslav@86: * @since 1.4
jaroslav@86: */
jaroslav@86: public static String toString(boolean b) {
jaroslav@86: return b ? "true" : "false";
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns a {@code String} object representing this Boolean's
jaroslav@86: * value. If this object represents the value {@code true},
jaroslav@86: * a string equal to {@code "true"} is returned. Otherwise, a
jaroslav@86: * string equal to {@code "false"} is returned.
jaroslav@86: *
jaroslav@86: * @return a string representation of this object.
jaroslav@86: */
jaroslav@86: public String toString() {
jaroslav@86: return value ? "true" : "false";
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns a hash code for this {@code Boolean} object.
jaroslav@86: *
jaroslav@86: * @return the integer {@code 1231} if this object represents
jaroslav@86: * {@code true}; returns the integer {@code 1237} if this
jaroslav@86: * object represents {@code false}.
jaroslav@86: */
jaroslav@86: public int hashCode() {
jaroslav@86: return value ? 1231 : 1237;
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns {@code true} if and only if the argument is not
jaroslav@86: * {@code null} and is a {@code Boolean} object that
jaroslav@86: * represents the same {@code boolean} value as this object.
jaroslav@86: *
jaroslav@86: * @param obj the object to compare with.
jaroslav@86: * @return {@code true} if the Boolean objects represent the
jaroslav@86: * same value; {@code false} otherwise.
jaroslav@86: */
jaroslav@86: public boolean equals(Object obj) {
jaroslav@86: if (obj instanceof Boolean) {
jaroslav@86: return value == ((Boolean)obj).booleanValue();
jaroslav@86: }
jaroslav@86: return false;
jaroslav@86: }
jaroslav@86:
jaroslav@86: /**
jaroslav@86: * Returns {@code true} if and only if the system property
jaroslav@86: * named by the argument exists and is equal to the string
jaroslav@86: * {@code "true"}. (Beginning with version 1.0.2 of the
jaroslav@86: * JavaTM platform, the test of
jaroslav@86: * this string is case insensitive.) A system property is accessible
jaroslav@86: * through {@code getProperty}, a method defined by the
jaroslav@86: * {@code System} class.
jaroslav@86: *
jaroslav@86: * Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
jaroslav@86: *
jaroslav@86: *
jaroslav@86: * @param x the first {@code boolean} to compare
jaroslav@86: * @param y the second {@code boolean} to compare
jaroslav@86: * @return the value {@code 0} if {@code x == y};
jaroslav@86: * a value less than {@code 0} if {@code !x && y}; and
jaroslav@86: * a value greater than {@code 0} if {@code x && !y}
jaroslav@86: * @since 1.7
jaroslav@86: */
jaroslav@86: public static int compare(boolean x, boolean y) {
jaroslav@86: return (x == y) ? 0 : (x ? 1 : -1);
jaroslav@86: }
jaroslav@86:
jaroslav@86: private static boolean toBoolean(String name) {
jaroslav@86: return ((name != null) && name.equalsIgnoreCase("true"));
jaroslav@86: }
jaroslav@86: }