NumberDuration.java

  1. package org.sim0mq.message.types;

  2. import java.io.Serializable;

  3. import org.djunits.value.vdouble.scalar.Duration;
  4. import org.djunits.value.vfloat.scalar.FloatDuration;
  5. import org.sim0mq.Sim0MQException;

  6. /**
  7.  * Wrapper for a Number or float/double with Unit of type Duration. Store it internally as a Number <b>or</b> as a DoubleScalar,
  8.  * <b>or</b> as a FloatScalar and have methods to retrieve it in different ways.
  9.  * <p>
  10.  * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  11.  * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
  12.  * </p>
  13.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  14.  * initial version Apr 24, 2017 <br>
  15.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  16.  */
  17. public class NumberDuration extends Number implements Serializable
  18. {
  19.     /** */
  20.     private static final long serialVersionUID = 20170424L;

  21.     /** Number - any of the number types. */
  22.     private final Number duration;

  23.     /** DoubleScalar of type Duration. */
  24.     private final Duration doubleScalar;

  25.     /** FloatScalar of type FloatDuration. */
  26.     private final FloatDuration floatScalar;

  27.     /**
  28.      * Create a duration from a Number.
  29.      * @param duration the duration as a Number.
  30.      */
  31.     public NumberDuration(final Number duration)
  32.     {
  33.         this.duration = duration;
  34.         this.doubleScalar = null;
  35.         this.floatScalar = null;
  36.     }

  37.     /**
  38.      * Create a duration from a DoubleScalar Duration type.
  39.      * @param duration the duration as a DoubleScalar Duration.
  40.      */
  41.     public NumberDuration(final Duration duration)
  42.     {
  43.         this.duration = null;
  44.         this.doubleScalar = duration;
  45.         this.floatScalar = null;
  46.     }

  47.     /**
  48.      * Create a duration from a FloatScalar FloatDuration type.
  49.      * @param duration the duration as a FloatScalar FloatDuration.
  50.      */
  51.     public NumberDuration(final FloatDuration duration)
  52.     {
  53.         this.duration = null;
  54.         this.doubleScalar = null;
  55.         this.floatScalar = duration;
  56.     }

  57.     /**
  58.      * Instantiate a NumberDuration based on a value.
  59.      * @param value the value that can be Duration, FloatDuration, or Number
  60.      * @return an instantiation of NumberDuration
  61.      * @throws Sim0MQException if the value is neither Duration, FloatDuration, nor Number
  62.      */
  63.     public static NumberDuration instantiate(final Object value) throws Sim0MQException
  64.     {
  65.         if (value instanceof Duration)
  66.         {
  67.             return new NumberDuration((Duration) value);
  68.         }
  69.         else if (value instanceof FloatDuration)
  70.         {
  71.             return new NumberDuration((FloatDuration) value);
  72.         }
  73.         else if (value instanceof Number)
  74.         {
  75.             return new NumberDuration((Number) value);
  76.         }
  77.         else
  78.         {
  79.             throw new Sim0MQException("value should be Number, Duration or FloatDuration");
  80.         }
  81.     }
  82.    
  83.     @Override
  84.     public int intValue()
  85.     {
  86.         return this.duration.intValue();
  87.     }

  88.     @Override
  89.     public long longValue()
  90.     {
  91.         return this.duration.longValue();
  92.     }

  93.     @Override
  94.     public float floatValue()
  95.     {
  96.         return this.duration.floatValue();
  97.     }

  98.     @Override
  99.     public double doubleValue()
  100.     {
  101.         return this.duration.doubleValue();
  102.     }

  103.     /**
  104.      * Return the NumberDuration as an object, e.g., for serializing.
  105.      * @return NumberDuration as an object
  106.      */
  107.     public Object getObject()
  108.     {
  109.         if (this.duration != null)
  110.         {
  111.             return this.duration;
  112.         }
  113.         else if (this.doubleScalar != null)
  114.         {
  115.             return this.doubleScalar;
  116.         }
  117.         else if (this.floatScalar != null)
  118.         {
  119.             return this.floatScalar;
  120.         }
  121.         else
  122.         {
  123.             // should never happen
  124.             throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
  125.         }
  126.     }

  127.     /**
  128.      * @return the duration as a Number
  129.      */
  130.     public Number getNumber()
  131.     {
  132.         if (this.duration != null)
  133.         {
  134.             return this.duration;
  135.         }
  136.         else if (this.doubleScalar != null)
  137.         {
  138.             return this.doubleScalar;
  139.         }
  140.         else if (this.floatScalar != null)
  141.         {
  142.             return this.floatScalar;
  143.         }
  144.         else
  145.         {
  146.             // should never happen
  147.             throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
  148.         }
  149.     }

  150.     /**
  151.      * @return the duration as a djunits Duration type
  152.      */
  153.     public Duration getDuration()
  154.     {
  155.         if (this.duration != null)
  156.         {
  157.             return Duration.instantiateSI(this.duration.doubleValue());
  158.         }
  159.         else if (this.doubleScalar != null)
  160.         {
  161.             return this.doubleScalar;
  162.         }
  163.         else if (this.floatScalar != null)
  164.         {
  165.             return new Duration(this.floatScalar.getInUnit(), this.floatScalar.getDisplayUnit());
  166.         }
  167.         else
  168.         {
  169.             // should never happen
  170.             throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
  171.         }
  172.     }

  173.     /**
  174.      * @return the duration as a djunits FloatDuration type
  175.      */
  176.     public FloatDuration getFloatDuration()
  177.     {
  178.         if (this.duration != null)
  179.         {
  180.             return FloatDuration.instantiateSI(this.duration.floatValue());
  181.         }
  182.         else if (this.doubleScalar != null)
  183.         {
  184.             return new FloatDuration((float) this.doubleScalar.getInUnit(), this.doubleScalar.getDisplayUnit());
  185.         }
  186.         else if (this.floatScalar != null)
  187.         {
  188.             return this.floatScalar;
  189.         }
  190.         else
  191.         {
  192.             // should never happen
  193.             throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
  194.         }
  195.     }
  196. }