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