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