package com.evolute.adt; public class Pair implements Comparable> { private final CARCLASS left; private final CDRCLASS right; public Pair( CARCLASS leftValue, CDRCLASS rightValue ) { this.left = leftValue; this.right = rightValue; } public CARCLASS getLeft() { return left; } public CDRCLASS getRight() { return right; } @SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { boolean result = obj != null; if( result ) { if( obj instanceof Pair ) { result = ( left == null ? (((Pair) obj).left == null ) : (left.equals(((Pair) obj).left)) ); result &= ( right == null ? (((Pair) obj).right == null ) : (right.equals(((Pair) obj).right)) ); } else { result = false; } } return result; } @Override public int compareTo(Pair pair) { int result = compare( left, pair == null ? null : pair.left ); if( result == 0 ) { result = compare( right, pair == null ? null : pair.right ); } return result; } @SuppressWarnings("unchecked") private int compare( Object a, Object b ) { int result = a == null ? (b == null ? 0 : -1 ) : ( b == null ? 1 : 0 ); if( a != null && b != null ) { if( a.getClass().equals( b.getClass() ) && (a instanceof Comparable) ) { ((Comparable)a).compareTo(b); } else { result = compare(a.toString(),b.toString()); } } return result; } @Override public int hashCode() { int result = super.hashCode(); if( left != null ) { result = left.hashCode(); if( right != null ) { result ^= right.hashCode(); } } else if( right != null ) { result = right.hashCode(); } return result; } @Override public String toString() { return "<"+left+","+right+">"; } }