1 package org.opentrafficsim.core.network.lane;
2
3 import org.opentrafficsim.core.gtu.RelativePosition;
4 import org.opentrafficsim.core.unit.LengthUnit;
5 import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
6
7
8
9
10
11
12
13
14
15
16
17 public abstract class AbstractSensor implements Sensor
18 {
19
20 private static final long serialVersionUID = 20141231L;
21
22
23 private final Lane lane;
24
25
26 private final double longitudinalPositionSI;
27
28
29 private final RelativePosition.TYPE positionType;
30
31
32
33
34
35
36
37
38 public AbstractSensor(final Lane lane, final DoubleScalar.Rel<LengthUnit> longitudinalPosition,
39 final RelativePosition.TYPE positionType)
40 {
41 this.lane = lane;
42 this.longitudinalPositionSI = longitudinalPosition.getSI();
43 this.positionType = positionType;
44 }
45
46
47 @Override
48 public final Lane getLane()
49 {
50 return this.lane;
51 }
52
53
54 @Override
55 public final DoubleScalar.Abs<LengthUnit> getLongitudinalPosition()
56 {
57 return new DoubleScalar.Abs<LengthUnit>(this.longitudinalPositionSI, LengthUnit.METER);
58 }
59
60
61 @Override
62 public final RelativePosition.TYPE getPositionType()
63 {
64 return this.positionType;
65 }
66
67
68 @Override
69 public final double getLongitudinalPositionSI()
70 {
71 return this.longitudinalPositionSI;
72 }
73
74
75 @SuppressWarnings("checkstyle:designforextension")
76 @Override
77 public int hashCode()
78 {
79 final int prime = 31;
80 int result = 1;
81 result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
82 long temp;
83 temp = Double.doubleToLongBits(this.longitudinalPositionSI);
84 result = prime * result + (int) (temp ^ (temp >>> 32));
85 result = prime * result + ((this.positionType == null) ? 0 : this.positionType.hashCode());
86 return result;
87 }
88
89
90 @SuppressWarnings({"checkstyle:needbraces", "checkstyle:designforextension"})
91 @Override
92 public boolean equals(final Object obj)
93 {
94 if (this == obj)
95 return true;
96 if (obj == null)
97 return false;
98 if (getClass() != obj.getClass())
99 return false;
100 AbstractSensor other = (AbstractSensor) obj;
101 if (this.lane == null)
102 {
103 if (other.lane != null)
104 return false;
105 }
106 else if (!this.lane.equals(other.lane))
107 return false;
108 if (Double.doubleToLongBits(this.longitudinalPositionSI) != Double
109 .doubleToLongBits(other.longitudinalPositionSI))
110 return false;
111 if (this.positionType == null)
112 {
113 if (other.positionType != null)
114 return false;
115 }
116 else if (!this.positionType.equals(other.positionType))
117 return false;
118 return true;
119 }
120
121
122 @SuppressWarnings("checkstyle:designforextension")
123 @Override
124 public int compareTo(final Sensor o)
125 {
126 if (this.lane != o.getLane())
127 {
128 return this.lane.hashCode() < o.getLane().hashCode() ? -1 : 1;
129 }
130 if (this.longitudinalPositionSI != o.getLongitudinalPositionSI())
131 {
132 return this.longitudinalPositionSI < o.getLongitudinalPositionSI() ? -1 : 1;
133 }
134 if (!this.positionType.equals(o.getPositionType()))
135 {
136 return this.positionType.hashCode() < o.getPositionType().hashCode() ? -1 : 1;
137 }
138 if (!this.equals(o))
139 {
140 return this.hashCode() < o.hashCode() ? -1 : 1;
141 }
142 return 0;
143 }
144
145 }