1 package org.opentrafficsim.road.gtu.lane.perception.headway;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.opentrafficsim.core.gtu.GTUException;
5
6 import nl.tudelft.simulation.language.Throw;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public abstract class AbstractHeadway implements Headway
22 {
23
24
25 private static final long serialVersionUID = 20170324L;
26
27
28 private final Length distance;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 private final Length overlapFront;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 private final Length overlapRear;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 private final Length overlap;
77
78
79
80
81
82
83
84
85
86 protected AbstractHeadway(final Length distance, final Length overlapFront, final Length overlap, final Length overlapRear)
87 throws GTUException
88 {
89 Throw.when(distance != null && (overlap != null || overlapFront != null || overlapRear != null), GTUException.class,
90 "overlap parameter cannot be null for front / rear headway");
91 this.distance = distance;
92
93 Throw.when(distance == null && (overlap == null || overlapFront == null || overlapRear == null), GTUException.class,
94 "overlap parameter cannot be null for parallel headway");
95 Throw.when(overlap != null && overlap.si < 0, GTUException.class, "overlap cannot be negative");
96 this.overlap = overlap;
97 this.overlapFront = overlapFront;
98 this.overlapRear = overlapRear;
99 }
100
101
102
103
104
105
106 public AbstractHeadway(final Length distance) throws GTUException
107 {
108 this(distance, null, null, null);
109 }
110
111
112
113
114
115
116
117
118 @SuppressWarnings("checkstyle:parameternumber")
119 public AbstractHeadway(final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
120 {
121 this(null, overlapFront, overlap, overlapRear);
122 }
123
124
125 @Override
126 public final Length getDistance()
127 {
128 return this.distance;
129 }
130
131
132 @Override
133 public final Length getOverlapFront()
134 {
135 return this.overlapFront;
136 }
137
138
139 @Override
140 public final Length getOverlapRear()
141 {
142 return this.overlapRear;
143 }
144
145
146 @Override
147 public final Length getOverlap()
148 {
149 return this.overlap;
150 }
151
152
153 @Override
154 public final boolean isAhead()
155 {
156 return this.distance != null && this.distance.si > 0.0;
157 }
158
159
160 @Override
161 public final boolean isBehind()
162 {
163 return this.distance != null && this.distance.si < 0.0;
164 }
165
166
167 @Override
168 public final boolean isParallel()
169 {
170 return this.overlap != null;
171 }
172
173
174 @SuppressWarnings("checkstyle:designforextension")
175 @Override
176 public int hashCode()
177 {
178 final int prime = 31;
179 int result = 1;
180 result = prime * result + ((this.distance == null) ? 0 : this.distance.hashCode());
181 result = prime * result + ((this.overlap == null) ? 0 : this.overlap.hashCode());
182 result = prime * result + ((this.overlapFront == null) ? 0 : this.overlapFront.hashCode());
183 result = prime * result + ((this.overlapRear == null) ? 0 : this.overlapRear.hashCode());
184 return result;
185 }
186
187
188 @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
189 @Override
190 public boolean equals(final Object obj)
191 {
192 if (this == obj)
193 return true;
194 if (obj == null)
195 return false;
196 if (getClass() != obj.getClass())
197 return false;
198 AbstractHeadway other = (AbstractHeadway) obj;
199 if (this.distance == null)
200 {
201 if (other.distance != null)
202 return false;
203 }
204 else if (!this.distance.equals(other.distance))
205 return false;
206 if (this.overlap == null)
207 {
208 if (other.overlap != null)
209 return false;
210 }
211 else if (!this.overlap.equals(other.overlap))
212 return false;
213 if (this.overlapFront == null)
214 {
215 if (other.overlapFront != null)
216 return false;
217 }
218 else if (!this.overlapFront.equals(other.overlapFront))
219 return false;
220 if (this.overlapRear == null)
221 {
222 if (other.overlapRear != null)
223 return false;
224 }
225 else if (!this.overlapRear.equals(other.overlapRear))
226 return false;
227 return true;
228 }
229
230
231 @SuppressWarnings("checkstyle:designforextension")
232 @Override
233 public String toString()
234 {
235 if (isParallel())
236 {
237 return String.format("Parallel to object %s of type %s with speed %s", getId(), getObjectType(), getSpeed());
238 }
239 return String.format("Headway %s to object %s of type %s with speed %s", getDistance(), getId(), getObjectType(),
240 getSpeed());
241 }
242
243 }