Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
3 | * of California | |
4 | * All rights reserved. | |
5 | * | |
6 | * This software is open-source under the BSD license; see either | |
7 | * "license.txt" or | |
8 | * http://jung.sourceforge.net/license.txt for a description. | |
9 | */ | |
10 | package edu.uci.ics.jung.utils; | |
11 | ||
12 | ||
13 | /** | |
14 | * The <code>MutableDouble</code> class wraps a value of the primitive type <code>double</code> in a mutable object. An object of type <code>MutableDouble</code> contains a single field whose type is <code>double</code>. | |
15 | * This allows the system to not pile up large | |
16 | * sets of temporary "numbers" and reduces object creation when doing math. | |
17 | * <p> | |
18 | * In addition, this class provides several methods for converting a <code>double</code> to a String and a String to a <code>double</code>. | |
19 | * <p> | |
20 | * Warning: It is important to not modify Mutable values when they are in a | |
21 | * sorted data structure, such as a TreeSet! They will fall out of order and | |
22 | * cause the set to be inconsistent | |
23 | * | |
24 | * @author Scott White | |
25 | */ | |
26 | public class MutableDouble extends Number implements Comparable { | |
27 | private double mDouble; | |
28 | ||
29 | /** | |
30 | * Constructs a new MutableDouble with a default value of 0 | |
31 | * assigned | |
32 | */ | |
33 | public MutableDouble() { | |
34 | 122 | super(); |
35 | 122 | mDouble = 0; |
36 | 122 | } |
37 | ||
38 | /** | |
39 | * Constructs a new MutableDouble with the input value. | |
40 | */ | |
41 | 20292 | public MutableDouble(double initialValue) { |
42 | 20292 | setDoubleValue(initialValue); |
43 | 20292 | } |
44 | ||
45 | /** | |
46 | * Returns the floor integer value, accomplished by casting the | |
47 | * contained double to <code>int</code>. | |
48 | */ | |
49 | public int intValue() { | |
50 | 18 | return (int) mDouble; |
51 | } | |
52 | ||
53 | /** | |
54 | * Returns the floor integer value as a long, accomplished by casting the | |
55 | * contained double to <code>long</code>. | |
56 | */ | |
57 | public long longValue() { | |
58 | 0 | return (long) mDouble; |
59 | } | |
60 | ||
61 | /** | |
62 | * Returns the nearest float value, accomplished by casting the | |
63 | * contained double to <code>float</code>. | |
64 | */ | |
65 | public float floatValue() { | |
66 | 0 | return (float) mDouble; |
67 | } | |
68 | ||
69 | /** | |
70 | * Returns the value as a double, accomplished by returning the | |
71 | * primitive contained double. | |
72 | */ | |
73 | public double doubleValue() { | |
74 | 69491 | return mDouble; |
75 | } | |
76 | ||
77 | /** | |
78 | * @see java.lang.Comparable | |
79 | */ | |
80 | public int compareTo(java.lang.Object o) { | |
81 | 0 | double thisVal = this.doubleValue(); |
82 | 0 | double anotherVal = ((MutableDouble) o).doubleValue(); |
83 | 0 | return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); |
84 | } | |
85 | ||
86 | /** | |
87 | * Compares this object to the specified object. | |
88 | * The result is <code>true</code> if and only if the argument is not | |
89 | * <code>null</code> and is an <code>MutableDouble</code> object that contains | |
90 | * the same <code>double</code> value as this object. | |
91 | * | |
92 | * @param obj the object to compare with. | |
93 | * @return <code>true</code> if the objects are the same; | |
94 | * <code>false</code> otherwise. | |
95 | */ | |
96 | public boolean equals(Object obj) { | |
97 | 0 | if ((obj != null) && (obj instanceof MutableDouble)) { |
98 | 0 | return doubleValue() == ((MutableDouble) obj).doubleValue(); |
99 | } | |
100 | 0 | return false; |
101 | } | |
102 | ||
103 | /** | |
104 | * Returns a hashcode for this Integer. | |
105 | * | |
106 | * @return a hash code value for this object, equal to the | |
107 | * primitive <tt>int</tt> value represented by this | |
108 | * <tt>MutableDouble</tt> object. | |
109 | */ | |
110 | public int hashCode() { | |
111 | 0 | long bits = Double.doubleToLongBits(mDouble); |
112 | 0 | return (int) (bits ^ (bits >>> 32)); |
113 | } | |
114 | ||
115 | /** | |
116 | * Sets the double value to a new value. | |
117 | */ | |
118 | public void setDoubleValue(double newDouble) { | |
119 | 43614 | mDouble = newDouble; |
120 | 43614 | } |
121 | ||
122 | /** | |
123 | * Increases the <tt>double</tt>'s value by <tt>value</tt>. | |
124 | * The object will, after this call, contain the value | |
125 | * <code>doubleValue() + value</code>. | |
126 | * | |
127 | * @param value the amount to add | |
128 | * @return this object, for convenience in chaining operations | |
129 | */ | |
130 | public MutableDouble add(double value) { | |
131 | 632 | mDouble += value; |
132 | 632 | return this; |
133 | } | |
134 | ||
135 | /** | |
136 | * Decreases the <tt>double</tt>'s value by <tt>value</tt>. | |
137 | * The object will, after this call, contain the value | |
138 | * <code>doubleValue() - value</code>. | |
139 | * | |
140 | * @param value the amount to subtract | |
141 | * @return this object, for convenience in chaining operations | |
142 | */ | |
143 | public MutableDouble subtract(double value) { | |
144 | 0 | mDouble -= value; |
145 | 0 | return this; |
146 | } | |
147 | ||
148 | /** | |
149 | * Uses the default String converter to return the value of this | |
150 | * as a string. | |
151 | */ | |
152 | public String toString() { | |
153 | 0 | return String.valueOf(mDouble); |
154 | } | |
155 | ||
156 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |