001package basics.mutabledata; 002// Do not override equals or hashCode for mutable data. 003// Do not make mutable data Comparable. 004final public class MutPair<S,T> 005implements Cloneable 006{ 007 private S x; 008 private T y; 009 public MutPair() { } 010 public MutPair(S x, T y) { this.x = x; this.y = y; } 011 public void setFirst(S x) { this.x = x; } 012 public void setSecond(T y) { this.y = y; } 013 public S first() { return x; } 014 public T second() { return y; } 015 public String toString() { return "MutPair(" + x + "," + y + ")"; } 016 // Clone does not play nice with generics because it returns Object. 017 // Deep clone is difficult to implement generically because: 018 // (1) cast to MutPair is allowed, not to MutPair<S,T>; we need the 019 // latter in order to get at the fields. 020 // (2) Cloneable type does not have public clone method, so we 021 // cannot call x.clone() or y.clone(); this would be true even 022 // if we had "MutPair<S extends Cloneable,T extends Cloneable>" 023 public Object clone() { 024 try { 025 return super.clone(); 026 } catch (CloneNotSupportedException e) { 027 throw new InternalError(); 028 } 029 } 030}