You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.9 KiB

package com.evolute.adt;
public class Pair<CARCLASS extends Object, CDRCLASS extends Object> implements Comparable<Pair<? extends CARCLASS,? extends CDRCLASS>>
{
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<? extends CARCLASS, ? extends CDRCLASS> 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+">";
}
}