View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.mutable;
19  
20  import java.io.Serializable;
21  import java.util.concurrent.atomic.AtomicBoolean;
22  
23  import org.apache.commons.lang3.BooleanUtils;
24  
25  /**
26   * A mutable {@code boolean} wrapper.
27   * <p>
28   * This class was created before the introduction of {@link AtomicBoolean}.
29   * </p>
30   * <p>
31   * Note that as MutableBoolean does not extend {@link Boolean}, it is not treated by {@link String#format(String, Object...)} as a Boolean parameter.
32   * </p>
33   *
34   * @see Boolean
35   * @see AtomicBoolean
36   * @since 2.2
37   */
38  public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
39  
40      /**
41       * Required for serialization support.
42       *
43       * @see java.io.Serializable
44       */
45      private static final long serialVersionUID = -4830728138360036487L;
46  
47      /** The mutable value. */
48      private boolean value;
49  
50      /**
51       * Constructs a new MutableBoolean with the default value of false.
52       */
53      public MutableBoolean() {
54      }
55  
56      /**
57       * Constructs a new MutableBoolean with the specified value.
58       *
59       * @param value  the initial value to store
60       */
61      public MutableBoolean(final boolean value) {
62          this.value = value;
63      }
64  
65      /**
66       * Constructs a new MutableBoolean with the specified value.
67       *
68       * @param value  the initial value to store, not null
69       * @throws NullPointerException if the object is null
70       */
71      public MutableBoolean(final Boolean value) {
72          this.value = value.booleanValue();
73      }
74  
75      /**
76       * Returns the value of this MutableBoolean as a boolean.
77       *
78       * @return the boolean value represented by this object.
79       */
80      public boolean booleanValue() {
81          return value;
82      }
83  
84      /**
85       * Compares this mutable to another in ascending order.
86       *
87       * @param other  the other mutable to compare to, not null
88       * @return negative if this is less, zero if equal, positive if greater
89       *  where false is less than true
90       */
91      @Override
92      public int compareTo(final MutableBoolean other) {
93          return BooleanUtils.compare(this.value, other.value);
94      }
95  
96      /**
97       * Compares this object to the specified object. The result is {@code true} if and only if the argument is
98       * not {@code null} and is an {@link MutableBoolean} object that contains the same
99       * {@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 }