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 018package org.apache.commons.lang3.mutable; 019 020import java.io.Serializable; 021 022import org.apache.commons.lang3.BooleanUtils; 023 024/** 025 * A mutable {@code boolean} wrapper. 026 * <p> 027 * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter. 028 * 029 * @see Boolean 030 * @since 2.2 031 */ 032public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> { 033 034 /** 035 * Required for serialization support. 036 * 037 * @see java.io.Serializable 038 */ 039 private static final long serialVersionUID = -4830728138360036487L; 040 041 /** The mutable value. */ 042 private boolean value; 043 044 /** 045 * Constructs a new MutableBoolean with the default value of false. 046 */ 047 public MutableBoolean() { 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(final boolean value) { 056 this.value = value; 057 } 058 059 /** 060 * Constructs a new MutableBoolean with the specified value. 061 * 062 * @param value the initial value to store, not null 063 * @throws NullPointerException if the object is null 064 */ 065 public MutableBoolean(final Boolean value) { 066 this.value = value.booleanValue(); 067 } 068 069 //----------------------------------------------------------------------- 070 /** 071 * Gets the value as a Boolean instance. 072 * 073 * @return the value as a Boolean, never null 074 */ 075 @Override 076 public Boolean getValue() { 077 return Boolean.valueOf(this.value); 078 } 079 080 /** 081 * Sets the value. 082 * 083 * @param value the value to set 084 */ 085 public void setValue(final boolean value) { 086 this.value = value; 087 } 088 089 /** 090 * Sets the value to false. 091 * 092 * @since 3.3 093 */ 094 public void setFalse() { 095 this.value = false; 096 } 097 098 /** 099 * Sets the value to true. 100 * 101 * @since 3.3 102 */ 103 public void setTrue() { 104 this.value = true; 105 } 106 107 /** 108 * Sets the value from any Boolean instance. 109 * 110 * @param value the value to set, not null 111 * @throws NullPointerException if the object is null 112 */ 113 @Override 114 public void setValue(final Boolean value) { 115 this.value = value.booleanValue(); 116 } 117 118 //----------------------------------------------------------------------- 119 /** 120 * Checks if the current value is {@code true}. 121 * 122 * @return {@code true} if the current value is {@code true} 123 * @since 2.5 124 */ 125 public boolean isTrue() { 126 return value; 127 } 128 129 /** 130 * Checks if the current value is {@code false}. 131 * 132 * @return {@code true} if the current value is {@code false} 133 * @since 2.5 134 */ 135 public boolean isFalse() { 136 return !value; 137 } 138 139 //----------------------------------------------------------------------- 140 /** 141 * Returns the value of this MutableBoolean as a boolean. 142 * 143 * @return the boolean value represented by this object. 144 */ 145 public boolean booleanValue() { 146 return value; 147 } 148 149 //----------------------------------------------------------------------- 150 /** 151 * Gets this mutable as an instance of Boolean. 152 * 153 * @return a Boolean instance containing the value from this mutable, never null 154 * @since 2.5 155 */ 156 public Boolean toBoolean() { 157 return Boolean.valueOf(booleanValue()); 158 } 159 160 //----------------------------------------------------------------------- 161 /** 162 * Compares this object to the specified object. The result is {@code true} if and only if the argument is 163 * not {@code null} and is an {@code MutableBoolean} object that contains the same 164 * {@code boolean} value as this object. 165 * 166 * @param obj the object to compare with, null returns false 167 * @return {@code true} if the objects are the same; {@code false} otherwise. 168 */ 169 @Override 170 public boolean equals(final Object obj) { 171 if (obj instanceof MutableBoolean) { 172 return value == ((MutableBoolean) obj).booleanValue(); 173 } 174 return false; 175 } 176 177 /** 178 * Returns a suitable hash code for this mutable. 179 * 180 * @return the hash code returned by {@code Boolean.TRUE} or {@code Boolean.FALSE} 181 */ 182 @Override 183 public int hashCode() { 184 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(); 185 } 186 187 //----------------------------------------------------------------------- 188 /** 189 * Compares this mutable to another in ascending order. 190 * 191 * @param other the other mutable to compare to, not null 192 * @return negative if this is less, zero if equal, positive if greater 193 * where false is less than true 194 */ 195 @Override 196 public int compareTo(final MutableBoolean other) { 197 return BooleanUtils.compare(this.value, other.value); 198 } 199 200 //----------------------------------------------------------------------- 201 /** 202 * Returns the String value of this mutable. 203 * 204 * @return the mutable value as a string 205 */ 206 @Override 207 public String toString() { 208 return String.valueOf(value); 209 } 210 211}