View Javadoc
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    * <p>
9    * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights
10   * reserved. <br>
11   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
12   * <p>
13   * @version Dec 31, 2014 <br>
14   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16   */
17  public abstract class AbstractSensor implements Sensor
18  {
19      /** */
20      private static final long serialVersionUID = 20141231L;
21  
22      /** The lane for which this is a sensor. */
23      private final Lane lane;
24  
25      /** The position (between 0.0 and the length of the Lane) of the sensor on the design line of the lane in SI units. */
26      private final double longitudinalPositionSI;
27  
28      /** the relative position of the vehicle that triggers the sensor. */
29      private final RelativePosition.TYPE positionType;
30  
31      /**
32       * @param lane The lane for which this is a sensor.
33       * @param longitudinalPosition DoubleScalar.Rel&lt;LengthUnit&gt;; the position (between 0.0 and the length of the
34       *            Lane) of the sensor on the design line of the lane.
35       * @param positionType RelativePosition.TYPE; the relative position type (e.g., FRONT, BACK) of the vehicle that
36       *            triggers the sensor.
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      /** {@inheritDoc} */
47      @Override
48      public final Lane getLane()
49      {
50          return this.lane;
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public final DoubleScalar.Abs<LengthUnit> getLongitudinalPosition()
56      {
57          return new DoubleScalar.Abs<LengthUnit>(this.longitudinalPositionSI, LengthUnit.METER);
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public final RelativePosition.TYPE getPositionType()
63      {
64          return this.positionType;
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public final double getLongitudinalPositionSI()
70      {
71          return this.longitudinalPositionSI;
72      }
73  
74      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }