View Javadoc
1   package org.opentrafficsim.road.network.sampling;
2   
3   import org.djunits.unit.SpeedUnit;
4   import org.djunits.value.vdouble.scalar.Speed;
5   import org.opentrafficsim.core.gtu.GTUException;
6   import org.opentrafficsim.core.network.NetworkException;
7   import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
8   import org.opentrafficsim.kpi.sampling.data.ExtendedDataType;
9   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10  
11  import nl.tudelft.simulation.language.Throw;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 11 okt. 2016 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   */
23  public class SpeedLimit extends ExtendedDataType<Speed>
24  {
25  
26      /**
27       * Constructor.
28       */
29      public SpeedLimit()
30      {
31          super("SpeedLimit");
32      }
33  
34      /** {@inheritDoc} */
35      @Override
36      public final Speed getValue(final GtuDataInterface gtu)
37      {
38          Throw.whenNull(gtu, "GTU may not be null.");
39          Throw.when(!(gtu instanceof GtuData), IllegalArgumentException.class,
40                  "Extended data type speed limit is only for use with GtuData. The provided data %s is of the wrong type.", gtu);
41          LaneBasedGTU laneGtu = ((GtuData) gtu).getGtu();
42          try
43          {
44              return laneGtu.getReferencePosition().getLane().getSpeedLimit(laneGtu.getGTUType());
45          }
46          catch (NetworkException | GTUException exception)
47          {
48              throw new RuntimeException("Could not obtain speed limit.", exception);
49          }
50      }
51  
52      /** {@inheritDoc} */
53      public final String toString()
54      {
55          return "SpeedLimit";
56      }
57  
58  }