View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.util.function.Supplier;
4   
5   import org.djunits.value.vdouble.scalar.Acceleration;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djunits.value.vdouble.scalar.Speed;
8   import org.djutils.exceptions.Throw;
9   import org.opentrafficsim.core.distributions.ConstantSupplier;
10  
11  /**
12   * Stores some of the information that is needed to generate a GTU.
13   * <p>
14   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * <p>
17   * $LastChangedDate$, @version $Revision$, by $Author$, initial version Jul 8, 2014 <br>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   */
20  public class GtuTemplate implements Supplier<GtuCharacteristics>
21  {
22      /** The type of the GTU. */
23      private final GtuType gtuType;
24  
25      /** Supplier for the length of the GTU. */
26      private final Supplier<Length> lengthSupplier;
27  
28      /** Supplier for the width of the GTU. */
29      private final Supplier<Length> widthSupplier;
30  
31      /** Supplier for the maximum speed of the GTU. */
32      private final Supplier<Speed> maximumSpeedSupplier;
33  
34      /** Supplier for the maximum acceleration of the GTU. */
35      private final Supplier<Acceleration> maxAcceleration;
36  
37      /** Supplier for the maximum deceleration of the GTU. */
38      private final Supplier<Acceleration> maxDeceleration;
39  
40      /**
41       * Constructor.
42       * @param gtuType GtuType, the GtuType to make it identifiable.
43       * @param lengthSupplier supplier for the length of the GTU type (parallel with driving direction).
44       * @param widthSupplier supplier for the width of the GTU type (perpendicular to driving direction).
45       * @param maximumSpeedSupplier supplier for the maximum speed of the GTU type (in the driving direction).
46       * @throws NullPointerException when one of the arguments is null
47       */
48      public GtuTemplate(final GtuType gtuType, final Supplier<Length> lengthSupplier, final Supplier<Length> widthSupplier,
49              final Supplier<Speed> maximumSpeedSupplier) throws NullPointerException
50      {
51          this(gtuType, lengthSupplier, widthSupplier, maximumSpeedSupplier, new ConstantSupplier<>(Acceleration.ofSI(3.0)),
52                  new ConstantSupplier<>(Acceleration.ofSI(-8.0)));
53      }
54  
55      /**
56       * Constructor.
57       * @param gtuType GtuType, the GtuType to make it identifiable.
58       * @param lengthSupplier supplier for the length of the GTU type (parallel with driving direction).
59       * @param widthSupplier supplier for the width of the GTU type (perpendicular to driving direction).
60       * @param maximumSpeedSupplier supplier for the maximum speed of the GTU type (in the driving direction).
61       * @param maximumAccelerationSupplier supplier for the maximum acceleration of the GTU type
62       * @param maximumDecelerationSupplier supplier for the maximum deceleration of the GTU type
63       * @throws NullPointerException when one of the arguments is null
64       */
65      public GtuTemplate(final GtuType gtuType, final Supplier<Length> lengthSupplier, final Supplier<Length> widthSupplier,
66              final Supplier<Speed> maximumSpeedSupplier, final Supplier<Acceleration> maximumAccelerationSupplier,
67              final Supplier<Acceleration> maximumDecelerationSupplier) throws NullPointerException
68      {
69          Throw.whenNull(gtuType, "gtuType is null");
70          Throw.whenNull(lengthSupplier, "lengthSupplier is null");
71          Throw.whenNull(widthSupplier, "widthSupplier is null");
72          Throw.whenNull(maximumSpeedSupplier, "maximumSpeedSupplier is null");
73          Throw.whenNull(maximumAccelerationSupplier, "maximumAccelerationSupplier is null");
74          Throw.whenNull(maximumDecelerationSupplier, "maximumDecelerationSupplier is null");
75  
76          this.gtuType = gtuType;
77          this.lengthSupplier = lengthSupplier;
78          this.widthSupplier = widthSupplier;
79          this.maximumSpeedSupplier = maximumSpeedSupplier;
80          this.maxAcceleration = maximumAccelerationSupplier;
81          this.maxDeceleration = maximumDecelerationSupplier;
82      }
83  
84      /**
85       * Returns the same Characteristics, but pertaining to a different GTU type. This is useful for when the GTU type is used
86       * for other purposes in simulation, where the {@code GtuCharacteristics} should be the same.
87       * @param newGtuType the new GTU type.
88       * @return Copy of this {@code TemplateGTUType} linked to the new GTU type.
89       */
90      public GtuTemplate copyForGtuType(final GtuType newGtuType)
91      {
92          return new GtuTemplate(newGtuType, this.lengthSupplier, this.widthSupplier, this.maximumSpeedSupplier,
93                  this.maxAcceleration, this.maxDeceleration);
94      }
95  
96      /**
97       * Returns characteristics for the given GTU.
98       * @return characteristics for the given GTU
99       */
100     @Override
101     @SuppressWarnings("checkstyle:designforextension")
102     public GtuCharacteristics get()
103     {
104         Acceleration acceleration = this.maxAcceleration.get();
105         Acceleration deceleration = this.maxDeceleration.get();
106         Throw.when(acceleration.si <= 0, IllegalArgumentException.class, "Acceleration should be above 0.");
107         Throw.when(deceleration.si >= 0, IllegalArgumentException.class, "Deceleration should be below 0.");
108         Length length = this.lengthSupplier.get();
109         return new GtuCharacteristics(this.gtuType, length, this.widthSupplier.get(), this.maximumSpeedSupplier.get(),
110                 acceleration, deceleration, length.times(0.75));
111     }
112 
113     /**
114      * Returns the GTU type.
115      * @return gtuType.
116      */
117     @SuppressWarnings("checkstyle:designforextension")
118     public GtuType getGtuType()
119     {
120         return this.gtuType;
121     }
122 
123     @Override
124     @SuppressWarnings("checkstyle:designforextension")
125     public String toString()
126     {
127         return String.format("TemplateGTUType [%s, %s, %s, %s]", this.gtuType, this.lengthSupplier, this.widthSupplier,
128                 this.maximumSpeedSupplier);
129     }
130 
131 }