/*
*
* Copyright (c) 2007
* Adrian Michel
* http://www.tradery.com
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Adrian Michel makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
package com.tradery.collections;
import com.tradery.contract.Contract;
/**
* Models a pair of integer values.
*/
public class IntegerPair
{
private int _first;
private int _second;
/**
* Default constructor - sets both values to 0;
*/
protected IntegerPair()
{
_first = 0;
_second = 0;
}
/**
* Copy constructor - copies the values from another IntegerPair into the current
*
* @param ip The IntegerPair object to be copied
*/
protected IntegerPair( IntegerPair ip )
{
_first = ip.first();
_second = ip.second();
}
/**
* Constructor taking two integer
*
* @param first The first value
* @param second The second value
*/
public IntegerPair( int first, int second )
{
_first = first;
_second = second;
}
/**
* Constructor taking two Integer objects
*
* @param first The first Integer object
* @param second The second Integer object
*/
public IntegerPair( Integer first, Integer second )
{
if( Contract.REQUIRE )
{
Contract.require( first != null, "First Integer in the IntegerPair constructor cannot be null" );
Contract.require( second != null, "Second Integer in the IntegerPair constructor cannot be null" );
}
_first = first.intValue();
_second = second.intValue();
}
/**
* Returns the first value in the pair, as an int
*
* @return the first value
*/
public int first()
{
return _first;
}
/**
* Returns the second value in the pair as an int
*
* @return the second value
*/
public int second()
{
return _second;
}
/**
* Generates a string representation of the IntegerPair object, which
* is the two integers separated by ",", for example "1,2"
*
* @return The string representation
*/
public String toString()
{
return ( new Integer( _first ) ).toString() + "," + ( new Integer( _second ) ).toString();
}
/**
* Allows setting the first value in the pair from an int
*
* @param first The first value to be set
*/
protected void setFirst( int first )
{
_first = first;
}
/**
* Allows setting the second value in the pair from an int
*
* @param second The second value to be set
*/
protected void setSecond( int second )
{
_second = second;
}
/**
* Allows setting the first value in the pair from an Integer
*
* @param first The first value to be set
*/
protected void setFirst( Integer first )
{
_first = first.intValue();
}
/**
* Allows setting the second value in the pair from an Integer
*
* @param second The second value to be set
*/
protected void setSecond( Integer second )
{
_second = second.intValue();
}
/**
* Determines if the current IntegerPair is equal to another IntegerPair object
*
* Equal means that the two values in the pair are equal.
*
* @param pair The IntegerPair with which the current object needs to be compared.
* @return true if the two objects are equal, false otherwise
*/
public boolean equals( Object pair )
{
if( pair == null )
return false;
else
return _first == ( ( IntegerPair) pair ).first() && _second == ( (IntegerPair) pair).second();
}
public int hashCode() { return _first + _second;}
/**
* Clones the current object
*
* @return The new object cloned after the current object
*/
public Object clone()
{
return new IntegerPair( this );
}
}