/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.database;

/**
 * A class similar to Integer used to represent a primary key for a java object.
 * 
 * @author Jon Maber
 * @version 1
 */

public class PrimaryKey extends java.lang.Number
	{

	/**
	 * The actual int value that will be used for storage of this key.
	 * The storage system will have to use a 4 byte integer for the
	 * key.
	 */
	private int myint;

	/**
	 * PrimaryKey can be constructed with an int.
	 * 
	 * @param i The number used to represent this key in storage.
	 */
	
	public PrimaryKey( int i )
		{
		myint=i;
		}

	/**
	 * Outputs the int value as a string.
	 * @return A string created with Integer.toString()
	 */
	
	public String toString()
		{
		return Integer.toString( myint );
		}

	/**
	 * Retrieves the int value.
	 * @return The underlying int value.
	 */
		
	public int intValue()
		{
		return myint;
		}

	/**
	 * Retrieves the int value cast to a long.
	 * @return The int padded out to a long.
	 */
		
	public long longValue()
		{
		return myint;
		}

	/**
	 * Retrieves the int value cast to a double.
	 * @return A double.
	 */
	
	public double doubleValue()
		{
		return myint;
		}

	/**
	 * Retrieves the int value cast to a float.
	 * @return A float value.
	 */
	
	public float floatValue()
		{
		return myint;
		}

    /**
     * Returns a hashcode for this PrimaryKey.
     *
     * @return  A hash code value for this object. (Actually the int value.)
     */
    public int hashCode()
    	{
		return myint;
    	}

    /**
     * Compares this object to the specified object.
     * If this method wasn't included the PrimaryKeys
     * would be compared by reference instead and two
     * instances of the same PrimaryKey would be
     * considered unequal.  This is important for storing
     * PersistentOjbects in Hashtables etc.
     *
     * @param   obj   the object to compare with.
     * @return  <code>true</code> if the objects are the same;
     *          <code>false</code> otherwise.
     */
    public boolean equals(Object obj)
    	{
		if ((obj != null) && (obj instanceof PrimaryKey))
			{
	    	return myint == ((PrimaryKey)obj).intValue();
			}
		return false;
    	}
	}
 
