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
018package org.apache.commons.lang3.mutable;
019
020import java.io.Serializable;
021
022/**
023 * A mutable <code>boolean</code> wrapper.
024 * <p>
025 * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter. 
026 * 
027 * @see Boolean
028 * @since 2.2
029 * @version $Id: MutableBoolean.java 1565915 2014-02-08 04:35:17Z ggregory $
030 */
031public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
032
033    /**
034     * Required for serialization support.
035     * 
036     * @see java.io.Serializable
037     */
038    private static final long serialVersionUID = -4830728138360036487L;
039
040    /** The mutable value. */
041    private boolean value;
042
043    /**
044     * Constructs a new MutableBoolean with the default value of false.
045     */
046    public MutableBoolean() {
047        super();
048    }
049
050    /**
051     * Constructs a new MutableBoolean with the specified value.
052     * 
053     * @param value  the initial value to store
054     */
055    public MutableBoolean(final boolean value) {
056        super();
057        this.value = value;
058    }
059
060    /**
061     * Constructs a new MutableBoolean with the specified value.
062     * 
063     * @param value  the initial value to store, not null
064     * @throws NullPointerException if the object is null
065     */
066    public MutableBoolean(final Boolean value) {
067        super();
068        this.value = value.booleanValue();
069    }
070
071    //-----------------------------------------------------------------------
072    /**
073     * Gets the value as a Boolean instance.
074     * 
075     * @return the value as a Boolean, never null
076     */
077    @Override
078    public Boolean getValue() {
079        return Boolean.valueOf(this.value);
080    }
081
082    /**
083     * Sets the value.
084     * 
085     * @param value  the value to set
086     */
087    public void setValue(final boolean value) {
088        this.value = value;
089    }
090
091    /**
092     * Sets the value to true.
093     * 
094     * @since 3.3
095     */
096    public void setFalse() {
097        this.value = false;
098    }
099
100    /**
101     * Sets the value to false.
102     * 
103     * @since 3.3
104     */
105    public void setTrue() {
106        this.value = true;
107    }
108
109    /**
110     * Sets the value from any Boolean instance.
111     * 
112     * @param value  the value to set, not null
113     * @throws NullPointerException if the object is null
114     */
115    @Override
116    public void setValue(final Boolean value) {
117        this.value = value.booleanValue();
118    }
119
120    //-----------------------------------------------------------------------
121    /**
122     * Checks if the current value is <code>true</code>.
123     * 
124     * @return <code>true</code> if the current value is <code>true</code>
125     * @since 2.5
126     */
127    public boolean isTrue() {
128        return value == true;
129    }
130
131    /**
132     * Checks if the current value is <code>false</code>.
133     * 
134     * @return <code>true</code> if the current value is <code>false</code>
135     * @since 2.5
136     */
137    public boolean isFalse() {
138        return value == false;
139    }
140
141    //-----------------------------------------------------------------------
142    /**
143     * Returns the value of this MutableBoolean as a boolean.
144     * 
145     * @return the boolean value represented by this object.
146     */
147    public boolean booleanValue() {
148        return value;
149    }
150
151    //-----------------------------------------------------------------------
152    /**
153     * Gets this mutable as an instance of Boolean.
154     *
155     * @return a Boolean instance containing the value from this mutable, never null
156     * @since 2.5
157     */
158    public Boolean toBoolean() {
159        return Boolean.valueOf(booleanValue());
160    }
161
162    //-----------------------------------------------------------------------
163    /**
164     * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
165     * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
166     * <code>boolean</code> value as this object.
167     * 
168     * @param obj  the object to compare with, null returns false
169     * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
170     */
171    @Override
172    public boolean equals(final Object obj) {
173        if (obj instanceof MutableBoolean) {
174            return value == ((MutableBoolean) obj).booleanValue();
175        }
176        return false;
177    }
178
179    /**
180     * Returns a suitable hash code for this mutable.
181     * 
182     * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
183     */
184    @Override
185    public int hashCode() {
186        return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
187    }
188
189    //-----------------------------------------------------------------------
190    /**
191     * Compares this mutable to another in ascending order.
192     * 
193     * @param other  the other mutable to compare to, not null
194     * @return negative if this is less, zero if equal, positive if greater
195     *  where false is less than true
196     */
197    @Override
198    public int compareTo(final MutableBoolean other) {
199        final boolean anotherVal = other.value;
200        return value == anotherVal ? 0 : (value ? 1 : -1);
201    }
202
203    //-----------------------------------------------------------------------
204    /**
205     * Returns the String value of this mutable.
206     * 
207     * @return the mutable value as a string
208     */
209    @Override
210    public String toString() {
211        return String.valueOf(value);
212    }
213
214}