1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.lang;
19
20import java.io.Serializable;
21
22/**
23 * The wrapper for the primitive type {@code boolean}.
24 *
25 * @since 1.0
26 */
27public final class Boolean implements Serializable, Comparable<Boolean> {
28
29    private static final long serialVersionUID = -3665804199014368530L;
30
31    /**
32     * The boolean value of the receiver.
33     */
34    private final boolean value;
35
36    /**
37     * The {@link Class} object that represents the primitive type {@code
38     * boolean}.
39     */
40    @SuppressWarnings("unchecked")
41    public static final Class<Boolean> TYPE
42             = (Class<Boolean>) boolean[].class.getComponentType();
43    // Note: This can't be set to "boolean.class", since *that* is
44    // defined to be "java.lang.Boolean.TYPE";
45
46    /**
47     * The {@code Boolean} object that represents the primitive value
48     * {@code true}.
49     */
50    public static final Boolean TRUE = new Boolean(true);
51
52    /**
53     * The {@code Boolean} object that represents the primitive value
54     * {@code false}.
55     */
56    public static final Boolean FALSE = new Boolean(false);
57
58    /**
59     * Constructs a new {@code Boolean} with its boolean value specified by
60     * {@code string}. If {@code string} is not {@code null} and is equal to
61     * "true" using a non-case sensitive comparison, the result will be a
62     * Boolean representing the primitive value {@code true}, otherwise it will
63     * be a Boolean representing the primitive value {@code false}.
64     *
65     * @param string
66     *            the string representing a boolean value.
67     */
68    public Boolean(String string) {
69        this(parseBoolean(string));
70    }
71
72    /**
73     * Constructs a new {@code Boolean} with the specified primitive boolean
74     * value.
75     *
76     * @param value
77     *            the primitive boolean value, {@code true} or {@code false}.
78     */
79    public Boolean(boolean value) {
80        this.value = value;
81    }
82
83    /**
84     * Gets the primitive value of this boolean, either {@code true} or
85     * {@code false}.
86     *
87     * @return this object's primitive value, {@code true} or {@code false}.
88     */
89    public boolean booleanValue() {
90        return value;
91    }
92
93    /**
94     * Compares this instance with the specified object and indicates if they
95     * are equal. In order to be equal, {@code o} must be an instance of
96     * {@code Boolean} and have the same boolean value as this object.
97     *
98     * @param o
99     *            the object to compare this boolean with.
100     * @return {@code true} if the specified object is equal to this
101     *         {@code Boolean}; {@code false} otherwise.
102     */
103    @Override
104    public boolean equals(Object o) {
105        return (o == this)
106                || ((o instanceof Boolean) && (value == ((Boolean) o).value));
107    }
108
109    /**
110     * Compares this object to the specified boolean object to determine their
111     * relative order.
112     *
113     * @param that
114     *            the boolean object to compare this object to.
115     * @return 0 if the value of this boolean and the value of {@code that} are
116     *         equal; a positive value if the value of this boolean is
117     *         {@code true} and the value of {@code that} is {@code false}; a
118     *         negative value if the value if this boolean is {@code false} and
119     *         the value of {@code that} is {@code true}.
120     * @see java.lang.Comparable
121     * @since 1.5
122     */
123    public int compareTo(Boolean that) {
124        return value == that.value ? 0 : value ? 1 : -1;
125    }
126
127    /**
128     * Returns an integer hash code for this boolean.
129     *
130     * @return this boolean's hash code, which is {@code 1231} for {@code true}
131     *         values and {@code 1237} for {@code false} values.
132     */
133    @Override
134    public int hashCode() {
135        return value ? 1231 : 1237;
136    }
137
138    /**
139     * Returns a string containing a concise, human-readable description of this
140     * boolean.
141     *
142     * @return "true" if the value of this boolean is {@code true}, "false"
143     *         otherwise.
144     */
145    @Override
146    public String toString() {
147        return String.valueOf(value);
148    }
149
150    /**
151     * Returns the {@code boolean} value of the system property identified by
152     * {@code string}.
153     *
154     * @param string
155     *            the name of the requested system property.
156     * @return {@code true} if the system property named by {@code string}
157     *         exists and it is equal to "true" using case insensitive
158     *         comparison, {@code false} otherwise.
159     * @see System#getProperty(String)
160     */
161    public static boolean getBoolean(String string) {
162        if (string == null || string.length() == 0) {
163            return false;
164        }
165        return (parseBoolean(System.getProperty(string)));
166    }
167
168    /**
169     * Parses the specified string as a {@code boolean}.
170     *
171     * @param s
172     *            the string representation of a boolean value.
173     * @return {@code true} if {@code s} is not {@code null} and is equal to
174     *         {@code "true"} using case insensitive comparison, {@code false}
175     *         otherwise.
176     * @since 1.5
177     */
178    public static boolean parseBoolean(String s) {
179        return "true".equalsIgnoreCase(s); //$NON-NLS-1$
180    }
181
182    /**
183     * Converts the specified boolean to its string representation.
184     *
185     * @param value
186     *            the boolean to convert.
187     * @return "true" if {@code value} is {@code true}, "false" otherwise.
188     */
189    public static String toString(boolean value) {
190        return String.valueOf(value);
191    }
192
193    /**
194     * Parses the specified string as a boolean value.
195     *
196     * @param string
197     *            the string representation of a boolean value.
198     * @return {@code Boolean.TRUE} if {@code string} is equal to "true" using
199     *         case insensitive comparison, {@code Boolean.FALSE} otherwise.
200     * @see #parseBoolean(String)
201     */
202    public static Boolean valueOf(String string) {
203        return parseBoolean(string) ? Boolean.TRUE : Boolean.FALSE;
204    }
205
206    /**
207     * Returns a {@code Boolean} instance for the specified boolean value.
208     * <p>
209     * If it is not necessary to get a new {@code Boolean} instance, it is
210     * recommended to use this method instead of the constructor, since it
211     * returns its static instances, which results in better performance.
212     *
213     * @param b
214     *            the boolean to convert to a {@code Boolean}.
215     * @return {@code Boolean.TRUE} if {@code b} is equal to {@code true},
216     *         {@code Boolean.FALSE} otherwise.
217     */
218    public static Boolean valueOf(boolean b) {
219        return b ? Boolean.TRUE : Boolean.FALSE;
220    }
221}
222