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 491052 2006-12-29 17:16:37Z scolebourne $ 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 055 * a value. 056 */ 057 public MutableBoolean(boolean value) { 058 super(); 059 this.value = value; 060 } 061 062 /** 063 * Constructs a new MutableBoolean with the specified value. 064 * 065 * @param value 066 * a value. 067 * @throws NullPointerException 068 * if the object is null 069 */ 070 public MutableBoolean(Boolean value) { 071 super(); 072 this.value = value.booleanValue(); 073 } 074 075 // ----------------------------------------------------------------------- 076 /** 077 * Returns the value of this MutableBoolean as a boolean. 078 * 079 * @return the boolean value represented by this object. 080 */ 081 public boolean booleanValue() { 082 return value; 083 } 084 085 /** 086 * Compares this mutable to another in ascending order. 087 * 088 * @param obj 089 * the mutable to compare to 090 * @return zero if this object represents the same boolean value as the argument; a positive value if this object 091 * represents true and the argument represents false; and a negative value if this object represents false 092 * and the argument represents true 093 * @throws ClassCastException 094 * if the argument is not a MutableInt 095 */ 096 public int compareTo(Object obj) { 097 MutableBoolean other = (MutableBoolean) obj; 098 boolean anotherVal = other.value; 099 return value == anotherVal ? 0 : (value ? 1 : -1); 100 } 101 102 // ----------------------------------------------------------------------- 103 /** 104 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is 105 * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same 106 * <code>boolean</code> value as this object. 107 * 108 * @param obj 109 * the object to compare with. 110 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 111 */ 112 public boolean equals(Object obj) { 113 if (obj instanceof MutableBoolean) { 114 return value == ((MutableBoolean) obj).booleanValue(); 115 } 116 return false; 117 } 118 119 // ----------------------------------------------------------------------- 120 /** 121 * Gets the value as a Boolean instance. 122 * 123 * @return the value as a Boolean 124 */ 125 public Object getValue() { 126 return BooleanUtils.toBooleanObject(this.value); 127 } 128 129 /** 130 * Returns a suitable hashcode for this mutable. 131 * 132 * @return the integer <code>1231</code> if this object represents <code>true</code>; returns the integer 133 * <code>1237</code> if this object represents <code>false</code>. 134 */ 135 public int hashCode() { 136 return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode(); 137 } 138 139 /** 140 * Sets the value. 141 * 142 * @param value 143 * the value to set 144 */ 145 public void setValue(boolean value) { 146 this.value = value; 147 } 148 149 /** 150 * Sets the value from any Boolean instance. 151 * 152 * @param value 153 * the value to set 154 * @throws NullPointerException 155 * if the object is null 156 * @throws ClassCastException 157 * if the type is not a {@link Boolean} 158 */ 159 public void setValue(Object value) { 160 setValue(((Boolean) value).booleanValue()); 161 } 162 163 /** 164 * Returns the String value of this mutable. 165 * 166 * @return the mutable value as a string 167 */ 168 public String toString() { 169 return String.valueOf(value); 170 } 171 172 }