Coverage details for edu.uci.ics.jung.utils.MutableDouble

LineHitsSource
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() {
34122        super();
35122        mDouble = 0;
36122    }
37  
38     /**
39      * Constructs a new MutableDouble with the input value.
40      */
4120292    public MutableDouble(double initialValue) {
4220292        setDoubleValue(initialValue);
4320292    }
44  
45     /**
46      * Returns the floor integer value, accomplished by casting the
47      * contained double to <code>int</code>.
48      */
49     public int intValue() {
5018        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() {
580        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() {
660        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() {
7469491        return mDouble;
75     }
76  
77     /**
78      * @see java.lang.Comparable
79      */
80     public int compareTo(java.lang.Object o) {
810        double thisVal = this.doubleValue();
820        double anotherVal = ((MutableDouble) o).doubleValue();
830        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) {
970        if ((obj != null) && (obj instanceof MutableDouble)) {
980            return doubleValue() == ((MutableDouble) obj).doubleValue();
99         }
1000        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() {
1110        long bits = Double.doubleToLongBits(mDouble);
1120        return (int) (bits ^ (bits >>> 32));
113     }
114  
115     /**
116      * Sets the double value to a new value.
117      */
118     public void setDoubleValue(double newDouble) {
11943614        mDouble = newDouble;
12043614    }
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) {
131632        mDouble += value;
132632        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) {
1440        mDouble -= value;
1450        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() {
1530        return String.valueOf(mDouble);
154     }
155  
156 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.