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    *      http://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  
22  /**
23   * A mutable <code>boolean</code> wrapper.
24   * <p>
25   * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter. 
26   * 
27   * @see Boolean
28   * @since 2.2
29   * @version $Id: MutableBoolean.java 1436770 2013-01-22 07:09:45Z ggregory $
30   */
31  public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
32  
33      /**
34       * Required for serialization support.
35       * 
36       * @see java.io.Serializable
37       */
38      private static final long serialVersionUID = -4830728138360036487L;
39  
40      /** The mutable value. */
41      private boolean value;
42  
43      /**
44       * Constructs a new MutableBoolean with the default value of false.
45       */
46      public MutableBoolean() {
47          super();
48      }
49  
50      /**
51       * Constructs a new MutableBoolean with the specified value.
52       * 
53       * @param value  the initial value to store
54       */
55      public MutableBoolean(final boolean value) {
56          super();
57          this.value = value;
58      }
59  
60      /**
61       * Constructs a new MutableBoolean with the specified value.
62       * 
63       * @param value  the initial value to store, not null
64       * @throws NullPointerException if the object is null
65       */
66      public MutableBoolean(final Boolean value) {
67          super();
68          this.value = value.booleanValue();
69      }
70  
71      //-----------------------------------------------------------------------
72      /**
73       * Gets the value as a Boolean instance.
74       * 
75       * @return the value as a Boolean, never null
76       */
77      @Override
78      public Boolean getValue() {
79          return Boolean.valueOf(this.value);
80      }
81  
82      /**
83       * Sets the value.
84       * 
85       * @param value  the value to set
86       */
87      public void setValue(final boolean value) {
88          this.value = value;
89      }
90  
91      /**
92       * Sets the value from any Boolean instance.
93       * 
94       * @param value  the value to set, not null
95       * @throws NullPointerException if the object is null
96       */
97      @Override
98      public void setValue(final Boolean value) {
99          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 }