DemandPattern.java

  1. package org.opentrafficsim.road.gtu.generator.headway;

  2. import org.djunits.value.vdouble.scalar.Frequency;
  3. import org.djunits.value.vdouble.scalar.Time;
  4. import org.djunits.value.vdouble.vector.FrequencyVector;
  5. import org.djunits.value.vdouble.vector.TimeVector;
  6. import org.opentrafficsim.road.gtu.strategical.od.Interpolation;

  7. /**
  8.  * Demand pattern defined by a frequency vector, time vector and interpolation.
  9.  * <p>
  10.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  12.  * <p>
  13.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 22 mrt. 2018 <br>
  14.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  15.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  16.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  17.  */
  18. public class DemandPattern implements Arrivals
  19. {

  20.     /** Demand vector. */
  21.     private final FrequencyVector demandVector;

  22.     /** Time vector, may be null. */
  23.     private final TimeVector timeVector;

  24.     /** Interpolation, may be null. */
  25.     private final Interpolation interpolation;

  26.     /**
  27.      * Constructor.
  28.      * @param demandVector FrequencyVector; demand vector
  29.      * @param timeVector TimeVector; time vector
  30.      * @param interpolation Interpolation; interpolation
  31.      */
  32.     public DemandPattern(final FrequencyVector demandVector, final TimeVector timeVector, final Interpolation interpolation)
  33.     {
  34.         this.demandVector = demandVector;
  35.         this.timeVector = timeVector;
  36.         this.interpolation = interpolation;
  37.     }

  38.     /**
  39.      * Returns the demand vector.
  40.      * @return FrequencyVector; returns the demand vector
  41.      */
  42.     public final FrequencyVector getDemandVector()
  43.     {
  44.         return this.demandVector;
  45.     }

  46.     /**
  47.      * Returns the time vector.
  48.      * @return TimeVector; returns the time vector
  49.      */
  50.     public final TimeVector getTimeVector()
  51.     {
  52.         return this.timeVector;
  53.     }

  54.     /**
  55.      * Returns the interpolation.
  56.      * @return Interpolation; returns the interpolation
  57.      */
  58.     public final Interpolation getInterpolation()
  59.     {
  60.         return this.interpolation;
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public Frequency getFrequency(final Time time, final boolean sliceStart)
  65.     {
  66.         return this.interpolation.interpolateVector(time, this.demandVector, this.timeVector, sliceStart);
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public Time nextTimeSlice(final Time time)
  71.     {
  72.         for (Time d : this.timeVector)
  73.         {
  74.             if (d.gt(time))
  75.             {
  76.                 return d;
  77.             }
  78.         }
  79.         return null;
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public int hashCode()
  84.     {
  85.         final int prime = 31;
  86.         int result = 1;
  87.         result = prime * result + ((this.demandVector == null) ? 0 : this.demandVector.hashCode());
  88.         result = prime * result + ((this.interpolation == null) ? 0 : this.interpolation.hashCode());
  89.         result = prime * result + ((this.timeVector == null) ? 0 : this.timeVector.hashCode());
  90.         return result;
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     public boolean equals(final Object obj)
  95.     {
  96.         if (this == obj)
  97.         {
  98.             return true;
  99.         }
  100.         if (obj == null)
  101.         {
  102.             return false;
  103.         }
  104.         if (getClass() != obj.getClass())
  105.         {
  106.             return false;
  107.         }
  108.         DemandPattern other = (DemandPattern) obj;
  109.         if (this.demandVector == null)
  110.         {
  111.             if (other.demandVector != null)
  112.             {
  113.                 return false;
  114.             }
  115.         }
  116.         else if (!this.demandVector.equals(other.demandVector))
  117.         {
  118.             return false;
  119.         }
  120.         if (this.interpolation != other.interpolation)
  121.         {
  122.             return false;
  123.         }
  124.         if (this.timeVector == null)
  125.         {
  126.             if (other.timeVector != null)
  127.             {
  128.                 return false;
  129.             }
  130.         }
  131.         else if (!this.timeVector.equals(other.timeVector))
  132.         {
  133.             return false;
  134.         }
  135.         return true;
  136.     }

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     public String toString()
  140.     {
  141.         return "DemandPattern [demandVector=" + this.demandVector + ", timeVector=" + this.timeVector + ", interpolation="
  142.                 + this.interpolation + "]";
  143.     }

  144. }