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