View Javadoc
1   package org.opentrafficsim.kpi.sampling;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.opentrafficsim.kpi.interfaces.LaneDataInterface;
7   
8   import nl.tudelft.simulation.language.Throw;
9   
10  /**
11   * <p>
12   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
16   * initial version Mar 30, 2016 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   */
20  public class KpiLaneDirection implements Serializable
21  {
22      /** */
23      private static final long serialVersionUID = 20160330L;
24  
25      /** The lane. */
26      private final LaneDataInterface lane;
27  
28      /** The GTU direction to drive on this lane. */
29      private final KpiGtuDirectionality direction;
30  
31      /**
32       * @param lane the lane
33       * @param direction the direction to drive on this lane
34       */
35      public KpiLaneDirection(final LaneDataInterface lane, final KpiGtuDirectionality direction)
36      {
37          Throw.whenNull(lane, "Lane may not be null.");
38          Throw.whenNull(direction, "Direction may not be null.");
39          this.lane = lane;
40          this.direction = direction;
41      }
42  
43      /**
44       * @return the lane
45       */
46      public final LaneDataInterface getLaneData()
47      {
48          return this.lane;
49      }
50  
51      /**
52       * @return the direction to drive on this lane
53       */
54      public final KpiGtuDirectionality getKpiDirection()
55      {
56          return this.direction;
57      }
58  
59      /**
60       * Returns the position with increasing value in the direction of travel, i.e. the node to the back of the vehicle is at x =
61       * 0 while the node in front of the vehicle is at x = {@code lane.getLength()}, irrespective of the design line direction.
62       * @param position the position on the lane irrespective of the direction
63       * @return position with increasing value in the direction of travel
64       */
65      public final Length getPositionInDirection(Length position)
66      {
67          Throw.whenNull(position, "Position may not be null.");
68          return this.direction.equals(KpiGtuDirectionality.DIR_PLUS) ? position : this.lane.getLength().minus(position);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public String toString()
74      {
75          return "[" + this.lane + (this.direction.isPlus() ? " +]" : " -]");
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final int hashCode()
81      {
82          final int prime = 31;
83          int result = 1;
84          result = prime * result + ((this.direction == null) ? 0 : this.direction.hashCode());
85          result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
86          return result;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final boolean equals(final Object obj)
92      {
93          if (this == obj)
94          {
95              return true;
96          }
97          if (obj == null)
98          {
99              return false;
100         }
101         if (getClass() != obj.getClass())
102         {
103             return false;
104         }
105         KpiLaneDirection other = (KpiLaneDirection) obj;
106         if (this.direction != other.direction)
107         {
108             return false;
109         }
110         if (this.lane == null)
111         {
112             if (other.lane != null)
113             {
114                 return false;
115             }
116         }
117         else if (!this.lane.equals(other.lane))
118         {
119             return false;
120         }
121         return true;
122     }
123 
124 }