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 * @author Apache Software Foundation 028 * @version $Id: MutableBoolean.java 916081 2010-02-25 01:28:13Z niallp $ 029 */ 030 public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> { 031 032 /** 033 * Required for serialization support. 034 * 035 * @see java.io.Serializable 036 */ 037 private static final long serialVersionUID = -4830728138360036487L; 038 039 /** The mutable value. */ 040 private boolean value; 041 042 /** 043 * Constructs a new MutableBoolean with the default value of false. 044 */ 045 public MutableBoolean() { 046 super(); 047 } 048 049 /** 050 * Constructs a new MutableBoolean with the specified value. 051 * 052 * @param value the initial value to store 053 */ 054 public MutableBoolean(boolean value) { 055 super(); 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(Boolean value) { 066 super(); 067 this.value = value.booleanValue(); 068 } 069 070 //----------------------------------------------------------------------- 071 /** 072 * Gets the value as a Boolean instance. 073 * 074 * @return the value as a Boolean, never null 075 */ 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(boolean value) { 086 this.value = value; 087 } 088 089 /** 090 * Sets the value from any Boolean instance. 091 * 092 * @param value the value to set, not null 093 * @throws NullPointerException if the object is null 094 */ 095 public void setValue(Boolean value) { 096 this.value = value.booleanValue(); 097 } 098 099 //----------------------------------------------------------------------- 100 /** 101 * Checks if the current value is <code>true</code>. 102 * 103 * @return <code>true</code> if the current value is <code>true</code> 104 * @since 2.5 105 */ 106 public boolean isTrue() { 107 return value == true; 108 } 109 110 /** 111 * Checks if the current value is <code>false</code>. 112 * 113 * @return <code>true</code> if the current value is <code>false</code> 114 * @since 2.5 115 */ 116 public boolean isFalse() { 117 return value == false; 118 } 119 120 //----------------------------------------------------------------------- 121 /** 122 * Returns the value of this MutableBoolean as a boolean. 123 * 124 * @return the boolean value represented by this object. 125 */ 126 public boolean booleanValue() { 127 return value; 128 } 129 130 //----------------------------------------------------------------------- 131 /** 132 * Gets this mutable as an instance of Boolean. 133 * 134 * @return a Boolean instance containing the value from this mutable, never null 135 * @since 2.5 136 */ 137 public Boolean toBoolean() { 138 return Boolean.valueOf(booleanValue()); 139 } 140 141 //----------------------------------------------------------------------- 142 /** 143 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is 144 * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same 145 * <code>boolean</code> value as this object. 146 * 147 * @param obj the object to compare with, null returns false 148 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 149 */ 150 @Override 151 public boolean equals(Object obj) { 152 if (obj instanceof MutableBoolean) { 153 return value == ((MutableBoolean) obj).booleanValue(); 154 } 155 return false; 156 } 157 158 /** 159 * Returns a suitable hash code for this mutable. 160 * 161 * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code> 162 */ 163 @Override 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 other 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(MutableBoolean other) { 177 boolean anotherVal = other.value; 178 return value == anotherVal ? 0 : (value ? 1 : -1); 179 } 180 181 //----------------------------------------------------------------------- 182 /** 183 * Returns the String value of this mutable. 184 * 185 * @return the mutable value as a string 186 */ 187 @Override 188 public String toString() { 189 return String.valueOf(value); 190 } 191 192 }