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

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  * The <code>MutableInteger</code> class wraps a value of the primitive type <code>int</code> in a mutable object. An object of type <code>MutableInteger</code> contains a single field whose type is <code>int</code>.
14  * This allows the system to not pile up large
15  * sets of temporary "numbers" and reduces object creation when doing math.
16  * <p>
17  * In addition, this class provides several methods for converting a <code>int</code> to a String and a String to a <code>int</code>.
18  * <p>
19  * Warning: It is important to not modify Mutable values when they are in a
20  * sorted data structure, such as a TreeSet! They will fall out of order and
21  * cause the set to be inconsistent
22  *
23  * @author Scott White
24  */
25 public class MutableInteger extends Number implements Comparable {
26     private int mInteger;
27  
28     /**
29      * Constructs a new MutableInteger with default value 0.
30      */
312    public MutableInteger() {
322        setInteger(0);
332    }
34  
35     /**
36      * Returns the integer value of this object.
37      */
38     public int intValue() {
394398        return mInteger;
40     }
41  
42     /**
43      * Returns the integer value of this object, expressed as a long.
44      */
45     public long longValue() {
460        return mInteger;
47     }
48  
49     /**
50      * Returns the integer value of this object, expressed as a float.
51      */
52     public float floatValue() {
530        return mInteger;
54     }
55  
56     /**
57      * Returns the integer value of this object, expressed as a double.
58      */
59     public double doubleValue() {
600        return mInteger;
61     }
62  
63     /**
64      * Increases the <tt>int</tt>'s value by <tt>value</tt>.
65      * The object will, after this call, contain the value
66      * <code>(int) ( intValue() + value ) </code>.
67      *
68      * @param value the amount to add
69      * @return this object, for convenience in chaining operations
70      */
71     public MutableInteger add(double value) {
7289        mInteger += value;
7389        return this;
74     }
75  
76     /**
77      * Increases the <tt>int</tt>'s value by <tt>value</tt>.
78      * The object will, after this call, contain the value
79      * <code>(int) ( intValue() - value ) </code>.
80      *
81      * @param value the amount to subtract
82      * @return this object, for convenience in chaining operations
83      */
84     public MutableInteger subtract(double value) {
8589        mInteger -= value;
8689        return this;
87     }
88  
89911    public MutableInteger(int initialValue) {
90911        setInteger(initialValue);
91911    }
92  
93     /**
94      * @see java.lang.Comparable
95      */
96     public int compareTo(java.lang.Object o) {
970        int thisVal = this.intValue();
980        int anotherVal = ((MutableInteger) o).intValue();
990        return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
100     }
101  
102     /**
103      * Compares this object to the specified object.
104      * The result is <code>true</code> if and only if the argument is not
105      * <code>null</code> and is an <code>MutableInteger</code> object that contains
106      * the same <code>int</code> value as this object.
107      *
108      * @param obj the object to compare with.
109      * @return <code>true</code> if the objects are the same;
110      * <code>false</code> otherwise.
111      */
112     public boolean equals(Object obj) {
1132        if ((obj != null) && (obj instanceof MutableInteger)) {
1142            return intValue() == ((MutableInteger) obj).intValue();
115         }
1160        return false;
117     }
118  
119     /**
120      * Returns a hashcode for this Integer.
121      *
122      * @return a hash code value for this object, equal to the
123      * primitive <tt>int</tt> value represented by this
124      * <tt>MutableInteger</tt> object.
125      */
126     public int hashCode() {
12714        return GeneralUtils.hash(mInteger);
128     }
129  
130     /**
131      * Sets the value of this object to <tt>newInteger</tt>.
132      */
133     public void setInteger(int newInteger) {
134915        mInteger = newInteger;
135915    }
136  
137     /**
138      * Adds one to the contained integer value.
139      * @return this, to assist in chaining.
140      */
141     public MutableInteger increment() {
1420        mInteger++;
1430        return this;
144     }
145  
146     /**
147      * Subtracts one from the contained integer value.
148  
149      */
150     public void decrement() {
1510        mInteger--;
1520    }
153  
154     public String toString() {
1550        return String.valueOf(mInteger);
156     }
157 }

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.