001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang;
019:
020: import java.io.Serializable;
021:
022: /**
023: * Boolean is the wrapper for the primitive type <code>boolean</code>.
024: *
025: * @since 1.0
026: */
027: public final class Boolean implements Serializable, Comparable<Boolean> {
028:
029: private static final long serialVersionUID = -3665804199014368530L;
030:
031: /**
032: * The boolean value of the receiver.
033: */
034: private final boolean value;
035:
036: /**
037: * The {@link java.lang.Class} that represents this class.
038: */
039: @SuppressWarnings("unchecked")
040: public static final Class<Boolean> TYPE = (Class<Boolean>) new boolean[0]
041: .getClass().getComponentType();
042:
043: // Note: This can't be set to "boolean.class", since *that* is
044: // defined to be "java.lang.Boolean.TYPE";
045:
046: /**
047: * The instance of the receiver which represents truth.
048: */
049: public static final Boolean TRUE = new Boolean(true);
050:
051: /**
052: * The instance of the receiver which represents falsehood.
053: */
054: public static final Boolean FALSE = new Boolean(false);
055:
056: /**
057: * Constructs a new instance of this class given a string. If the string is
058: * equal to "true" using a non-case sensitive comparison, the result will be
059: * a Boolean representing true, otherwise it will be a Boolean representing
060: * false.
061: *
062: * @param string
063: * The name of the desired boolean.
064: */
065: public Boolean(String string) {
066: this (parseBoolean(string));
067: }
068:
069: /**
070: * Constructs a new instance of this class given true or false.
071: *
072: * @param value
073: * true or false.
074: */
075: public Boolean(boolean value) {
076: this .value = value;
077: }
078:
079: /**
080: * Answers true if the receiver represents true and false if the receiver
081: * represents false.
082: *
083: * @return true or false.
084: */
085: public boolean booleanValue() {
086: return value;
087: }
088:
089: /**
090: * Compares the argument to the receiver, and answers true if they represent
091: * the <em>same</em> object using a class specific comparison.
092: * <p>
093: * In this case, the argument must also be a Boolean, and the receiver and
094: * argument must represent the same boolean value (i.e. both true or both
095: * false).
096: * </p>
097: *
098: * @param o
099: * the object to compare with this object
100: * @return <code>true</code> if the object is the same as this object
101: * <code>false</code> if it is different from this object
102: *
103: * @see #hashCode
104: */
105: @Override
106: public boolean equals(Object o) {
107: return (o == this )
108: || ((o instanceof Boolean) && (value == ((Boolean) o).value));
109: }
110:
111: /**
112: * Compares this <code>Boolean</code> to another <code>Boolean</code>.
113: * If this instance has the same value as the instance passed, then
114: * <code>0</code> is returned. If this instance is <code>true</code> and
115: * the instance passed is <code>false</code>, then a positive value is
116: * returned. If this instance is <code>false</code> and the instance
117: * passed is <code>true</code>, then a negative value is returned.
118: *
119: * @param that
120: * The instance to compare to.
121: * @throws NullPointerException
122: * if <code>that</code> is <code>null</code>.
123: * @since 1.5
124: * @see java.lang.Comparable
125: */
126: public int compareTo(Boolean that) {
127: if (that == null) {
128: throw new NullPointerException();
129: }
130:
131: if (this .value == that.value) {
132: return 0;
133: }
134:
135: return this .value ? 1 : -1;
136: }
137:
138: /**
139: * Answers an integer hash code for the receiver. Any two objects which
140: * answer <code>true</code> when passed to <code>equals</code> must
141: * answer the same value for this method.
142: *
143: * @return the receiver's hash
144: * @see #equals
145: */
146: @Override
147: public int hashCode() {
148: return value ? 1231 : 1237;
149: }
150:
151: /**
152: * Answers a string containing a concise, human-readable description of the
153: * receiver.
154: *
155: * @return a printable representation for the receiver.
156: */
157: @Override
158: public String toString() {
159: return String.valueOf(value);
160: }
161:
162: /**
163: * Answers true if the system property described by the argument equal to
164: * "true" using case insensitive comparison, and false otherwise.
165: *
166: * @param string
167: * The name of the desired boolean.
168: * @return The boolean value.
169: */
170: public static boolean getBoolean(String string) {
171: if (string == null || string.length() == 0) {
172: return false;
173: }
174: return (parseBoolean(System.getProperty(string)));
175: }
176:
177: /**
178: * Parses the string as a <code>boolean</code>. If the string is not
179: * <code>null</code> and is equal to <code>"true"</code>, regardless
180: * case, then <code>true</code> is returned, otherwise <code>false</code>.
181: *
182: * @param s
183: * The string to parse.
184: * @return A boolean value.
185: * @since 1.5
186: */
187: public static boolean parseBoolean(String s) {
188: return "true".equalsIgnoreCase(s); //$NON-NLS-1$
189: }
190:
191: /**
192: * Converts the specified boolean to its string representation. When the
193: * boolean is true answer <code>"true"</code>, otherwise answer
194: * <code>"false"</code>.
195: *
196: * @param value
197: * the boolean
198: * @return the boolean converted to a string
199: */
200: public static String toString(boolean value) {
201: return String.valueOf(value);
202: }
203:
204: /**
205: * Answers a Boolean representing true if the argument is equal to "true"
206: * using case insensitive comparison, and a Boolean representing false
207: * otherwise.
208: *
209: * @param string
210: * The name of the desired boolean.
211: * @return the boolean value.
212: */
213: public static Boolean valueOf(String string) {
214: return parseBoolean(string) ? Boolean.TRUE : Boolean.FALSE;
215: }
216:
217: /**
218: * Answers Boolean.TRUE if the argument is equal to "true" using case
219: * insensitive comparison, and Boolean.FALSE representing false otherwise.
220: *
221: * @param b
222: * the boolean value.
223: * @return Boolean.TRUE or Boolean.FALSE Global true/false objects.
224: */
225: public static Boolean valueOf(boolean b) {
226: return b ? Boolean.TRUE : Boolean.FALSE;
227: }
228: }
|