TypedQuantity.java

  1. package org.opentrafficsim.draw.egtf.typed;

  2. import org.djunits.unit.FrequencyUnit;
  3. import org.djunits.unit.LinearDensityUnit;
  4. import org.djunits.unit.SpeedUnit;
  5. import org.djunits.unit.Unit;
  6. import org.djunits.value.ValueRuntimeException;
  7. import org.djunits.value.base.Scalar;
  8. import org.djunits.value.vdouble.matrix.FrequencyMatrix;
  9. import org.djunits.value.vdouble.matrix.LinearDensityMatrix;
  10. import org.djunits.value.vdouble.matrix.SpeedMatrix;
  11. import org.djunits.value.vdouble.matrix.base.DoubleMatrix;
  12. import org.djunits.value.vdouble.scalar.Frequency;
  13. import org.djunits.value.vdouble.scalar.LinearDensity;
  14. import org.djunits.value.vdouble.scalar.Speed;
  15. import org.opentrafficsim.draw.egtf.Converter;
  16. import org.opentrafficsim.draw.egtf.Quantity;

  17. /**
  18.  * Quantities for a strongly-typed context.
  19.  * <p>
  20.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  21.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * </p>
  23.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  24.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  25.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  26.  * @param <U> unit of data
  27.  * @param <T> data type
  28.  * @param <K> grid output format
  29.  */
  30. public class TypedQuantity<U extends Unit<U>, T extends Scalar<U, T>, K extends DoubleMatrix<U, ?, ?, ?>> extends Quantity<T, K>
  31. {
  32.     /** Standard quantity for speed. */
  33.     public static final Quantity<Speed, SpeedMatrix> SPEED = new TypedQuantity<>("Speed", true, new Converter<SpeedMatrix>()
  34.     {
  35.         @Override
  36.         public SpeedMatrix convert(final double[][] data)
  37.         {
  38.             try
  39.             {
  40.                 return new SpeedMatrix(data, SpeedUnit.SI);
  41.             }
  42.             catch (ValueRuntimeException exception)
  43.             {
  44.                 // should not happen
  45.                 throw new RuntimeException("Unexcepted exception: data is null when converting.", exception);
  46.             }
  47.         }
  48.     });

  49.     /** Standard quantity for flow. */
  50.     public static final Quantity<Frequency, FrequencyMatrix> FLOW = new TypedQuantity<>("Flow", new Converter<FrequencyMatrix>()
  51.     {
  52.         @Override
  53.         public FrequencyMatrix convert(final double[][] data)
  54.         {
  55.             try
  56.             {
  57.                 return new FrequencyMatrix(data, FrequencyUnit.SI);
  58.             }
  59.             catch (ValueRuntimeException exception)
  60.             {
  61.                 // should not happen
  62.                 throw new RuntimeException("Unexcepted exception: data is null when converting.", exception);
  63.             }
  64.         }
  65.     });

  66.     /** Standard quantity for density. */
  67.     public static final Quantity<LinearDensity, LinearDensityMatrix> DENSITY =
  68.             new TypedQuantity<>("Density", new Converter<LinearDensityMatrix>()
  69.             {
  70.                 @Override
  71.                 public LinearDensityMatrix convert(final double[][] data)
  72.                 {
  73.                     try
  74.                     {
  75.                         return new LinearDensityMatrix(data, LinearDensityUnit.SI);
  76.                     }
  77.                     catch (ValueRuntimeException exception)
  78.                     {
  79.                         // should not happen
  80.                         throw new RuntimeException("Unexcepted exception: data is null when converting.", exception);
  81.                     }
  82.                 }
  83.             });

  84.     /**
  85.      * Constructor.
  86.      * @param name String; name
  87.      * @param converter Converter&lt;K&gt;; converter for output format
  88.      */
  89.     public TypedQuantity(final String name, final Converter<K> converter)
  90.     {
  91.         super(name, false, converter);
  92.     }

  93.     /**
  94.      * Constructor. Private so only the default SPEED quantity is speed.
  95.      * @param name String; name
  96.      * @param speed boolean; whether this quantity is speed
  97.      * @param converter Converter&lt;K&gt;; converter for output format
  98.      */
  99.     protected TypedQuantity(final String name, final boolean speed, final Converter<K> converter)
  100.     {
  101.         super(name, speed, converter);
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public String toString()
  106.     {
  107.         return "TypedQuantity []";
  108.     }

  109. }