package rpi.goldsd.container;

import java.util.Enumeration;

/**
 * The <tt>AlphabetSequence</tt> class implements the <tt>Sequence</tt>
 * interface by providing <tt>sequence()</tt> methods that generate
 * enumerations of <tt>Char</tt> 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 <tt>Char</tt> 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
   * <tt>c</tt> value.
   * @param c the start of the generated sequence.
   * @return an enumeration of sequential <tt>Char</tt> objects starting at
   *         the value of <tt>c</tt>.
   */
  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 <tt>Char</tt> object representing the start of
   *                   the generated sequence.
   * @return an enumeration of sequential <tt>Char</tt> objects.
   * @exception IllegalArgumentException if the <tt>startValue</tt> argument
   *            is not of type <tt>Char</tt>, or if the given <tt>Char</tt>
   *            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) );
      }
    } );
  }
}

