View Javadoc

1   package org.lcsim.util.loop;
2   
3   import java.io.IOException;
4   
5   import hep.physics.event.HEPEvent;
6   import hep.physics.event.generator.EventGenerator;
7   import org.freehep.record.source.AbstractRecordSource;
8   import org.freehep.record.source.NoSuchRecordException;
9   
10  /**
11   *
12   * @author Tony Johnson
13   */
14  public class EventGeneratorRecordSource extends AbstractRecordSource {
15  
16    private EventGenerator generator;
17    private HEPEvent current;
18  
19    public EventGeneratorRecordSource(EventGenerator generator, String name) {
20      super(name);
21      this.generator = generator;
22    }
23    
24    public boolean supportsRewind() {
25      return true;
26    }
27  
28    public boolean hasRewind() {
29      return current != null;
30    }
31  
32    public void rewind() throws IOException {
33      generator.reset();
34      current = null;
35    }
36  
37    public Class<?> getRecordClass() {
38      return HEPEvent.class;
39    }
40  
41    public Object getCurrentRecord() throws IOException {
42      if (current == null) throw new IllegalStateException();
43      return current;
44    }
45  
46    public boolean supportsNext() {
47      return true;
48    }
49  
50    public void next() throws IOException, NoSuchRecordException {
51      current = generator.generate();
52      if (current == null) throw new NoSuchRecordException();
53    }
54  
55    EventGenerator getGenerator() {
56      return generator;
57    }
58  }