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