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