View Javadoc
1   package org.opentrafficsim.core.egtf.typed;
2   
3   import org.djunits.unit.FrequencyUnit;
4   import org.djunits.unit.LinearDensityUnit;
5   import org.djunits.unit.SpeedUnit;
6   import org.djunits.unit.Unit;
7   import org.djunits.value.Scalar;
8   import org.djunits.value.StorageType;
9   import org.djunits.value.ValueException;
10  import org.djunits.value.vdouble.matrix.DoubleMatrixInterface;
11  import org.djunits.value.vdouble.matrix.FrequencyMatrix;
12  import org.djunits.value.vdouble.matrix.LinearDensityMatrix;
13  import org.djunits.value.vdouble.matrix.SpeedMatrix;
14  import org.djunits.value.vdouble.scalar.Frequency;
15  import org.djunits.value.vdouble.scalar.LinearDensity;
16  import org.djunits.value.vdouble.scalar.Speed;
17  import org.opentrafficsim.core.egtf.Converter;
18  import org.opentrafficsim.core.egtf.Quantity;
19  
20  /**
21   * Quantities for a strongly-typed context.
22   * <p>
23   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 24 okt. 2018 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   * @param <U> unit of data
31   * @param <T> data type
32   * @param <K> grid output format
33   */
34  public class TypedQuantity<U extends Unit<U>, T extends Scalar<U>, K extends DoubleMatrixInterface<U>> extends Quantity<T, K>
35  {
36      /** Standard quantity for speed. */
37      public static final Quantity<Speed, SpeedMatrix> SPEED = new TypedQuantity<>("Speed", true, new Converter<SpeedMatrix>()
38      {
39          @Override
40          public SpeedMatrix convert(final double[][] data)
41          {
42              try
43              {
44                  return new SpeedMatrix(data, SpeedUnit.SI, StorageType.DENSE);
45              }
46              catch (ValueException exception)
47              {
48                  // should not happen
49                  throw new RuntimeException("Unexcepted exception: data is null when converting.", exception);
50              }
51          }
52      });
53  
54      /** Standard quantity for flow. */
55      public static final Quantity<Frequency, FrequencyMatrix> FLOW = new TypedQuantity<>("Flow", new Converter<FrequencyMatrix>()
56      {
57          @Override
58          public FrequencyMatrix convert(final double[][] data)
59          {
60              try
61              {
62                  return new FrequencyMatrix(data, FrequencyUnit.SI, StorageType.DENSE);
63              }
64              catch (ValueException exception)
65              {
66                  // should not happen
67                  throw new RuntimeException("Unexcepted exception: data is null when converting.", exception);
68              }
69          }
70      });
71  
72      /** Standard quantity for density. */
73      public static final Quantity<LinearDensity, LinearDensityMatrix> DENSITY =
74              new TypedQuantity<>("Density", new Converter<LinearDensityMatrix>()
75              {
76                  @Override
77                  public LinearDensityMatrix convert(final double[][] data)
78                  {
79                      try
80                      {
81                          return new LinearDensityMatrix(data, LinearDensityUnit.SI, StorageType.DENSE);
82                      }
83                      catch (ValueException exception)
84                      {
85                          // should not happen
86                          throw new RuntimeException("Unexcepted exception: data is null when converting.", exception);
87                      }
88                  }
89              });
90  
91      /**
92       * Constructor.
93       * @param name String; name
94       * @param converter Converter&lt;K&gt;; converter for output format
95       */
96      public TypedQuantity(final String name, final Converter<K> converter)
97      {
98          super(name, false, converter);
99      }
100 
101     /**
102      * Constructor. Private so only the default SPEED quantity is speed.
103      * @param name String; name
104      * @param speed boolean; whether this quantity is speed
105      * @param converter Converter&lt;K&gt;; converter for output format
106      */
107     protected TypedQuantity(final String name, final boolean speed, final Converter<K> converter)
108     {
109         super(name, speed, converter);
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public String toString()
115     {
116         return "TypedQuantity []";
117     }
118 
119 }