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 import org.apache.commons.lang3.BooleanUtils; 23 24 /** 25 * A mutable {@code boolean} wrapper. 26 * <p> 27 * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter. 28 * </p> 29 * 30 * @see Boolean 31 * @since 2.2 32 */ 33 public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> { 34 35 /** 36 * Required for serialization support. 37 * 38 * @see java.io.Serializable 39 */ 40 private static final long serialVersionUID = -4830728138360036487L; 41 42 /** The mutable value. */ 43 private boolean value; 44 45 /** 46 * Constructs a new MutableBoolean with the default value of false. 47 */ 48 public MutableBoolean() { 49 } 50 51 /** 52 * Constructs a new MutableBoolean with the specified value. 53 * 54 * @param value the initial value to store 55 */ 56 public MutableBoolean(final boolean value) { 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 this.value = value.booleanValue(); 68 } 69 70 /** 71 * Returns the value of this MutableBoolean as a boolean. 72 * 73 * @return the boolean value represented by this object. 74 */ 75 public boolean booleanValue() { 76 return value; 77 } 78 79 /** 80 * Compares this mutable to another in ascending order. 81 * 82 * @param other the other mutable to compare to, not null 83 * @return negative if this is less, zero if equal, positive if greater 84 * where false is less than true 85 */ 86 @Override 87 public int compareTo(final MutableBoolean other) { 88 return BooleanUtils.compare(this.value, other.value); 89 } 90 91 /** 92 * Compares this object to the specified object. The result is {@code true} if and only if the argument is 93 * not {@code null} and is an {@link MutableBoolean} object that contains the same 94 * {@code boolean} value as this object. 95 * 96 * @param obj the object to compare with, null returns false 97 * @return {@code true} if the objects are the same; {@code false} otherwise. 98 */ 99 @Override 100 public boolean equals(final Object obj) { 101 if (obj instanceof MutableBoolean) { 102 return value == ((MutableBoolean) obj).booleanValue(); 103 } 104 return false; 105 } 106 107 /** 108 * Gets the value as a Boolean instance. 109 * 110 * @return the value as a Boolean, never null 111 */ 112 @Override 113 public Boolean getValue() { 114 return Boolean.valueOf(this.value); 115 } 116 117 /** 118 * Returns a suitable hash code for this mutable. 119 * 120 * @return the hash code returned by {@code Boolean.TRUE} or {@code Boolean.FALSE} 121 */ 122 @Override 123 public int hashCode() { 124 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(); 125 } 126 127 /** 128 * Checks if the current value is {@code false}. 129 * 130 * @return {@code true} if the current value is {@code false} 131 * @since 2.5 132 */ 133 public boolean isFalse() { 134 return !value; 135 } 136 137 /** 138 * Checks if the current value is {@code true}. 139 * 140 * @return {@code true} if the current value is {@code true} 141 * @since 2.5 142 */ 143 public boolean isTrue() { 144 return value; 145 } 146 147 /** 148 * Sets the value to false. 149 * 150 * @since 3.3 151 */ 152 public void setFalse() { 153 this.value = false; 154 } 155 156 /** 157 * Sets the value to true. 158 * 159 * @since 3.3 160 */ 161 public void setTrue() { 162 this.value = true; 163 } 164 165 /** 166 * Sets the value. 167 * 168 * @param value the value to set 169 */ 170 public void setValue(final boolean value) { 171 this.value = value; 172 } 173 174 /** 175 * Sets the value from any Boolean instance. 176 * 177 * @param value the value to set, not null 178 * @throws NullPointerException if the object is null 179 */ 180 @Override 181 public void setValue(final Boolean value) { 182 this.value = value.booleanValue(); 183 } 184 185 /** 186 * Gets this mutable as an instance of Boolean. 187 * 188 * @return a Boolean instance containing the value from this mutable, never null 189 * @since 2.5 190 */ 191 public Boolean toBoolean() { 192 return Boolean.valueOf(booleanValue()); 193 } 194 195 /** 196 * Returns the String value of this mutable. 197 * 198 * @return the mutable value as a string 199 */ 200 @Override 201 public String toString() { 202 return String.valueOf(value); 203 } 204 205 }