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