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 org.apache.commons.lang.mutable;
019:
020: import java.io.Serializable;
021:
022: import org.apache.commons.lang.BooleanUtils;
023:
024: /**
025: * A mutable <code>boolean</code> wrapper.
026: *
027: * @see Boolean
028: * @since 2.2
029: * @author Apache Software Foundation
030: * @version $Id: MutableBoolean.java 491052 2006-12-29 17:16:37Z scolebourne $
031: */
032: public class MutableBoolean implements Mutable, Serializable,
033: Comparable {
034:
035: /**
036: * Required for serialization support.
037: *
038: * @see java.io.Serializable
039: */
040: private static final long serialVersionUID = -4830728138360036487L;
041:
042: /** The mutable value. */
043: private boolean value;
044:
045: /**
046: * Constructs a new MutableBoolean with the default value of false.
047: */
048: public MutableBoolean() {
049: super ();
050: }
051:
052: /**
053: * Constructs a new MutableBoolean with the specified value.
054: *
055: * @param value
056: * a value.
057: */
058: public MutableBoolean(boolean value) {
059: super ();
060: this .value = value;
061: }
062:
063: /**
064: * Constructs a new MutableBoolean with the specified value.
065: *
066: * @param value
067: * a value.
068: * @throws NullPointerException
069: * if the object is null
070: */
071: public MutableBoolean(Boolean value) {
072: super ();
073: this .value = value.booleanValue();
074: }
075:
076: // -----------------------------------------------------------------------
077: /**
078: * Returns the value of this MutableBoolean as a boolean.
079: *
080: * @return the boolean value represented by this object.
081: */
082: public boolean booleanValue() {
083: return value;
084: }
085:
086: /**
087: * Compares this mutable to another in ascending order.
088: *
089: * @param obj
090: * the mutable to compare to
091: * @return zero if this object represents the same boolean value as the argument; a positive value if this object
092: * represents true and the argument represents false; and a negative value if this object represents false
093: * and the argument represents true
094: * @throws ClassCastException
095: * if the argument is not a MutableInt
096: */
097: public int compareTo(Object obj) {
098: MutableBoolean other = (MutableBoolean) obj;
099: boolean anotherVal = other.value;
100: return value == anotherVal ? 0 : (value ? 1 : -1);
101: }
102:
103: // -----------------------------------------------------------------------
104: /**
105: * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
106: * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
107: * <code>boolean</code> value as this object.
108: *
109: * @param obj
110: * the object to compare with.
111: * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
112: */
113: public boolean equals(Object obj) {
114: if (obj instanceof MutableBoolean) {
115: return value == ((MutableBoolean) obj).booleanValue();
116: }
117: return false;
118: }
119:
120: // -----------------------------------------------------------------------
121: /**
122: * Gets the value as a Boolean instance.
123: *
124: * @return the value as a Boolean
125: */
126: public Object getValue() {
127: return BooleanUtils.toBooleanObject(this .value);
128: }
129:
130: /**
131: * Returns a suitable hashcode for this mutable.
132: *
133: * @return the integer <code>1231</code> if this object represents <code>true</code>; returns the integer
134: * <code>1237</code> if this object represents <code>false</code>.
135: */
136: public int hashCode() {
137: return value ? Boolean.TRUE.hashCode() : Boolean.FALSE
138: .hashCode();
139: }
140:
141: /**
142: * Sets the value.
143: *
144: * @param value
145: * the value to set
146: */
147: public void setValue(boolean value) {
148: this .value = value;
149: }
150:
151: /**
152: * Sets the value from any Boolean instance.
153: *
154: * @param value
155: * the value to set
156: * @throws NullPointerException
157: * if the object is null
158: * @throws ClassCastException
159: * if the type is not a {@link Boolean}
160: */
161: public void setValue(Object value) {
162: setValue(((Boolean) value).booleanValue());
163: }
164:
165: /**
166: * Returns the String value of this mutable.
167: *
168: * @return the mutable value as a string
169: */
170: public String toString() {
171: return String.valueOf(value);
172: }
173:
174: }
|