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   import org.sim0mq.Sim0MQException;
8   
9   /**
10   * Wrapper for a Number or float/double with Unit of type Time. Store it internally as a Number <b>or</b> as a DoubleScalar,
11   * <b>or</b> as a FloatScalar and have methods to retrieve it in different ways.
12   * <p>
13   * Copyright (c) 2016-2020 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      /** FloatScalar of type FloatTime. */
32      private final FloatTime floatScalar;
33  
34      /**
35       * Create a time from a Number.
36       * @param time the time as a Number.
37       */
38      public NumberTime(final Number time)
39      {
40          this.time = time;
41          this.doubleScalar = null;
42          this.floatScalar = null;
43      }
44  
45      /**
46       * Create a time from a DoubleScalar Time type.
47       * @param time the time as a DoubleScalar Time.
48       */
49      public NumberTime(final Time time)
50      {
51          this.time = null;
52          this.doubleScalar = time;
53          this.floatScalar = null;
54      }
55  
56      /**
57       * Create a time from a FloatScalar FloatTime type.
58       * @param time the time as a FloatScalar FloatTime.
59       */
60      public NumberTime(final FloatTime time)
61      {
62          this.time = null;
63          this.doubleScalar = null;
64          this.floatScalar = time;
65      }
66  
67      /**
68       * Instantiate a NumberTime based on a value.
69       * @param value the value that can be Time, FloatTime, or Number
70       * @return an instantiation of NumberTime
71       * @throws Sim0MQException if the value is neither Time, FloatTime, nor Number
72       */
73      public static NumberTime instantiate(final Object value) throws Sim0MQException
74      {
75          if (value instanceof Time)
76          {
77              return new NumberTime((Time) value);
78          }
79          else if (value instanceof FloatTime)
80          {
81              return new NumberTime((FloatTime) value);
82          }
83          else if (value instanceof Number)
84          {
85              return new NumberTime((Number) value);
86          }
87          else
88          {
89              throw new Sim0MQException("value should be Number, Time or FloatTime");
90          }
91      }
92    
93      /** {@inheritDoc} */
94      @Override
95      public int intValue()
96      {
97          return this.time.intValue();
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public long longValue()
103     {
104         return this.time.longValue();
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public float floatValue()
110     {
111         return this.time.floatValue();
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public double doubleValue()
117     {
118         return this.time.doubleValue();
119     }
120 
121     /**
122      * Return the NumberTime as an object, e.g., for serializing.
123      * @return NumberTime as an object
124      */
125     public Object getObject()
126     {
127         if (this.time != null)
128         {
129             return this.time;
130         }
131         else if (this.doubleScalar != null)
132         {
133             return this.doubleScalar;
134         }
135         else if (this.floatScalar != null)
136         {
137             return this.floatScalar;
138         }
139         else
140         {
141             // should never happen
142             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
143         }
144     }
145 
146     /**
147      * @return the time as a Number
148      */
149     public Number getNumber()
150     {
151         if (this.time != null)
152         {
153             return this.time;
154         }
155         else if (this.doubleScalar != null)
156         {
157             return this.doubleScalar;
158         }
159         else if (this.floatScalar != null)
160         {
161             return this.floatScalar;
162         }
163         else
164         {
165             // should never happen
166             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
167         }
168     }
169 
170     /**
171      * @return the time as a djunits Time type
172      */
173     public Time getTime()
174     {
175         if (this.time != null)
176         {
177             return Time.instantiateSI(this.time.doubleValue());
178         }
179         else if (this.doubleScalar != null)
180         {
181             return this.doubleScalar;
182         }
183         else if (this.floatScalar != null)
184         {
185             return new Time(this.floatScalar.getInUnit(), this.floatScalar.getDisplayUnit());
186         }
187         else
188         {
189             // should never happen
190             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
191         }
192     }
193 
194     /**
195      * @return the time as a djunits FloatTime type
196      */
197     public FloatTime getFloatTime()
198     {
199         if (this.time != null)
200         {
201             return FloatTime.instantiateSI(this.time.floatValue());
202         }
203         else if (this.doubleScalar != null)
204         {
205             return new FloatTime((float) this.doubleScalar.getInUnit(), this.doubleScalar.getDisplayUnit());
206         }
207         else if (this.floatScalar != null)
208         {
209             return this.floatScalar;
210         }
211         else
212         {
213             // should never happen
214             throw new RuntimeException("NumberTime is neither Number, nor Time, nor FloatTime");
215         }
216     }
217 }