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 * </p> 029 * 030 * @see Boolean 031 * @since 2.2 032 */ 033public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> { 034 035 /** 036 * Required for serialization support. 037 * 038 * @see java.io.Serializable 039 */ 040 private static final long serialVersionUID = -4830728138360036487L; 041 042 /** The mutable value. */ 043 private boolean value; 044 045 /** 046 * Constructs a new MutableBoolean with the default value of false. 047 */ 048 public MutableBoolean() { 049 } 050 051 /** 052 * Constructs a new MutableBoolean with the specified value. 053 * 054 * @param value the initial value to store 055 */ 056 public MutableBoolean(final boolean value) { 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(final Boolean value) { 067 this.value = value.booleanValue(); 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 * Checks if the current value is {@code true}. 120 * 121 * @return {@code true} if the current value is {@code true} 122 * @since 2.5 123 */ 124 public boolean isTrue() { 125 return value; 126 } 127 128 /** 129 * Checks if the current value is {@code false}. 130 * 131 * @return {@code true} if the current value is {@code false} 132 * @since 2.5 133 */ 134 public boolean isFalse() { 135 return !value; 136 } 137 138 /** 139 * Returns the value of this MutableBoolean as a boolean. 140 * 141 * @return the boolean value represented by this object. 142 */ 143 public boolean booleanValue() { 144 return value; 145 } 146 147 /** 148 * Gets this mutable as an instance of Boolean. 149 * 150 * @return a Boolean instance containing the value from this mutable, never null 151 * @since 2.5 152 */ 153 public Boolean toBoolean() { 154 return Boolean.valueOf(booleanValue()); 155 } 156 157 /** 158 * Compares this object to the specified object. The result is {@code true} if and only if the argument is 159 * not {@code null} and is an {@link MutableBoolean} object that contains the same 160 * {@code boolean} value as this object. 161 * 162 * @param obj the object to compare with, null returns false 163 * @return {@code true} if the objects are the same; {@code false} otherwise. 164 */ 165 @Override 166 public boolean equals(final Object obj) { 167 if (obj instanceof MutableBoolean) { 168 return value == ((MutableBoolean) obj).booleanValue(); 169 } 170 return false; 171 } 172 173 /** 174 * Returns a suitable hash code for this mutable. 175 * 176 * @return the hash code returned by {@code Boolean.TRUE} or {@code Boolean.FALSE} 177 */ 178 @Override 179 public int hashCode() { 180 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(); 181 } 182 183 /** 184 * Compares this mutable to another in ascending order. 185 * 186 * @param other the other mutable to compare to, not null 187 * @return negative if this is less, zero if equal, positive if greater 188 * where false is less than true 189 */ 190 @Override 191 public int compareTo(final MutableBoolean other) { 192 return BooleanUtils.compare(this.value, other.value); 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}