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
018 package org.apache.commons.lang3.mutable;
019
020 import 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 1160571 2011-08-23 07:36:08Z bayard $
030 */
031 public 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(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(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 public Boolean getValue() {
078 return Boolean.valueOf(this.value);
079 }
080
081 /**
082 * Sets the value.
083 *
084 * @param value the value to set
085 */
086 public void setValue(boolean value) {
087 this.value = value;
088 }
089
090 /**
091 * Sets the value from any Boolean instance.
092 *
093 * @param value the value to set, not null
094 * @throws NullPointerException if the object is null
095 */
096 public void setValue(Boolean value) {
097 this.value = value.booleanValue();
098 }
099
100 //-----------------------------------------------------------------------
101 /**
102 * Checks if the current value is <code>true</code>.
103 *
104 * @return <code>true</code> if the current value is <code>true</code>
105 * @since 2.5
106 */
107 public boolean isTrue() {
108 return value == true;
109 }
110
111 /**
112 * Checks if the current value is <code>false</code>.
113 *
114 * @return <code>true</code> if the current value is <code>false</code>
115 * @since 2.5
116 */
117 public boolean isFalse() {
118 return value == false;
119 }
120
121 //-----------------------------------------------------------------------
122 /**
123 * Returns the value of this MutableBoolean as a boolean.
124 *
125 * @return the boolean value represented by this object.
126 */
127 public boolean booleanValue() {
128 return value;
129 }
130
131 //-----------------------------------------------------------------------
132 /**
133 * Gets this mutable as an instance of Boolean.
134 *
135 * @return a Boolean instance containing the value from this mutable, never null
136 * @since 2.5
137 */
138 public Boolean toBoolean() {
139 return Boolean.valueOf(booleanValue());
140 }
141
142 //-----------------------------------------------------------------------
143 /**
144 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
145 * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
146 * <code>boolean</code> value as this object.
147 *
148 * @param obj the object to compare with, null returns false
149 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
150 */
151 @Override
152 public boolean equals(Object obj) {
153 if (obj instanceof MutableBoolean) {
154 return value == ((MutableBoolean) obj).booleanValue();
155 }
156 return false;
157 }
158
159 /**
160 * Returns a suitable hash code for this mutable.
161 *
162 * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
163 */
164 @Override
165 public int hashCode() {
166 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
167 }
168
169 //-----------------------------------------------------------------------
170 /**
171 * Compares this mutable to another in ascending order.
172 *
173 * @param other the other mutable to compare to, not null
174 * @return negative if this is less, zero if equal, positive if greater
175 * where false is less than true
176 */
177 public int compareTo(MutableBoolean other) {
178 boolean anotherVal = other.value;
179 return value == anotherVal ? 0 : (value ? 1 : -1);
180 }
181
182 //-----------------------------------------------------------------------
183 /**
184 * Returns the String value of this mutable.
185 *
186 * @return the mutable value as a string
187 */
188 @Override
189 public String toString() {
190 return String.valueOf(value);
191 }
192
193 }