View Javadoc
1   package org.opentrafficsim.road.gtu.generator.headway;
2   
3   import org.djunits.value.vdouble.scalar.Frequency;
4   import org.djunits.value.vdouble.scalar.Time;
5   import org.djunits.value.vdouble.vector.FrequencyVector;
6   import org.djunits.value.vdouble.vector.TimeVector;
7   import org.opentrafficsim.road.od.Interpolation;
8   
9   /**
10   * Demand pattern defined by a frequency vector, time vector and interpolation.
11   * <p>
12   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
17   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
18   */
19  public class DemandPattern implements Arrivals
20  {
21  
22      /** Demand vector. */
23      private final FrequencyVector demandVector;
24  
25      /** Time vector, may be null. */
26      private final TimeVector timeVector;
27  
28      /** Interpolation, may be null. */
29      private final Interpolation interpolation;
30  
31      /**
32       * Constructor.
33       * @param demandVector FrequencyVector; demand vector
34       * @param timeVector TimeVector; time vector
35       * @param interpolation Interpolation; interpolation
36       */
37      public DemandPattern(final FrequencyVector demandVector, final TimeVector timeVector, final Interpolation interpolation)
38      {
39          this.demandVector = demandVector;
40          this.timeVector = timeVector;
41          this.interpolation = interpolation;
42      }
43  
44      /**
45       * Returns the demand vector.
46       * @return FrequencyVector; returns the demand vector
47       */
48      public final FrequencyVector getDemandVector()
49      {
50          return this.demandVector;
51      }
52  
53      /**
54       * Returns the time vector.
55       * @return TimeVector; returns the time vector
56       */
57      public final TimeVector getTimeVector()
58      {
59          return this.timeVector;
60      }
61  
62      /**
63       * Returns the interpolation.
64       * @return Interpolation; returns the interpolation
65       */
66      public final Interpolation getInterpolation()
67      {
68          return this.interpolation;
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public Frequency getFrequency(final Time time, final boolean sliceStart)
74      {
75          return this.interpolation.interpolateVector(time, this.demandVector, this.timeVector, sliceStart);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public Time nextTimeSlice(final Time time)
81      {
82          for (Time d : this.timeVector)
83          {
84              if (d.gt(time))
85              {
86                  return d;
87              }
88          }
89          return null;
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public int hashCode()
95      {
96          final int prime = 31;
97          int result = 1;
98          result = prime * result + ((this.demandVector == null) ? 0 : this.demandVector.hashCode());
99          result = prime * result + ((this.interpolation == null) ? 0 : this.interpolation.hashCode());
100         result = prime * result + ((this.timeVector == null) ? 0 : this.timeVector.hashCode());
101         return result;
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public boolean equals(final Object obj)
107     {
108         if (this == obj)
109         {
110             return true;
111         }
112         if (obj == null)
113         {
114             return false;
115         }
116         if (getClass() != obj.getClass())
117         {
118             return false;
119         }
120         DemandPattern other = (DemandPattern) obj;
121         if (this.demandVector == null)
122         {
123             if (other.demandVector != null)
124             {
125                 return false;
126             }
127         }
128         else if (!this.demandVector.equals(other.demandVector))
129         {
130             return false;
131         }
132         if (this.interpolation != other.interpolation)
133         {
134             return false;
135         }
136         if (this.timeVector == null)
137         {
138             if (other.timeVector != null)
139             {
140                 return false;
141             }
142         }
143         else if (!this.timeVector.equals(other.timeVector))
144         {
145             return false;
146         }
147         return true;
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     public String toString()
153     {
154         return "DemandPattern [demandVector=" + this.demandVector + ", timeVector=" + this.timeVector + ", interpolation="
155                 + this.interpolation + "]";
156     }
157 
158 }