KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > util > ModifiableInteger


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* ----------------------
26  * ModifiableInteger.java
27  * ----------------------
28  *
29  * (C) Copyright 2002-2004, by Barak Naveh and Contributors.
30  *
31  * Original Author: Barak Naveh
32  * Contributor(s): -
33  *
34  * $Id: ModifiableInteger.java 504 2006-07-03 02:37:26Z perfecthash $
35  *
36  * Changes
37  * -------
38  * 2004-05-27 : Initial version (BN);
39  *
40  */

41 package org.jgrapht.util;
42
43 /**
44  * The <code>ModifiableInteger</code> class wraps a value of the primitive type
45  * <code>int</code> in an object, similarly to {@link java.lang.Integer}. An
46  * object of type <code>ModifiableInteger</code> contains a single field whose
47  * type is <code>int</code>.
48  *
49  * <p>Unlike <code>java.lang.Integer</code>, the int value which the
50  * ModifiableInteger represents can be modified. It becomes useful when used
51  * together with the collection framework. For example, if you want to have a
52  * {@link java.util.List} of counters. You could use <code>Integer</code> but
53  * that would have became wasteful and inefficient if you frequently had to
54  * update the counters.</p>
55  *
56  * <p>WARNING: Because instances of this class are mutable, great care must be
57  * exercised if used as keys of a {@link java.util.Map} or as values in a {@link
58  * java.util.Set} in a manner that affects equals comparisons while the
59  * instances are keys in the map (or values in the set). For more see
60  * documentation of <code>Map</code> and <code>Set</code>.</p>
61  *
62  * @author Barak Naveh
63  * @since May 27, 2004
64  */

