package rpi.goldsd.container; /** * The Int class provides a wrapper-class for the primitive data * type int that may be used in various associative containers * by implementing the Hashable interface (note that the * Hashable interface is actually an extension of the * Comparable interface). The actual integer value * (the int instance variable) is a public instance variable. * * @see Comparable * @see Hashable * @version 1.2, 4/16/98 * @author David Goldschmidt */ public class Int implements Hashable { /** The smallest value of type Int. */ public final static int MIN_VALUE = Integer.MIN_VALUE; /** The largest value of type Int. */ public final static int MAX_VALUE = Integer.MAX_VALUE; /** * Initializes the underlying int primitive data type. * @param v the initial integer value. */ public Int( int v ) { value = v; } /** Constructs an Int object with a default value of 0. */ public Int() { value = 0; } /** * Returns true if this Int object is equal to the * given Comparable argument C, which must be of * type Int. * @param C the right-hand side of the comparison. * @return true if this Int object is equal to the * given Int object C; false otherwise. * @exception IllegalArgumentException if the Comparable argument * is not of type Int. */ public boolean isEqualTo( Comparable C ) throws IllegalArgumentException { if ( C instanceof Int ) return ( value == ((Int)C).value ); else throw new IllegalArgumentException( "Must be an Int type." ); } /** * Returns true if this Int object is less than the * given Comparable argument C, which must be of type * Int. * @param C the right-hand side of the comparison. * @return true if this Int object is less than the * given Int object C; false otherwise. * @exception IllegalArgumentException if the Comparable argument * is not of type Int. */ public boolean isLessThan( Comparable C ) throws IllegalArgumentException { if ( C instanceof Int ) return ( value < ((Int)C).value ); else throw new IllegalArgumentException( "Must be an Int type." ); } /** * The static isPrime() method is used to determine if * a given Int object is a prime number. * @param I the Int object to be tested. * @return true if the given Int object is prime; * false otherwise. */ public static final boolean isPrime( Int I ) { return isPrime( I.value ); } /** * The static isPrime() method is used to determine if * a given int value is a prime number. * @param i the int value to be tested. * @return true if the given int value is prime; * false otherwise. */ public static final boolean isPrime( int i ) { if ( i == 2 ) return true; if ( i < 2 || i % 2 == 0 ) return false; for ( int stop = i, j = 3 ; j < stop ; stop = i / j, j += 2 ) if ( i % j == 0 ) return false; return true; } /** * Returns the hash value of this Int object, which is simply * the underlying integer value. If a more complex hashing function is * required, this method may be overridden. * @return the hash value of this Int object. */ public int hash() { return value; } /** * Returns the hash value of this Int object, based on the * given hashtable size. * @param tableSize the size of the hashtable making use of * this Int object. * @return the hash value of this Int object. */ public int hash( int tableSize ) { return ( Math.abs( hash() ) % tableSize ); } /** * Displays this Int object in the form of a String * object. This method simply returns a String representation * of the underlying integer value. * @return a String representing this Int object. */ public String toString() { return ( "" + value ); } /** The underlying primitive data type. */ public int value; }