1 8 9 package foxtrot.test; 10 11 14 public class MutableInteger implements Comparable 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 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 x) 42 { 43 } 44 return false; 45 } 46 47 public int compareTo(Object 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 toString() 65 { 66 return String.valueOf(get()); 67 } 68 } 69 | Popular Tags |