DataSource.java

  1. package org.opentrafficsim.draw.egtf;

  2. import java.util.LinkedHashMap;
  3. import java.util.Map;

  4. /**
  5.  * Data source for the EGTF. These are obtained using {@code EGTF.getDataSource()}.
  6.  * <p>
  7.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  8.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  9.  * </p>
  10.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  11.  */
  12. public final class DataSource
  13. {
  14.     /** Unique name. */
  15.     private final String name;

  16.     /** Data stream of this data source. */
  17.     private final Map<String, DataStream<?>> streams = new LinkedHashMap<>();

  18.     /**
  19.      * Constructor.
  20.      * @param name unique name
  21.      */
  22.     DataSource(final String name)
  23.     {
  24.         this.name = name;
  25.     }

  26.     /**
  27.      * Returns the name.
  28.      * @return name
  29.      */
  30.     public String getName()
  31.     {
  32.         return this.name;
  33.     }

  34.     /**
  35.      * Add a non-speed stream for the quantity to this data source.
  36.      * @param quantity quantity
  37.      * @param thetaCong standard deviation of this quantity of measurements in congestion by this data source
  38.      * @param thetaFree standard deviation of this quantity of measurements in free flow by this data source
  39.      * @param <T> implicit data type
  40.      * @return the created data stream
  41.      * @throws IllegalArgumentException if the quantity is speed
  42.      */
  43.     public <T extends Number> DataStream<T> addStream(final Quantity<T, ?> quantity, final T thetaCong, final T thetaFree)
  44.     {
  45.         return addStreamSI(quantity, thetaCong.doubleValue(), thetaFree.doubleValue());
  46.     }

  47.     /**
  48.      * Add a stream for the quantity to this data source.
  49.      * @param quantity quantity
  50.      * @param thetaCong standard deviation of this quantity of measurements in congestion by this data source in SI
  51.      * @param thetaFree standard deviation of this quantity of measurements in free flow by this data source in SI
  52.      * @param <T> implicit data type
  53.      * @return the created data stream
  54.      */
  55.     public <T extends Number> DataStream<T> addStreamSI(final Quantity<T, ?> quantity, final double thetaCong,
  56.             final double thetaFree)
  57.     {
  58.         if (this.streams.containsKey(quantity.getName()))
  59.         {
  60.             throw new IllegalStateException(
  61.                     String.format("Data source %s already has a stream for quantity %s.", this.name, quantity.getName()));
  62.         }
  63.         if (thetaCong <= 0.0 || thetaFree <= 0.0)
  64.         {
  65.             throw new IllegalArgumentException("Standard deviation must be positive and above 0.");
  66.         }
  67.         DataStream<T> dataStream = new DataStream<>(this, quantity, thetaCong, thetaFree);
  68.         this.streams.put(quantity.getName(), dataStream);
  69.         return dataStream;
  70.     }

  71.     /**
  72.      * Get a stream for the quantity of this data source. If no stream has been created, one will be created with 1.0 standard
  73.      * deviation.
  74.      * @param quantity quantity
  75.      * @return stream for the quantity of this data source
  76.      * @param <T> implicit data type
  77.      */
  78.     @SuppressWarnings({"unchecked"})
  79.     public <T extends Number> DataStream<T> getStream(final Quantity<T, ?> quantity)
  80.     {
  81.         if (!this.streams.containsKey(quantity.getName()))
  82.         {
  83.             addStreamSI(quantity, 1.0, 1.0);
  84.         }
  85.         return (DataStream<T>) this.streams.get(quantity.getName());
  86.     }

  87.     @Override
  88.     public int hashCode()
  89.     {
  90.         final int prime = 31;
  91.         int result = 1;
  92.         result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
  93.         return result;
  94.     }

  95.     @Override
  96.     public boolean equals(final Object obj)
  97.     {
  98.         if (this == obj)
  99.         {
  100.             return true;
  101.         }
  102.         if (obj == null)
  103.         {
  104.             return false;
  105.         }
  106.         if (getClass() != obj.getClass())
  107.         {
  108.             return false;
  109.         }
  110.         DataSource other = (DataSource) obj;
  111.         if (this.name == null)
  112.         {
  113.             if (other.name != null)
  114.             {
  115.                 return false;
  116.             }
  117.         }
  118.         else if (!this.name.equals(other.name))
  119.         {
  120.             return false;
  121.         }
  122.         return true;
  123.     }

  124.     @Override
  125.     public String toString()
  126.     {
  127.         return "DataSource [" + this.name + "]";
  128.     }

  129. }