    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     * @deprecated Use {@link #get()}.
    117     */
    118    @Deprecated
    119    @Override
    120    public Boolean getValue() {
    121        return Boolean.valueOf(this.value);
    122    }
    123
    124    /**
    125     * Returns a suitable hash code for this mutable.
    126     *
    127     * @return the hash code returned by {@code Boolean.TRUE} or {@code Boolean.FALSE}
    128     */
    129    @Override
    130    public int hashCode() {
    131        return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
    132    }
    133
    134    /**
    135     * Checks if the current value is {@code false}.
    136     *
    137     * @return {@code true} if the current value is {@code false}
    138     * @since 2.5
    139     */
    140    public boolean isFalse() {
    141        return !value;
    142    }
    143
    144    /**
    145     * Checks if the current value is {@code true}.
    146     *
    147     * @return {@code true} if the current value is {@code true}
    148     * @since 2.5
    149     */
    150    public boolean isTrue() {
    151        return value;
    152    }
    153
    154    /**
    155     * Sets the value to false.
    156     *
    157     * @since 3.3
    158     */
    159    public void setFalse() {
    160        this.value = false;
    161    }
    162
    163    /**
    164     * Sets the value to true.
    165     *
    166     * @since 3.3
    167     */
    168    public void setTrue() {
    169        this.value = true;
    170    }
    171
    172    /**
    173     * Sets the value.
    174     *
    175     * @param value  the value to set
    176     */
    177    public void setValue(final boolean value) {
    178        this.value = value;
    179    }
    180
    181    /**
    182     * Sets the value from any Boolean instance.
    183     *
    184     * @param value  the value to set, not null
    185     * @throws NullPointerException if the object is null
    186     */
    187    @Override
    188    public void setValue(final Boolean value) {
    189        this.value = value.booleanValue();
    190    }
    191
    192    /**
    193     * Gets this mutable as an instance of Boolean.
    194     *
    195     * @return a Boolean instance containing the value from this mutable, never null
    196     * @since 2.5
    197     */
    198    public Boolean toBoolean() {
    199        return Boolean.valueOf(booleanValue());
    200    }
    201
    202    /**
    203     * Returns the String value of this mutable.
    204     *
    205     * @return the mutable value as a string
    206     */
    207    @Override
    208    public String toString() {
    209        return String.valueOf(value);
    210    }
    211
    212}