View Javadoc
1   package org.sim0mq.message.types;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Time;
6   import org.djunits.value.vfloat.scalar.FloatTime;
7   
8   /**
9    * Wrapper for a Number or float/double with Unit of type Time. Store it internally as a Number <b>or</b> as a DoubleScalar,
10   * <b>or</b> as a FloatScalar and have methods to retrieve it in different ways.
11   * <p>
12   * Copyright (c) 2016-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
14   * </p>
15   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
16   * initial version Apr 24, 2017 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class NumberTime extends Number implements Serializable
20  {
21      /** */
22      private static final long serialVersionUID = 20170424L;
23  
24      /** Number - any of the number types. */
25      private final Number time;
26  
27      /** DoubleScalar of type Time. */
28      private final Time doubleScalar;
29  
30      /** FloatScalar of type FloatTime. */
31      private final FloatTime floatScalar;
32  
33      /**
34       * Create a time from a Number.
35       * @param time the time as a Number.
36       */
37      public NumberTime(final Number time)
38      {
39          this.time = time;
40          this.doubleScalar = null;
41          this.floatScalar = null;
42      }
43  
44      /**
45       * Create a time from a DoubleScalar Time type.
46       * @param time the time as a DoubleScalar Time.
47       */
48      public NumberTime(final Time time)
49      {
50          this.time = null;
51          this.doubleScalar = time;
52          this.floatScalar = null;
53      }
54  
55      /**
56       * Create a time from a FloatScalar FloatTime type.
57       * @param time the time as a FloatScalar FloatTime.
58       */
59      public NumberTime(final FloatTime time)
60      {
61          this.time = null;
62          this.doubleScalar = null;
63          this.floatScalar = time;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public int intValue()
69      {
70          return this.time.intValue();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public long longValue()
76      {
77          return this.time.longValue();
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public float floatValue()
83      {
84          return this.time.floatValue();
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public double doubleValue()
90      {
91          return this.time.doubleValue();
92      }
93  
94      /**
95       * Return the NumberTime as an object, e.g., for serializing.
96       * @return NumberTime as an object
97       */
98      public Object getObject()
99      {
100         if (this.time != null)
101         {
102             return this.time;
103         }
104         else if (this.doubleScalar != null)
105         {
106             return this.doubleScalar;
107         }
108         else if (this.floatScalar != null)
109         {
110             return this.floatScalar;
111         }
112         else
113         {
114             // should never happen
115             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
116         }
117     }
118 
119     /**
120      * @return the time as a Number
121      */
122     public Number getNumber()
123     {
124         if (this.time != null)
125         {
126             return this.time;
127         }
128         else if (this.doubleScalar != null)
129         {
130             return this.doubleScalar;
131         }
132         else if (this.floatScalar != null)
133         {
134             return this.floatScalar;
135         }
136         else
137         {
138             // should never happen
139             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
140         }
141     }
142 
143     /**
144      * @return the time as a djunits Time type
145      */
146     public Time getTime()
147     {
148         if (this.time != null)
149         {
150             return Time.createSI(this.time.doubleValue());
151         }
152         else if (this.doubleScalar != null)
153         {
154             return this.doubleScalar;
155         }
156         else if (this.floatScalar != null)
157         {
158             return new Time(this.floatScalar.getInUnit(), this.floatScalar.getUnit());
159         }
160         else
161         {
162             // should never happen
163             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
164         }
165     }
166 
167     /**
168      * @return the time as a djunits FloatTime type
169      */
170     public FloatTime getFloatTime()
171     {
172         if (this.time != null)
173         {
174             return FloatTime.createSI(this.time.floatValue());
175         }
176         else if (this.doubleScalar != null)
177         {
178             return new FloatTime((float) this.doubleScalar.getInUnit(), this.doubleScalar.getUnit());
179         }
180         else if (this.floatScalar != null)
181         {
182             return this.floatScalar;
183         }
184         else
185         {
186             // should never happen
187             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
188         }
189     }
190 }