65 public class ModifiableInteger
66     extends Number JavaDoc
67     implements Comparable JavaDoc
68 {
69
70     //~ Static fields/initializers --------------------------------------------
71

72     private static final long serialVersionUID = 3618698612851422261L;
73
74     //~ Instance fields -------------------------------------------------------
75

76     /**
77      * The int value represented by this <code>ModifiableInteger</code>.
78      */

79     public int value;
80
81     //~ Constructors ----------------------------------------------------------
82

83     /**
84      * <b>!!! DON'T USE - Use the {@link #ModifiableInteger(int)} constructor
85      * instead !!!</b>
86      *
87      * <p>This constructor is for the use of java.beans.XMLDecoder
88      * deserialization. The constructor is marked as 'deprecated' to indicate to
89      * the programmer against using it by mistake.</p>
90      *
91      * @deprecated not really deprecated, just marked so to avoid mistaken use.
92      */

93     @Deprecated JavaDoc public ModifiableInteger()
94     {
95     }
96
97     /**
98      * Constructs a newly allocated <code>ModifiableInteger</code> object that
99      * represents the specified <code>int</code> value.
100      *
101      * @param value the value to be represented by the <code>
102      * ModifiableInteger</code> object.
103      */

104     public ModifiableInteger(int value)
105     {
106         this.value = value;
107     }
108
109     //~ Methods ---------------------------------------------------------------
110

111     /**
112      * Sets a new value for this modifiable integer.
113      *
114      * @param value the new value to set.
115      */

116     public void setValue(int value)
117     {
118         this.value = value;
119     }
120
121     /**
122      * Returns the value of this object, similarly to {@link #intValue()}. This
123      * getter is NOT redundant. It is used for serialization by
124      * java.beans.XMLEncoder.
125      *
126      * @return the value.
127      */

128     public int getValue()
129     {
130         return this.value;
131     }
132
133     /**
134      * Adds one to the value of this modifiable integer.
135      */

136     public void increment()
137     {
138         this.value++;
139     }
140
141     /**
142      * Subtracts one from the value of this modifiable integer.
143      */

144     public void decrement()
145     {
146         this.value--;
147     }
148
149     /**
150      * Compares two <code>ModifiableInteger</code> objects numerically.
151      *
152      * @param anotherInteger the <code>ModifiableInteger</code> to be compared.
153      *
154      * @return the value <code>0</code> if this <code>ModifiableInteger</code>
155      * is equal to the argument <code>ModifiableInteger</code>; a value
156      * less than <code>0</code> if this <code>ModifiableInteger</code>
157      * is numerically less than the argument <code>
158      * ModifiableInteger</code>; and a value greater than <code>0</code>
159      * if this <code>ModifiableInteger</code> is numerically greater
160      * than the argument <code>ModifiableInteger</code> (signed
161      * comparison).
162      */

163     public int compareTo(ModifiableInteger anotherInteger)
164     {
165         int thisVal = this.value;
166         int anotherVal = anotherInteger.value;
167
168         return (thisVal < anotherVal) ? -1 : ((thisVal == anotherVal) ? 0 : 1);
169     }
170
171     /**
172      * Compares this <code>ModifiableInteger</code> object to another object. If
173      * the object is an <code>ModifiableInteger</code>, this function behaves
174      * like <code>compareTo(Integer)</code>. Otherwise, it throws a <code>
175      * ClassCastException</code> (as <code>ModifiableInteger</code> objects are
176      * only comparable to other <code>ModifiableInteger</code> objects).
177      *
178      * @param o the <code>Object</code> to be compared.
179      *
180      * @return the value <code>0</code> if the argument is a <code>
181      * ModifiableInteger</code> numerically equal to this <code>
182      * ModifiableInteger</code>; a value less than <code>0</code> if the
183      * argument is a <code>ModifiableInteger</code> numerically greater
184      * than this <code>ModifiableInteger</code>; and a value greater
185      * than <code>0</code> if the argument is a <code>
186      * ModifiableInteger</code> numerically less than this <code>
187      * ModifiableInteger</code>.
188      *
189      * @see java.lang.Comparable#compareTo(java.lang.Object)
190      */

191     public int compareTo(Object JavaDoc o)
192     {
193         return compareTo((ModifiableInteger) o);
194     }
195
196     /**
197      * @see Number#doubleValue()
198      */

199     public double doubleValue()
200     {
201         return this.value;
202     }
203
204     /**
205      * Compares this object to the specified object. The result is <code>
206      * true</code> if and only if the argument is not <code>null</code> and is
207      * an <code>ModifiableInteger</code> object that contains the same <code>
208      * int</code> value as this object.
209      *
210      * @param o the object to compare with.
211      *
212      * @return <code>true</code> if the objects are the same; <code>false</code>
213      * otherwise.
214      */

215     public boolean equals(Object JavaDoc o)
216     {
217         if (o instanceof ModifiableInteger) {
218             return this.value == ((ModifiableInteger) o).value;
219         }
220
221         return false;
222     }
223
224     /**
225      * @see Number#floatValue()
226      */

227     public float floatValue()
228     {
229         return this.value;
230     }
231
232     /**
233      * Returns a hash code for this <code>ModifiableInteger</code>.
234      *
235      * @return a hash code value for this object, equal to the primitive <code>
236      * int</code> value represented by this <code>
237      * ModifiableInteger</code> object.
238      */

239     public int hashCode()
240     {
241         return this.value;
242     }
243
244     /**
245      * @see Number#intValue()
246      */

247     public int intValue()
248     {
249         return this.value;
250     }
251
252     /**
253      * @see Number#longValue()
254      */

255     public long longValue()
256     {
257         return this.value;
258     }
259
260     /**
261      * Returns an <code>Integer</code> object representing this <code>
262      * ModifiableInteger</code>'s value.
263      *
264      * @return an <code>Integer</code> representation of the value of this
265      * object.
266      */

267     public Integer JavaDoc toInteger()
268     {
269         return new Integer JavaDoc(this.value);
270     }
271
272     /**
273      * Returns a <code>String</code> object representing this <code>
274      * ModifiableInteger</code>'s value. The value is converted to signed
275      * decimal representation and returned as a string, exactly as if the
276      * integer value were given as an argument to the {@link
277      * java.lang.Integer#toString(int)} method.
278      *
279      * @return a string representation of the value of this object in
280      * base&nbsp;10.
281      */

282     public String JavaDoc toString()
283     {
284         return String.valueOf(this.value);
285     }
286 }
287
Popular Tags