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.gtu.strategical.od.Interpolation;
8   
9   /**
10   * Demand pattern defined by a frequency vector, time vector and interpolation.
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/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 22 mrt. 2018 <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   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19   */
20  public class DemandPattern implements Arrivals
21  {
22  
23      /** Demand vector. */
24      private final FrequencyVector demandVector;
25  
26      /** Time vector, may be null. */
27      private final TimeVector timeVector;
28  
29      /** Interpolation, may be null. */
30      private final Interpolation interpolation;
31  
32      /**
33       * Constructor.
34       * @param demandVector FrequencyVector; demand vector
35       * @param timeVector TimeVector; time vector
36       * @param interpolation Interpolation; interpolation
37       */
38      public DemandPattern(final FrequencyVector demandVector, final TimeVector timeVector, final Interpolation interpolation)
39      {
40          this.demandVector = demandVector;
41          this.timeVector = timeVector;
42          this.interpolation = interpolation;
43      }
44  
45      /**
46       * Returns the demand vector.
47       * @return FrequencyVector; returns the demand vector
48       */
49      public final FrequencyVector getDemandVector()
50      {
51          return this.demandVector;
52      }
53  
54      /**
55       * Returns the time vector.
56       * @return TimeVector; returns the time vector
57       */
58      public final TimeVector getTimeVector()
59      {
60          return this.timeVector;
61      }
62  
63      /**
64       * Returns the interpolation.
65       * @return Interpolation; returns the interpolation
66       */
67      public final Interpolation getInterpolation()
68      {
69          return this.interpolation;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public Frequency getFrequency(final Time time, final boolean sliceStart)
75      {
76          return this.interpolation.interpolateVector(time, this.demandVector, this.timeVector, sliceStart);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public Time nextTimeSlice(final Time time)
82      {
83          for (Time d : this.timeVector)
84          {
85              if (d.gt(time))
86              {
87                  return d;
88              }
89          }
90          return null;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public int hashCode()
96      {
97          final int prime = 31;
98          int result = 1;
99          result = prime * result + ((this.demandVector == null) ? 0 : this.demandVector.hashCode());
100         result = prime * result + ((this.interpolation == null) ? 0 : this.interpolation.hashCode());
101         result = prime * result + ((this.timeVector == null) ? 0 : this.timeVector.hashCode());
102         return result;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public boolean equals(final Object obj)
108     {
109         if (this == obj)
110         {
111             return true;
112         }
113         if (obj == null)
114         {
115             return false;
116         }
117         if (getClass() != obj.getClass())
118         {
119             return false;
120         }
121         DemandPattern other = (DemandPattern) obj;
122         if (this.demandVector == null)
123         {
124             if (other.demandVector != null)
125             {
126                 return false;
127             }
128         }
129         else if (!this.demandVector.equals(other.demandVector))
130         {
131             return false;
132         }
133         if (this.interpolation != other.interpolation)
134         {
135             return false;
136         }
137         if (this.timeVector == null)
138         {
139             if (other.timeVector != null)
140             {
141                 return false;
142             }
143         }
144         else if (!this.timeVector.equals(other.timeVector))
145         {
146             return false;
147         }
148         return true;
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public String toString()
154     {
155         return "DemandPattern [demandVector=" + this.demandVector + ", timeVector=" + this.timeVector + ", interpolation="
156                 + this.interpolation + "]";
157     }
158 
159 }