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.lang.mutable;
19  
20  import java.io.Serializable;
21  
22  import org.apache.commons.lang.BooleanUtils;
23  
24  /**
25   * A mutable <code>boolean</code> wrapper.
26   * 
27   * @see Boolean
28   * @since 2.2
29   * @author Apache Software Foundation
30   * @version $Id: MutableBoolean.java 491052 2006-12-29 17:16:37Z scolebourne $
31   */
32  public class MutableBoolean implements Mutable, Serializable, Comparable {
33  
34      /**
35       * Required for serialization support.
36       * 
37       * @see java.io.Serializable
38       */
39      private static final long serialVersionUID = -4830728138360036487L;
40  
41      /** The mutable value. */
42      private boolean value;
43  
44      /**
45       * Constructs a new MutableBoolean with the default value of false.
46       */
47      public MutableBoolean() {
48          super();
49      }
50  
51      /**
52       * Constructs a new MutableBoolean with the specified value.
53       * 
54       * @param value
55       *            a value.
56       */
57      public MutableBoolean(boolean value) {
58          super();
59          this.value = value;
60      }
61  
62      /**
63       * Constructs a new MutableBoolean with the specified value.
64       * 
65       * @param value
66       *            a value.
67       * @throws NullPointerException
68       *             if the object is null
69       */
70      public MutableBoolean(Boolean value) {
71          super();
72          this.value = value.booleanValue();
73      }
74  
75      // -----------------------------------------------------------------------
76      /**
77       * Returns the value of this MutableBoolean as a boolean.
78       * 
79       * @return the boolean value represented by this object.
80       */
81      public boolean booleanValue() {
82          return value;
83      }
84  
85      /**
86       * Compares this mutable to another in ascending order.
87       * 
88       * @param obj
89       *            the mutable to compare to
90       * @return zero if this object represents the same boolean value as the argument; a positive value if this object
91       *         represents true and the argument represents false; and a negative value if this object represents false
92       *         and the argument represents true
93       * @throws ClassCastException
94       *             if the argument is not a MutableInt
95       */
96      public int compareTo(Object obj) {
97          MutableBoolean other = (MutableBoolean) obj;
98          boolean anotherVal = other.value;
99          return value == anotherVal ? 0 : (value ? 1 : -1);
100     }
101 
102     // -----------------------------------------------------------------------
103     /**
104      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
105      * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
106      * <code>boolean</code> value as this object.
107      * 
108      * @param obj
109      *            the object to compare with.
110      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
111      */
112     public boolean equals(Object obj) {
113         if (obj instanceof MutableBoolean) {
114             return value == ((MutableBoolean) obj).booleanValue();
115         }
116         return false;
117     }
118 
119     // -----------------------------------------------------------------------
120     /**
121      * Gets the value as a Boolean instance.
122      * 
123      * @return the value as a Boolean
124      */
125     public Object getValue() {
126         return BooleanUtils.toBooleanObject(this.value);
127     }
128 
129     /**
130      * Returns a suitable hashcode for this mutable.
131      * 
132      * @return the integer <code>1231</code> if this object represents <code>true</code>; returns the integer
133      *         <code>1237</code> if this object represents <code>false</code>.
134      */
135     public int hashCode() {
136         return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
137     }
138 
139     /**
140      * Sets the value.
141      * 
142      * @param value
143      *            the value to set
144      */
145     public void setValue(boolean value) {
146         this.value = value;
147     }
148 
149     /**
150      * Sets the value from any Boolean instance.
151      * 
152      * @param value
153      *            the value to set
154      * @throws NullPointerException
155      *             if the object is null
156      * @throws ClassCastException
157      *             if the type is not a {@link Boolean}
158      */
159     public void setValue(Object value) {
160         setValue(((Boolean) value).booleanValue());
161     }
162 
163     /**
164      * Returns the String value of this mutable.
165      * 
166      * @return the mutable value as a string
167      */
168     public String toString() {
169         return String.valueOf(value);
170     }
171 
172 }