package rpi.goldsd.container; import java.util.Enumeration; /** * The AlphabetSequence class implements the Sequence * interface by providing sequence() methods that generate * enumerations of Char objects. Both uppercase and lowercase * letters may be represented. * * @see Sequence * @version 1.0, 4/16/98 * @author David Goldschmidt */ public class AlphabetSequence implements Sequence { /** * Returns an enumeration of sequential characters starting at 'a' * and ending at 'z'. * @return an enumeration of sequential Char objects with values * starting at 'a' and ending at 'z'. */ public Enumeration sequence() { return sequence( new Char( 'a' ) ); } /** * Returns an enumeration of sequential characters starting at the given * c value. * @param c the start of the generated sequence. * @return an enumeration of sequential Char objects starting at * the value of c. */ public Enumeration sequence( char c ) { return sequence( new Char( c ) ); } /** * Returns an enumeration of sequential characters starting at the given * start character. The sequence continues to either 'z' or 'Z', depending * on the given start character. * @param startValue the Char object representing the start of * the generated sequence. * @return an enumeration of sequential Char objects. * @exception IllegalArgumentException if the startValue argument * is not of type Char, or if the given Char * is not in a valid range 'a'-'z' or 'A'-'Z'. */ public Enumeration sequence( final Object startValue ) throws IllegalArgumentException { if ( ! ( startValue instanceof Char ) ) throw new IllegalArgumentException( "Start value must be of type Char." ); if ( ! Character.isLetter( ((Char)startValue).value ) ) new IllegalArgumentException( "Start value must be a letter." ); return ( new Enumeration() { private char x = ((Char)startValue).value; { x--; } // This is an instance initializer ... public boolean hasMoreElements() { return ( x != 'z' && x != 'Z' ); } public Object nextElement() { if ( x != 'z' && x != 'Z' ) x++; return ( new Char(x) ); } } ); } }