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 | * 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 | */ | |
31 | 2 | public MutableInteger() { |
32 | 2 | setInteger(0); |
33 | 2 | } |
34 | ||
35 | /** | |
36 | * Returns the integer value of this object. | |
37 | */ | |
38 | public int intValue() { | |
39 | 4398 | return mInteger; |
40 | } | |
41 | ||
42 | /** | |
43 | * Returns the integer value of this object, expressed as a long. | |
44 | */ | |
45 | public long longValue() { | |
46 | 0 | return mInteger; |
47 | } | |
48 | ||
49 | /** | |
50 | * Returns the integer value of this object, expressed as a float. | |
51 | */ | |
52 | public float floatValue() { | |
53 | 0 | return mInteger; |
54 | } | |
55 | ||
56 | /** | |
57 | * Returns the integer value of this object, expressed as a double. | |
58 | */ | |
59 | public double doubleValue() { | |
60 | 0 | 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) { | |
72 | 89 | mInteger += value; |
73 | 89 | 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) { | |
85 | 89 | mInteger -= value; |
86 | 89 | return this; |
87 | } | |
88 | ||
89 | 911 | public MutableInteger(int initialValue) { |
90 | 911 | setInteger(initialValue); |
91 | 911 | } |
92 | ||
93 | /** | |
94 | * @see java.lang.Comparable | |
95 | */ | |
96 | public int compareTo(java.lang.Object o) { | |
97 | 0 | int thisVal = this.intValue(); |
98 | 0 | int anotherVal = ((MutableInteger) o).intValue(); |
99 | 0 | 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) { | |
113 | 2 | if ((obj != null) && (obj instanceof MutableInteger)) { |
114 | 2 | return intValue() == ((MutableInteger) obj).intValue(); |
115 | } | |
116 | 0 | 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() { | |
127 | 14 | 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) { | |
134 | 915 | mInteger = newInteger; |
135 | 915 | } |
136 | ||
137 | /** | |
138 | * Adds one to the contained integer value. | |
139 | * @return this, to assist in chaining. | |
140 | */ | |
141 | public MutableInteger increment() { | |
142 | 0 | mInteger++; |
143 | 0 | return this; |
144 | } | |
145 | ||
146 | /** | |
147 | * Subtracts one from the contained integer value. | |
148 | ||
149 | */ | |
150 | public void decrement() { | |
151 | 0 | mInteger--; |
152 | 0 | } |
153 | ||
154 | public String toString() { | |
155 | 0 | return String.valueOf(mInteger); |
156 | } | |
157 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |