View Javadoc
1   package org.sim0mq.message.types;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.DurationUnit;
6   import org.djunits.value.vdouble.scalar.Duration;
7   import org.djunits.value.vfloat.scalar.FloatDuration;
8   
9   /**
10   * Wrapper for a Number or float/double with Unit of type Duration. Store it internally as a Number <b>and</b> as a
11   * DoubleScalar, and have methods to retrieve it in different ways.
12   * <p>
13   * Copyright (c) 2016-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
15   * </p>
16   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17   * initial version Apr 24, 2017 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  public class NumberDuration extends Number implements Serializable
21  {
22      /** */
23      private static final long serialVersionUID = 20170424L;
24  
25      /** Number - any of the number types. */
26      private final Number duration;
27  
28      /** DoubleScalar of type Duration. */
29      private final Duration doubleScalar;
30  
31      /**
32       * Create a duration from a Number.
33       * @param duration the duration as a Number.
34       */
35      public NumberDuration(final Number duration)
36      {
37          this.duration = duration;
38          this.doubleScalar = new Duration(duration.doubleValue(), DurationUnit.SI);
39      }
40  
41      /**
42       * Create a duration from a DoubleScalar Duration type.
43       * @param duration the duration as a DoubleScalar Duration.
44       */
45      public NumberDuration(final Duration duration)
46      {
47          this.duration = duration;
48          this.doubleScalar = duration;
49      }
50  
51      /**
52       * Create a duration from a FloatScalar FloatDuration type.
53       * @param duration the duration as a FloatScalar FloatDuration.
54       */
55      public NumberDuration(final FloatDuration duration)
56      {
57          this.duration = duration;
58          this.doubleScalar = new Duration(duration.getInUnit(), duration.getUnit());
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public int intValue()
64      {
65          return this.duration.intValue();
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public long longValue()
71      {
72          return this.duration.longValue();
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public float floatValue()
78      {
79          return this.duration.floatValue();
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public double doubleValue()
85      {
86          return this.duration.doubleValue();
87      }
88  
89      /**
90       * @return the duration as a djunits Duration type
91       */
92      public Duration getDuration()
93      {
94          return this.doubleScalar;
95      }
96  
97      /**
98       * @return the duration as a djunits FloatDuration type
99       */
100     public FloatDuration getFloatDuration()
101     {
102         return new FloatDuration((float) this.doubleScalar.getInUnit(), this.doubleScalar.getUnit());
103     }
104 }