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