package rpi.goldsd.container;
import java.util.Enumeration;
/**
* The IntSequence class implements the Sequence
* interface by providing sequence() methods that generate
* enumerations of Int objects. This class is generally
* used to provide a sequence-generator for unique key-values in
* hashtables and other associative containers.
*
* @see Sequence
* @version 1.2, 5/11/98
* @author David Goldschmidt
*/
public class IntSequence implements Sequence
{
/**
* Constructs an IntSequence object with the specified
* startValue and endValue default values.
* @param startValue used by the default sequence() method as the
* default start value.
* @param endValue used by all sequence() methods as the
* default end value.
*/
public IntSequence( int startValue, int endValue )
{
this.startValue = startValue;
this.endValue = endValue;
}
/**
* Constructs an IntSequence object with the specified
* startValue default value.
* @param startValue used by the default sequence() method as the
* default start value.
*/
public IntSequence( int startValue ) { this( startValue, Int.MAX_VALUE ); }
/** Constructs an IntSequence object. */
public IntSequence() { this( 1, Int.MAX_VALUE ); }
/** Default start value for the no-argument sequence() method. */
protected int startValue;
/** Default end value for all sequence() methods. */
protected int endValue;
/**
* Returns an enumeration of sequential integer values starting at 1.
* The hasMoreElements() method of the enumeration always
* returns true since the nextElement() method will
* always return the next Int object in the sequence. When
* the enumeration reaches the defined Int.MAX_VALUE value,
* the sequence "wraps around" to 1.
* @return an enumeration of sequential Int objects with values
* starting at 1.
*/
public Enumeration sequence() { return sequence( new Int( startValue ) ); }
/**
* Returns an enumeration of sequential integer values starting
* at the given start value i.
* @param i the start of the generated sequence.
* @return an enumeration of sequential integer values starting
* at the given start value i.
*/
public Enumeration sequence( int i ) { return sequence( new Int( i ) ); }
/**
* Returns an enumeration of sequential integer values starting
* at the given start value. The hasMoreElements() method
* of the enumeration always returns true since the
* nextElement() method will always return the next
* Int object in the sequence. When the enumeration
* reaches the defined Int.MAX_VALUE, the sequence "wraps
* around" to the given start value.
* @param startValue the Int object representing the start of
* the generated sequence.
* @return an enumeration of sequential Int objects.
* @exception IllegalArgumentException if the startValue argument
* is not of type Int.
*/
public Enumeration sequence( final Object startValue )
throws IllegalArgumentException
{
if ( ! ( startValue instanceof Int ) ) throw
new IllegalArgumentException( "Start value must be of type Int." );
return ( new Enumeration() {
private int x = ((Int)startValue).value - 1;
public boolean hasMoreElements() { return true; }
public Object nextElement()
{
if ( x == endValue ) x = ((Int)startValue).value;
else x++;
return ( new Int(x) );
}
} );
}
}