package jas.example.dim;

import hep.analysis.*;
import jas.jds.module.*;
import java.util.*;

public class RandomEventSource implements EventSource
{
   private int nEvents;
   private int seed;
   private int n; // Current event counter
   private RandomEventImpl theEvent = new RandomEventImpl();

   /**
    * Public constructor for the RandomEventSource
    * @param desc A String describing the EventSource to be opened
    */
   public RandomEventSource(String desc)
   {
      /* 
       * We interpret the input string to contain blank delimited fields 
       * specifying the number of events and seed.
       */
      StringTokenizer st = new StringTokenizer(desc);
      nEvents = Integer.parseInt(st.nextToken());
      seed = Integer.parseInt(st.nextToken());
   }
   /**
    * The essential method that returns the next Event.
    */
   public EventData getNextEvent() throws EndOfDataException
   {
      if (++n >= nEvents) throw new EndOfDataException();
      return theEvent;
   }
   /**
    * Returns the public class that will be returned by getNextEvent()
    */
   public Class getEventDataClass()
   {
      return RandomEvent.class;
   }
   public void close()
   {
      // Nothing to do in this case
   }
   public int getTotalNumberOfEvents()
   {
       return nEvents;
   }
   public String getName()
   {
      return "Random Events seed="+seed;
   }
   public void beforeFirstEvent()
   {
      theEvent.setSeed(seed);
   }
   public void afterLastEvent()
   {
   }
   /**
    * This internal class provides the implementation of RandomEvent which is 
    * actually returned by the getNextEvent method.
    */
   private class RandomEventImpl implements RandomEvent
   {
      public double getValue()
      {
         return random.nextDouble();	
      }
      void setSeed(long seed)
      {
         random.setSeed(seed);
      }
      private Random random = new Random();
   }
}