View Javadoc
1   package org.opentrafficsim.kpi.sampling;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djunits.value.vdouble.scalar.Time;
5   import org.djutils.exceptions.Throw;
6   import org.opentrafficsim.kpi.interfaces.LaneData;
7   
8   /**
9    * Defines a rectangular region over space and time on a lane.
10   * <p>
11   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
16   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
17   * @param <L> lane data type
18   */
19  public class SpaceTimeRegion<L extends LaneData>
20  {
21  
22      /** Lane. */
23      private final L lane;
24  
25      /** Start position. */
26      private final Length startPosition;
27  
28      /** End position. */
29      private final Length endPosition;
30  
31      /** Start time. */
32      private final Time startTime;
33  
34      /** End time. */
35      private final Time endTime;
36  
37      /**
38       * @param lane L; lane
39       * @param startPosition Length; start position
40       * @param endPosition Length; end position
41       * @param startTime Time; start time
42       * @param endTime Time; end time
43       * @throws IllegalArgumentException if start time is larger than end time
44       */
45      public SpaceTimeRegion(final L lane, final Length startPosition, final Length endPosition, final Time startTime,
46              final Time endTime)
47      {
48          Throw.whenNull(startPosition, "Start position may not be null.");
49          Throw.whenNull(endPosition, "End position may not be null.");
50          Throw.whenNull(startTime, "Start time may not be null.");
51          Throw.whenNull(endTime, "End time may not be null.");
52          Throw.when(endPosition.lt(startPosition), IllegalArgumentException.class,
53                  "End position should be greater than start position.");
54          Throw.when(endTime.lt(startTime), IllegalArgumentException.class, "End time should be greater than start time.");
55          this.lane = lane;
56          this.startPosition = startPosition;
57          this.endPosition = endPosition;
58          this.startTime = startTime;
59          this.endTime = endTime;
60      }
61  
62      /**
63       * Returns the lane.
64       * @return L; lane.
65       */
66      public final L getLane()
67      {
68          return this.lane;
69      }
70  
71      /**
72       * Returns the start position.
73       * @return Length; start position.
74       */
75      public final Length getStartPosition()
76      {
77          return this.startPosition;
78      }
79  
80      /**
81       * Returns the end position.
82       * @return Length; end position.
83       */
84      public final Length getEndPosition()
85      {
86          return this.endPosition;
87      }
88  
89      /**
90       * Returns the start time.
91       * @return Time; start time.
92       */
93      public final Time getStartTime()
94      {
95          return this.startTime;
96      }
97  
98      /**
99       * Returns the end time.
100      * @return Time end; time.
101      */
102     public final Time getEndTime()
103     {
104         return this.endTime;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final int hashCode()
110     {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + ((this.endPosition == null) ? 0 : this.endPosition.hashCode());
114         result = prime * result + ((this.endTime == null) ? 0 : this.endTime.hashCode());
115         result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
116         result = prime * result + ((this.startPosition == null) ? 0 : this.startPosition.hashCode());
117         result = prime * result + ((this.startTime == null) ? 0 : this.startTime.hashCode());
118         return result;
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public final boolean equals(final Object obj)
124     {
125         if (this == obj)
126         {
127             return true;
128         }
129         if (obj == null)
130         {
131             return false;
132         }
133         if (getClass() != obj.getClass())
134         {
135             return false;
136         }
137         SpaceTimeRegion<?> other = (SpaceTimeRegion<?>) obj;
138         if (this.endPosition == null)
139         {
140             if (other.endPosition != null)
141             {
142                 return false;
143             }
144         }
145         else if (!this.endPosition.equals(other.endPosition))
146         {
147             return false;
148         }
149         if (this.endTime == null)
150         {
151             if (other.endTime != null)
152             {
153                 return false;
154             }
155         }
156         else if (!this.endTime.equals(other.endTime))
157         {
158             return false;
159         }
160         if (this.lane == null)
161         {
162             if (other.lane != null)
163             {
164                 return false;
165             }
166         }
167         else if (!this.lane.equals(other.lane))
168         {
169             return false;
170         }
171         if (this.startPosition == null)
172         {
173             if (other.startPosition != null)
174             {
175                 return false;
176             }
177         }
178         else if (!this.startPosition.equals(other.startPosition))
179         {
180             return false;
181         }
182         if (this.startTime == null)
183         {
184             if (other.startTime != null)
185             {
186                 return false;
187             }
188         }
189         else if (!this.startTime.equals(other.startTime))
190         {
191             return false;
192         }
193         return true;
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public final String toString()
199     {
200         return "SpaceTimeRegion [laneDirection=" + this.lane + ", startPosition=" + this.startPosition + ", endPosition="
201                 + this.endPosition + ", startTime=" + this.startTime + ", endTime=" + this.endTime + "]";
202     }
203 
204 }