KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > test > MutableInteger


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot.test;
10
11 /**
12  * @version $Revision: 1.3 $
13  */

14 public class MutableInteger implements Comparable JavaDoc
15 {
16    private int value;
17
18    public MutableInteger(int value)
19    {
20       this.value = value;
21    }
22
23    public int get()
24    {
25       return value;
26    }
27
28    public int hashCode()
29    {
30       return value;
31    }
32
33    public boolean equals(Object JavaDoc obj)
34    {
35       if (obj == null) return false;
36       if (obj == this) return true;
37       try
38       {
39          return get() == ((MutableInteger)obj).get();
40       }
41       catch (ClassCastException JavaDoc x)
42       {
43       }
44       return false;
45    }
46
47    public int compareTo(Object JavaDoc obj)
48    {
49       if (obj == null) return 1;
50       if (obj == this) return 0;
51
52       int thisValue = get();
53       int otherValue = ((MutableInteger)obj).get();
54       if (thisValue > otherValue) return 1;
55       if (thisValue < otherValue) return -1;
56       return 0;
57    }
58
59    public void set(int value)
60    {
61       this.value = value;
62    }
63
64    public String JavaDoc toString()
65    {
66       return String.valueOf(get());
67    }
68 }
69
Popular Tags