1 package net.sf.jhunlang.jmorph.util; 2 3 public class Pair 4 { 5 protected final Object a; 6 protected final Object b; 7 8 protected boolean computed; 9 protected int hash; 10 11 public Pair(Object a, Object b) 12 { 13 if (a == null || b == null) 14 { 15 throw new IllegalArgumentException("Neither component can be null"); 16 } 17 this.a = a; 18 this.b = b; 19 } 20 21 public Object getA() 22 { 23 return a; 24 } 25 26 public Object getB() 27 { 28 return b; 29 } 30 31 public synchronized int hashCode() 32 { 33 if (!computed) 34 { 35 hash = computeHashCode(); 36 computed = true; 37 } 38 return hash; 39 } 40 41 public boolean equals(Object o) 42 { 43 if (o == null || o.getClass() != getClass()) 44 { 45 return false; 46 } 47 return tellEquals(o); 48 } 49 50 protected boolean tellEquals(Object o) 51 { 52 Pair op = (Pair)o; 53 return hashCode() == op.hashCode() && 54 a.equals(op.getA()) && b.equals(op.getB()); 55 } 56 57 protected int computeHashCode() 58 { 59 return a.hashCode() * 31 + b.hashCode(); 60 } 61 62 public static String shorten(String s) 63 { 64 return s.substring(s.lastIndexOf('.') + 1); 65 } 66 67 public static String shorten(Class clz) 68 { 69 return shorten(clz.getName()); 70 } 71 72 public String contentString() 73 { 74 return a + ", " + b; 75 } 76 77 public String toString() 78 { 79 return shorten(getClass()) + "[" + contentString() + "]"; 80 } 81 }