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
43 this.reader = new BufferedReader(new FileReader(new File(fileName)));
44 }
45 catch (FileNotFoundException exception)
46 {
47 throw new RuntimeException(exception);
48 }
49 }
50
51
52 @Override
53 public Duration draw() throws ProbabilityException, ParameterException
54 {
55 try
56 {
57 String line = null;
58 do
59 {
60 line = this.reader.readLine();
61 if (null == line)
62 {
63 return null;
64 }
65 }
66 while (line.equals(""));
67 double when = Double.parseDouble(line);
68 if (when < this.prev)
69 {
70 throw new RuntimeException(
71 "Arrival times from file are not in chronological order (" + when + "<" + this.prev + ").");
72 }
73 Duration headway = Duration.createSI(when - this.prev);
74 this.prev = when;
75 return headway;
76 }
77 catch (NumberFormatException | IOException exception)
78 {
79 throw new RuntimeException("Unable to read arrival time from file.", exception);
80 }
81 }
82
83 }