1 package org.opentrafficsim.road.gtu.generator.headway;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8
9 import org.djunits.value.vdouble.scalar.Duration;
10 import org.opentrafficsim.base.parameters.ParameterException;
11 import org.opentrafficsim.core.distributions.Generator;
12 import org.opentrafficsim.core.distributions.ProbabilityException;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class ListHeadways implements Generator<Duration>
26 {
27
28
29 private final BufferedReader reader;
30
31
32 private double prev = 0.0;
33
34
35
36
37
38 public ListHeadways(final String fileName)
39 {
40 try
41 {
42 this.reader = new BufferedReader(new FileReader(new File(fileName)));
43 }
44 catch (FileNotFoundException exception)
45 {
46 throw new RuntimeException(exception);
47 }
48 }
49
50
51 @Override
52 public Duration draw() throws ProbabilityException, ParameterException
53 {
54 try
55 {
56 String line = null;
57 do
58 {
59 line = this.reader.readLine();
60 if (null == line)
61 {
62 return null;
63 }
64 }
65 while (line.equals(""));
66 double when = Double.parseDouble(line);
67 if (when < this.prev)
68 {
69 throw new RuntimeException(
70 "Arrival times from file are not in chronological order (" + when + "<" + this.prev + ").");
71 }
72 Duration headway = Duration.createSI(when - this.prev);
73 this.prev = when;
74 return headway;
75 }
76 catch (NumberFormatException | IOException exception)
77 {
78 throw new RuntimeException("Unable to read arrival time from file.", exception);
79 }
80 }
81
82 }