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 package org.apache.commons.lang3.mutable; 018 019 /** 020 * A mutable <code>float</code> wrapper. 021 * 022 * @see Float 023 * @since 2.1 024 * @author Apache Software Foundation 025 * @version $Id: MutableFloat.java 916081 2010-02-25 01:28:13Z niallp $ 026 */ 027 public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> { 028 029 /** 030 * Required for serialization support. 031 * 032 * @see java.io.Serializable 033 */ 034 private static final long serialVersionUID = 5787169186L; 035 036 /** The mutable value. */ 037 private float value; 038 039 /** 040 * Constructs a new MutableFloat with the default value of zero. 041 */ 042 public MutableFloat() { 043 super(); 044 } 045 046 /** 047 * Constructs a new MutableFloat with the specified value. 048 * 049 * @param value the initial value to store 050 */ 051 public MutableFloat(float value) { 052 super(); 053 this.value = value; 054 } 055 056 /** 057 * Constructs a new MutableFloat with the specified value. 058 * 059 * @param value the initial value to store, not null 060 * @throws NullPointerException if the object is null 061 */ 062 public MutableFloat(Number value) { 063 super(); 064 this.value = value.floatValue(); 065 } 066 067 /** 068 * Constructs a new MutableFloat parsing the given string. 069 * 070 * @param value the string to parse, not null 071 * @throws NumberFormatException if the string cannot be parsed into a float 072 * @since 2.5 073 */ 074 public MutableFloat(String value) throws NumberFormatException { 075 super(); 076 this.value = Float.parseFloat(value); 077 } 078 079 //----------------------------------------------------------------------- 080 /** 081 * Gets the value as a Float instance. 082 * 083 * @return the value as a Float, never null 084 */ 085 public Float getValue() { 086 return new Float(this.value); 087 } 088 089 /** 090 * Sets the value. 091 * 092 * @param value the value to set 093 */ 094 public void setValue(float value) { 095 this.value = value; 096 } 097 098 /** 099 * Sets the value from any Number instance. 100 * 101 * @param value the value to set, not null 102 * @throws NullPointerException if the object is null 103 */ 104 public void setValue(Number value) { 105 this.value = value.floatValue(); 106 } 107 108 //----------------------------------------------------------------------- 109 /** 110 * Checks whether the float value is the special NaN value. 111 * 112 * @return true if NaN 113 */ 114 public boolean isNaN() { 115 return Float.isNaN(value); 116 } 117 118 /** 119 * Checks whether the float value is infinite. 120 * 121 * @return true if infinite 122 */ 123 public boolean isInfinite() { 124 return Float.isInfinite(value); 125 } 126 127 //----------------------------------------------------------------------- 128 /** 129 * Increments the value. 130 * 131 * @since Commons Lang 2.2 132 */ 133 public void increment() { 134 value++; 135 } 136 137 /** 138 * Decrements the value. 139 * 140 * @since Commons Lang 2.2 141 */ 142 public void decrement() { 143 value--; 144 } 145 146 //----------------------------------------------------------------------- 147 /** 148 * Adds a value to the value of this instance. 149 * 150 * @param operand the value to add, not null 151 * @since Commons Lang 2.2 152 */ 153 public void add(float operand) { 154 this.value += operand; 155 } 156 157 /** 158 * Adds a value to the value of this instance. 159 * 160 * @param operand the value to add, not null 161 * @throws NullPointerException if the object is null 162 * @since Commons Lang 2.2 163 */ 164 public void add(Number operand) { 165 this.value += operand.floatValue(); 166 } 167 168 /** 169 * Subtracts a value from the value of this instance. 170 * 171 * @param operand the value to subtract 172 * @since Commons Lang 2.2 173 */ 174 public void subtract(float operand) { 175 this.value -= operand; 176 } 177 178 /** 179 * Subtracts a value from the value of this instance. 180 * 181 * @param operand the value to subtract, not null 182 * @throws NullPointerException if the object is null 183 * @since Commons Lang 2.2 184 */ 185 public void subtract(Number operand) { 186 this.value -= operand.floatValue(); 187 } 188 189 //----------------------------------------------------------------------- 190 // shortValue and bytValue rely on Number implementation 191 /** 192 * Returns the value of this MutableFloat as an int. 193 * 194 * @return the numeric value represented by this object after conversion to type int. 195 */ 196 @Override 197 public int intValue() { 198 return (int) value; 199 } 200 201 /** 202 * Returns the value of this MutableFloat as a long. 203 * 204 * @return the numeric value represented by this object after conversion to type long. 205 */ 206 @Override 207 public long longValue() { 208 return (long) value; 209 } 210 211 /** 212 * Returns the value of this MutableFloat as a float. 213 * 214 * @return the numeric value represented by this object after conversion to type float. 215 */ 216 @Override 217 public float floatValue() { 218 return value; 219 } 220 221 /** 222 * Returns the value of this MutableFloat as a double. 223 * 224 * @return the numeric value represented by this object after conversion to type double. 225 */ 226 @Override 227 public double doubleValue() { 228 return value; 229 } 230 231 //----------------------------------------------------------------------- 232 /** 233 * Gets this mutable as an instance of Float. 234 * 235 * @return a Float instance containing the value from this mutable, never null 236 */ 237 public Float toFloat() { 238 return Float.valueOf(floatValue()); 239 } 240 241 //----------------------------------------------------------------------- 242 /** 243 * Compares this object against some other object. The result is <code>true</code> if and only if the argument is 244 * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the 245 * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this 246 * purpose, two float values are considered to be the same if and only if the method 247 * {@link Float#floatToIntBits(float)}returns the same int value when applied to each. 248 * <p> 249 * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>, 250 * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote> 251 * 252 * <pre> 253 * f1.floatValue() == f2.floatValue() 254 * </pre> 255 * 256 * </blockquote> 257 * <p> 258 * also has the value <code>true</code>. However, there are two exceptions: 259 * <ul> 260 * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the 261 * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has 262 * the value <code>false</code>. 263 * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>, 264 * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though 265 * <code>0.0f==-0.0f</code> has the value <code>true</code>. 266 * </ul> 267 * This definition allows hashtables to operate properly. 268 * 269 * @param obj the object to compare with, null returns false 270 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 271 * @see java.lang.Float#floatToIntBits(float) 272 */ 273 @Override 274 public boolean equals(Object obj) { 275 return (obj instanceof MutableFloat) 276 && (Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value)); 277 } 278 279 /** 280 * Returns a suitable hash code for this mutable. 281 * 282 * @return a suitable hash code 283 */ 284 @Override 285 public int hashCode() { 286 return Float.floatToIntBits(value); 287 } 288 289 //----------------------------------------------------------------------- 290 /** 291 * Compares this mutable to another in ascending order. 292 * 293 * @param other the other mutable to compare to, not null 294 * @return negative if this is less, zero if equal, positive if greater 295 */ 296 public int compareTo(MutableFloat other) { 297 float anotherVal = other.value; 298 return Float.compare(value, anotherVal); 299 } 300 301 //----------------------------------------------------------------------- 302 /** 303 * Returns the String value of this mutable. 304 * 305 * @return the mutable value as a string 306 */ 307 @Override 308 public String toString() { 309 return String.valueOf(value); 310 } 311 312 }