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.lang.mutable; 019 020 import java.io.Serializable; 021 022 import org.apache.commons.lang.BooleanUtils; 023 024 /** 025 * A mutable <code>boolean</code> wrapper. 026 * 027 * @see Boolean 028 * @since 2.2 029 * @author Apache Software Foundation 030 * @version $Id: MutableBoolean.java 905707 2010-02-02 16:59:59Z niallp $ 031 */ 032 public class MutableBoolean implements Mutable, Serializable, Comparable { 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 super(); 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(boolean value) { 057 super(); 058 this.value = value; 059 } 060 061 /** 062 * Constructs a new MutableBoolean with the specified value. 063 * 064 * @param value the initial value to store, not null 065 * @throws NullPointerException if the object is null 066 */ 067 public MutableBoolean(Boolean value) { 068 super(); 069 this.value = value.booleanValue(); 070 } 071 072 //----------------------------------------------------------------------- 073 /** 074 * Gets the value as a Boolean instance. 075 * 076 * @return the value as a Boolean, never null 077 */ 078 public Object getValue() { 079 return BooleanUtils.toBooleanObject(this.value); 080 } 081 082 /** 083 * Sets the value. 084 * 085 * @param value the value to set 086 */ 087 public void setValue(boolean value) { 088 this.value = value; 089 } 090 091 /** 092 * Sets the value from any Boolean instance. 093 * 094 * @param value the value to set, not null 095 * @throws NullPointerException if the object is null 096 */ 097 public void setValue(Object value) { 098 setValue(((Boolean) value).booleanValue()); 099 } 100 101 //----------------------------------------------------------------------- 102 /** 103 * Checks if the current value is <code>true</code>. 104 * 105 * @return <code>true</code> if the current value is <code>true</code> 106 * @since 2.5 107 */ 108 public boolean isTrue() { 109 return value == true; 110 } 111 112 /** 113 * Checks if the current value is <code>false</code>. 114 * 115 * @return <code>true</code> if the current value is <code>false</code> 116 * @since 2.5 117 */ 118 public boolean isFalse() { 119 return value == false; 120 } 121 122 //----------------------------------------------------------------------- 123 /** 124 * Returns the value of this MutableBoolean as a boolean. 125 * 126 * @return the boolean value represented by this object. 127 */ 128 public boolean booleanValue() { 129 return value; 130 } 131 132 //----------------------------------------------------------------------- 133 /** 134 * Gets this mutable as an instance of Boolean. 135 * 136 * @return a Boolean instance containing the value from this mutable, never null 137 * @since 2.5 138 */ 139 public Boolean toBoolean() { 140 return BooleanUtils.toBooleanObject(this.value); 141 } 142 143 //----------------------------------------------------------------------- 144 /** 145 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is 146 * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same 147 * <code>boolean</code> value as this object. 148 * 149 * @param obj the object to compare with, null returns false 150 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 151 */ 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 public int hashCode() { 165 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(); 166 } 167 168 //----------------------------------------------------------------------- 169 /** 170 * Compares this mutable to another in ascending order. 171 * 172 * @param obj the other mutable to compare to, not null 173 * @return negative if this is less, zero if equal, positive if greater 174 * where false is less than true 175 */ 176 public int compareTo(Object obj) { 177 MutableBoolean other = (MutableBoolean) obj; 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 public String toString() { 189 return String.valueOf(value); 190 } 191 192 }