View Javadoc
1   package org.sim0mq.message.types;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.TimeUnit;
6   import org.djunits.value.vdouble.scalar.Time;
7   import org.djunits.value.vfloat.scalar.FloatTime;
8   
9   /**
10   * Wrapper for a Number or float/double with Unit of type Time. Store it internally as a Number <b>and</b> as a DoubleScalar,
11   * 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 NumberTime 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 time;
27  
28      /** DoubleScalar of type Time. */
29      private final Time doubleScalar;
30  
31      /**
32       * Create a duration from a Number.
33       * @param time the duration as a Number.
34       */
35      public NumberTime(final Number time)
36      {
37          this.time = time;
38          this.doubleScalar = new Time(time.doubleValue(), TimeUnit.BASE_SECOND);
39      }
40  
41      /**
42       * Create a duration from a DoubleScalar Time type.
43       * @param time the duration as a DoubleScalar Time.
44       */
45      public NumberTime(final Time time)
46      {
47          this.time = time;
48          this.doubleScalar = time;
49      }
50  
51      /**
52       * Create a duration from a FloatScalar FloatTime type.
53       * @param time the duration as a FloatScalar FloatTime.
54       */
55      public NumberTime(final FloatTime time)
56      {
57          this.time = time;
58          this.doubleScalar = new Time(time.getInUnit(), time.getUnit());
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public int intValue()
64      {
65          return this.time.intValue();
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public long longValue()
71      {
72          return this.time.longValue();
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public float floatValue()
78      {
79          return this.time.floatValue();
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public double doubleValue()
85      {
86          return this.time.doubleValue();
87      }
88  
89      /**
90       * @return the duration as a djunits Time type
91       */
92      public Time getTime()
93      {
94          return this.doubleScalar;
95      }
96  
97      /**
98       * @return the duration as a djunits FloatTime type
99       */
100     public FloatTime getFloatTime()
101     {
102         return new FloatTime((float) this.doubleScalar.getInUnit(), this.doubleScalar.getUnit());
103     }
104 }