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