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