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