1 package org.opentrafficsim.core.gtu;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.Acceleration;
6 import org.djunits.value.vdouble.scalar.Length;
7 import org.djunits.value.vdouble.scalar.Speed;
8 import org.djutils.exceptions.Throw;
9 import org.opentrafficsim.core.distributions.ConstantGenerator;
10 import org.opentrafficsim.core.distributions.Generator;
11
12
13
14
15
16
17
18
19
20
21 public class GtuTemplate implements Serializable, Generator<GtuCharacteristics>
22 {
23
24 private static final long serialVersionUID = 20141230L;
25
26
27 private final GtuType gtuType;
28
29
30 private final Generator<Length> lengthGenerator;
31
32
33 private final Generator<Length> widthGenerator;
34
35
36 private final Generator<Speed> maximumSpeedGenerator;
37
38
39 private final Generator<Acceleration> maxAcceleration;
40
41
42 private final Generator<Acceleration> maxDeceleration;
43
44
45
46
47
48
49
50
51 public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator, final Generator<Length> widthGenerator,
52 final Generator<Speed> maximumSpeedGenerator) throws NullPointerException
53 {
54 this(gtuType, lengthGenerator, widthGenerator, maximumSpeedGenerator,
55 new ConstantGenerator<>(Acceleration.instantiateSI(3.0)),
56 new ConstantGenerator<>(Acceleration.instantiateSI(-8.0)));
57 }
58
59
60
61
62
63
64
65
66
67
68 public GtuTemplate(final GtuType gtuType, final Generator<Length> lengthGenerator, final Generator<Length> widthGenerator,
69 final Generator<Speed> maximumSpeedGenerator, final Generator<Acceleration> maximumAccelerationGenerator,
70 final Generator<Acceleration> maximumDecelerationGenerator) throws NullPointerException
71 {
72 Throw.whenNull(gtuType, "gtuType is null");
73 Throw.whenNull(lengthGenerator, "lengthGenerator is null");
74 Throw.whenNull(widthGenerator, "widthGenerator is null");
75 Throw.whenNull(maximumSpeedGenerator, "maximumSpeedGenerator is null");
76 Throw.whenNull(maximumAccelerationGenerator, "maximumAccelerationGenerator is null");
77 Throw.whenNull(maximumDecelerationGenerator, "maximumDecelerationGenerator is null");
78
79 this.gtuType = gtuType;
80 this.lengthGenerator = lengthGenerator;
81 this.widthGenerator = widthGenerator;
82 this.maximumSpeedGenerator = maximumSpeedGenerator;
83 this.maxAcceleration = maximumAccelerationGenerator;
84 this.maxDeceleration = maximumDecelerationGenerator;
85 }
86
87
88
89
90
91
92
93 public GtuTemplate copyForGtuType(final GtuType newGtuType)
94 {
95 return new GtuTemplate(newGtuType, this.lengthGenerator, this.widthGenerator, this.maximumSpeedGenerator,
96 this.maxAcceleration, this.maxDeceleration);
97 }
98
99
100
101
102
103 @Override
104 @SuppressWarnings("checkstyle:designforextension")
105 public GtuCharacteristics draw()
106 {
107 Acceleration acceleration = this.maxAcceleration.draw();
108 Acceleration deceleration = this.maxDeceleration.draw();
109 Throw.when(acceleration.si <= 0, IllegalArgumentException.class, "Acceleration should be above 0.");
110 Throw.when(deceleration.si >= 0, IllegalArgumentException.class, "Deceleration should be below 0.");
111 Length length = this.lengthGenerator.draw();
112 return new GtuCharacteristics(this.gtuType, length, this.widthGenerator.draw(), this.maximumSpeedGenerator.draw(),
113 acceleration, deceleration, length.times(0.75));
114 }
115
116
117
118
119 @SuppressWarnings("checkstyle:designforextension")
120 public GtuType getGtuType()
121 {
122 return this.gtuType;
123 }
124
125 @Override
126 @SuppressWarnings("checkstyle:designforextension")
127 public String toString()
128 {
129 return String.format("TemplateGTUType [%s, %s, %s, %s]", this.gtuType, this.lengthGenerator, this.widthGenerator,
130 this.maximumSpeedGenerator);
131 }
132
133 }