// file:    testEnumeration.java
// author:  keller
// purpose: testing the Enumeration interface, also
//          showing how an interface is similar to a base class
//
//          We demonstrate on an enumeration from java.util and also from
//          a home-grown enumeration, arrayEnumeration.

import java.util.*;

class testEnumeration
{
static int size = 10;				// used for testing

// test program

public static void main(String arg[])
  {
  testVector();
  testHomeGrown();
  }


// printOnePerLine prints one element of an enumeration per line

static void printOnePerLine(Enumeration e)
  {
  while( e.hasMoreElements() )
    {
    System.out.println(e.nextElement());
    }
  System.out.println();
  }


// test the Vector class

static void testVector()
  {
  Vector v = new Vector();
  for( int i = 0; i < size; i++ )
    v.addElement(new Integer(i*i));

  printOnePerLine(v.elements());
  }

// note: Vector does not implement Enumeration, but applying method
// elements() gives an object in a class which does. We don't really
// even refer to this class by its name.


// test the arrayEnumeration class

static void testHomeGrown()
  {
  Object a[] = new Object[size];		// allocate array

  for( int i = 0; i < size; i++ )
    a[i] = new Float(i*i*i);

  printOnePerLine(new arrayEnumeration(a));
  }

}


// An Enumeration which we define to enumerate an array of Object's

class arrayEnumeration implements Enumeration
{
Object a[];
int ptr = 0;

// create the enumeration from an array

arrayEnumeration(Object a[])
  {
  this.a = a;
  }


// give the next element in the enumeration

public Object nextElement()
  {
  return a[ptr++];
  }


// tell if there are more elements

public boolean hasMoreElements()
  {
  return ptr < a.length;
  }  
}

