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   
8   /**
9    * Wrapper for a Number or float/double with Unit of type Duration. 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 NumberDuration 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 duration;
26  
27      /** DoubleScalar of type Duration. */
28      private final Duration doubleScalar;
29  
30      /** FloatScalar of type FloatDuration. */
31      private final FloatDuration floatScalar;
32  
33      /**
34       * Create a duration from a Number.
35       * @param duration the duration as a Number.
36       */
37      public NumberDuration(final Number duration)
38      {
39          this.duration = duration;
40          this.doubleScalar = null;
41          this.floatScalar = null;
42      }
43  
44      /**
45       * Create a duration from a DoubleScalar Duration type.
46       * @param duration the duration as a DoubleScalar Duration.
47       */
48      public NumberDuration(final Duration duration)
49      {
50          this.duration = null;
51          this.doubleScalar = duration;
52          this.floatScalar = null;
53      }
54  
55      /**
56       * Create a duration from a FloatScalar FloatDuration type.
57       * @param duration the duration as a FloatScalar FloatDuration.
58       */
59      public NumberDuration(final FloatDuration duration)
60      {
61          this.duration = null;
62          this.doubleScalar = null;
63          this.floatScalar = duration;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public int intValue()
69      {
70          return this.duration.intValue();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public long longValue()
76      {
77          return this.duration.longValue();
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public float floatValue()
83      {
84          return this.duration.floatValue();
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public double doubleValue()
90      {
91          return this.duration.doubleValue();
92      }
93  
94      /**
95       * Return the NumberDuration as an object, e.g., for serializing.
96       * @return NumberDuration as an object
97       */
98      public Object getObject()
99      {
100         if (this.duration != null)
101         {
102             return this.duration;
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("NumberDuration is neither Number, nor Duration, nor FloatDuration");
116         }
117     }
118 
119     /**
120      * @return the duration as a Number
121      */
122     public Number getNumber()
123     {
124         if (this.duration != null)
125         {
126             return this.duration;
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("NumberDuration is neither Number, nor Duration, nor FloatDuration");
140         }
141     }
142 
143     /**
144      * @return the duration as a djunits Duration type
145      */
146     public Duration getDuration()
147     {
148         if (this.duration != null)
149         {
150             return Duration.createSI(this.duration.doubleValue());
151         }
152         else if (this.doubleScalar != null)
153         {
154             return this.doubleScalar;
155         }
156         else if (this.floatScalar != null)
157         {
158             return new Duration(this.floatScalar.getInUnit(), this.floatScalar.getUnit());
159         }
160         else
161         {
162             // should never happen
163             throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
164         }
165     }
166 
167     /**
168      * @return the duration as a djunits FloatDuration type
169      */
170     public FloatDuration getFloatDuration()
171     {
172         if (this.duration != null)
173         {
174             return FloatDuration.createSI(this.duration.floatValue());
175         }
176         else if (this.doubleScalar != null)
177         {
178             return new FloatDuration((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("NumberDuration is neither Number, nor Duration, nor FloatDuration");
188         }
189     }
190 }