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
11
12
13
14
15
16
17
18
19
20 public class NumberDuration extends Number implements Serializable
21 {
22
23 private static final long serialVersionUID = 20170424L;
24
25
26 private final Number duration;
27
28
29 private final Duration doubleScalar;
30
31
32 private final FloatDuration floatScalar;
33
34
35
36
37
38 public NumberDuration(final Number duration)
39 {
40 this.duration = duration;
41 this.doubleScalar = null;
42 this.floatScalar = null;
43 }
44
45
46
47
48
49 public NumberDuration(final Duration duration)
50 {
51 this.duration = null;
52 this.doubleScalar = duration;
53 this.floatScalar = null;
54 }
55
56
57
58
59
60 public NumberDuration(final FloatDuration duration)
61 {
62 this.duration = null;
63 this.doubleScalar = null;
64 this.floatScalar = duration;
65 }
66
67
68
69
70
71
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
94 @Override
95 public int intValue()
96 {
97 return this.duration.intValue();
98 }
99
100
101 @Override
102 public long longValue()
103 {
104 return this.duration.longValue();
105 }
106
107
108 @Override
109 public float floatValue()
110 {
111 return this.duration.floatValue();
112 }
113
114
115 @Override
116 public double doubleValue()
117 {
118 return this.duration.doubleValue();
119 }
120
121
122
123
124
125 public Object getObject()
126 {
127 if (this.duration != null)
128 {
129 return this.duration;
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
142 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
143 }
144 }
145
146
147
148
149 public Number getNumber()
150 {
151 if (this.duration != null)
152 {
153 return this.duration;
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
166 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
167 }
168 }
169
170
171
172
173 public Duration getDuration()
174 {
175 if (this.duration != null)
176 {
177 return Duration.instantiateSI(this.duration.doubleValue());
178 }
179 else if (this.doubleScalar != null)
180 {
181 return this.doubleScalar;
182 }
183 else if (this.floatScalar != null)
184 {
185 return new Duration(this.floatScalar.getInUnit(), this.floatScalar.getDisplayUnit());
186 }
187 else
188 {
189
190 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
191 }
192 }
193
194
195
196
197 public FloatDuration getFloatDuration()
198 {
199 if (this.duration != null)
200 {
201 return FloatDuration.instantiateSI(this.duration.floatValue());
202 }
203 else if (this.doubleScalar != null)
204 {
205 return new FloatDuration((float) this.doubleScalar.getInUnit(), this.doubleScalar.getDisplayUnit());
206 }
207 else if (this.floatScalar != null)
208 {
209 return this.floatScalar;
210 }
211 else
212 {
213
214 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
215 }
216 }
217 }