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