1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.org.apache.commons.lang.mutable;
|
---|
18 |
|
---|
19 | import agents.org.apache.commons.lang.math.NumberUtils;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * A mutable <code>float</code> wrapper.
|
---|
23 | *
|
---|
24 | * @see Float
|
---|
25 | * @since 2.1
|
---|
26 | * @author Apache Software Foundation
|
---|
27 | * @version $Id: MutableFloat.java 905707 2010-02-02 16:59:59Z niallp $
|
---|
28 | */
|
---|
29 | public class MutableFloat extends Number implements Comparable, Mutable {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Required for serialization support.
|
---|
33 | *
|
---|
34 | * @see java.io.Serializable
|
---|
35 | */
|
---|
36 | private static final long serialVersionUID = 5787169186L;
|
---|
37 |
|
---|
38 | /** The mutable value. */
|
---|
39 | private float value;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructs a new MutableFloat with the default value of zero.
|
---|
43 | */
|
---|
44 | public MutableFloat() {
|
---|
45 | super();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs a new MutableFloat with the specified value.
|
---|
50 | *
|
---|
51 | * @param value the initial value to store
|
---|
52 | */
|
---|
53 | public MutableFloat(float value) {
|
---|
54 | super();
|
---|
55 | this.value = value;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Constructs a new MutableFloat with the specified value.
|
---|
60 | *
|
---|
61 | * @param value the initial value to store, not null
|
---|
62 | * @throws NullPointerException if the object is null
|
---|
63 | */
|
---|
64 | public MutableFloat(Number value) {
|
---|
65 | super();
|
---|
66 | this.value = value.floatValue();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Constructs a new MutableFloat parsing the given string.
|
---|
71 | *
|
---|
72 | * @param value the string to parse, not null
|
---|
73 | * @throws NumberFormatException if the string cannot be parsed into a float
|
---|
74 | * @since 2.5
|
---|
75 | */
|
---|
76 | public MutableFloat(String value) throws NumberFormatException {
|
---|
77 | super();
|
---|
78 | this.value = Float.parseFloat(value);
|
---|
79 | }
|
---|
80 |
|
---|
81 | //-----------------------------------------------------------------------
|
---|
82 | /**
|
---|
83 | * Gets the value as a Float instance.
|
---|
84 | *
|
---|
85 | * @return the value as a Float, never null
|
---|
86 | */
|
---|
87 | public Object getValue() {
|
---|
88 | return new Float(this.value);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Sets the value.
|
---|
93 | *
|
---|
94 | * @param value the value to set
|
---|
95 | */
|
---|
96 | public void setValue(float value) {
|
---|
97 | this.value = value;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Sets the value from any Number instance.
|
---|
102 | *
|
---|
103 | * @param value the value to set, not null
|
---|
104 | * @throws NullPointerException if the object is null
|
---|
105 | * @throws ClassCastException if the type is not a {@link Number}
|
---|
106 | */
|
---|
107 | public void setValue(Object value) {
|
---|
108 | setValue(((Number) value).floatValue());
|
---|
109 | }
|
---|
110 |
|
---|
111 | //-----------------------------------------------------------------------
|
---|
112 | /**
|
---|
113 | * Checks whether the float value is the special NaN value.
|
---|
114 | *
|
---|
115 | * @return true if NaN
|
---|
116 | */
|
---|
117 | public boolean isNaN() {
|
---|
118 | return Float.isNaN(value);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Checks whether the float value is infinite.
|
---|
123 | *
|
---|
124 | * @return true if infinite
|
---|
125 | */
|
---|
126 | public boolean isInfinite() {
|
---|
127 | return Float.isInfinite(value);
|
---|
128 | }
|
---|
129 |
|
---|
130 | //-----------------------------------------------------------------------
|
---|
131 | /**
|
---|
132 | * Increments the value.
|
---|
133 | *
|
---|
134 | * @since Commons Lang 2.2
|
---|
135 | */
|
---|
136 | public void increment() {
|
---|
137 | value++;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Decrements the value.
|
---|
142 | *
|
---|
143 | * @since Commons Lang 2.2
|
---|
144 | */
|
---|
145 | public void decrement() {
|
---|
146 | value--;
|
---|
147 | }
|
---|
148 |
|
---|
149 | //-----------------------------------------------------------------------
|
---|
150 | /**
|
---|
151 | * Adds a value to the value of this instance.
|
---|
152 | *
|
---|
153 | * @param operand the value to add, not null
|
---|
154 | * @since Commons Lang 2.2
|
---|
155 | */
|
---|
156 | public void add(float operand) {
|
---|
157 | this.value += operand;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Adds a value to the value of this instance.
|
---|
162 | *
|
---|
163 | * @param operand the value to add, not null
|
---|
164 | * @throws NullPointerException if the object is null
|
---|
165 | * @since Commons Lang 2.2
|
---|
166 | */
|
---|
167 | public void add(Number operand) {
|
---|
168 | this.value += operand.floatValue();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Subtracts a value from the value of this instance.
|
---|
173 | *
|
---|
174 | * @param operand the value to subtract
|
---|
175 | * @since Commons Lang 2.2
|
---|
176 | */
|
---|
177 | public void subtract(float operand) {
|
---|
178 | this.value -= operand;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Subtracts a value from the value of this instance.
|
---|
183 | *
|
---|
184 | * @param operand the value to subtract, not null
|
---|
185 | * @throws NullPointerException if the object is null
|
---|
186 | * @since Commons Lang 2.2
|
---|
187 | */
|
---|
188 | public void subtract(Number operand) {
|
---|
189 | this.value -= operand.floatValue();
|
---|
190 | }
|
---|
191 |
|
---|
192 | //-----------------------------------------------------------------------
|
---|
193 | // shortValue and bytValue rely on Number implementation
|
---|
194 | /**
|
---|
195 | * Returns the value of this MutableFloat as an int.
|
---|
196 | *
|
---|
197 | * @return the numeric value represented by this object after conversion to type int.
|
---|
198 | */
|
---|
199 | public int intValue() {
|
---|
200 | return (int) value;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Returns the value of this MutableFloat as a long.
|
---|
205 | *
|
---|
206 | * @return the numeric value represented by this object after conversion to type long.
|
---|
207 | */
|
---|
208 | public long longValue() {
|
---|
209 | return (long) value;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Returns the value of this MutableFloat as a float.
|
---|
214 | *
|
---|
215 | * @return the numeric value represented by this object after conversion to type float.
|
---|
216 | */
|
---|
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 | public double doubleValue() {
|
---|
227 | return value;
|
---|
228 | }
|
---|
229 |
|
---|
230 | //-----------------------------------------------------------------------
|
---|
231 | /**
|
---|
232 | * Gets this mutable as an instance of Float.
|
---|
233 | *
|
---|
234 | * @return a Float instance containing the value from this mutable, never null
|
---|
235 | */
|
---|
236 | public Float toFloat() {
|
---|
237 | return new Float(floatValue());
|
---|
238 | }
|
---|
239 |
|
---|
240 | //-----------------------------------------------------------------------
|
---|
241 | /**
|
---|
242 | * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
|
---|
243 | * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
|
---|
244 | * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
|
---|
245 | * purpose, two float values are considered to be the same if and only if the method
|
---|
246 | * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
|
---|
247 | * <p>
|
---|
248 | * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
|
---|
249 | * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
|
---|
250 | *
|
---|
251 | * <pre>
|
---|
252 | * f1.floatValue() == f2.floatValue()
|
---|
253 | * </pre>
|
---|
254 | *
|
---|
255 | * </blockquote>
|
---|
256 | * <p>
|
---|
257 | * also has the value <code>true</code>. However, there are two exceptions:
|
---|
258 | * <ul>
|
---|
259 | * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
|
---|
260 | * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
|
---|
261 | * the value <code>false</code>.
|
---|
262 | * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
|
---|
263 | * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
|
---|
264 | * <code>0.0f==-0.0f</code> has the value <code>true</code>.
|
---|
265 | * </ul>
|
---|
266 | * This definition allows hashtables to operate properly.
|
---|
267 | *
|
---|
268 | * @param obj the object to compare with, null returns false
|
---|
269 | * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
|
---|
270 | * @see java.lang.Float#floatToIntBits(float)
|
---|
271 | */
|
---|
272 | public boolean equals(Object obj) {
|
---|
273 | return (obj instanceof MutableFloat)
|
---|
274 | && (Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value));
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Returns a suitable hash code for this mutable.
|
---|
279 | *
|
---|
280 | * @return a suitable hash code
|
---|
281 | */
|
---|
282 | public int hashCode() {
|
---|
283 | return Float.floatToIntBits(value);
|
---|
284 | }
|
---|
285 |
|
---|
286 | //-----------------------------------------------------------------------
|
---|
287 | /**
|
---|
288 | * Compares this mutable to another in ascending order.
|
---|
289 | *
|
---|
290 | * @param obj the other mutable to compare to, not null
|
---|
291 | * @return negative if this is less, zero if equal, positive if greater
|
---|
292 | */
|
---|
293 | public int compareTo(Object obj) {
|
---|
294 | MutableFloat other = (MutableFloat) obj;
|
---|
295 | float anotherVal = other.value;
|
---|
296 | return NumberUtils.compare(value, anotherVal);
|
---|
297 | }
|
---|
298 |
|
---|
299 | //-----------------------------------------------------------------------
|
---|
300 | /**
|
---|
301 | * Returns the String value of this mutable.
|
---|
302 | *
|
---|
303 | * @return the mutable value as a string
|
---|
304 | */
|
---|
305 | public String toString() {
|
---|
306 | return String.valueOf(value);
|
---|
307 | }
|
---|
308 |
|
---|
309 | }
|
---|