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
10
11
12
13
14
15
16
17
18
19 public class NumberDuration extends Number implements Serializable
20 {
21
22 private static final long serialVersionUID = 20170424L;
23
24
25 private final Number duration;
26
27
28 private final Duration doubleScalar;
29
30
31 private final FloatDuration floatScalar;
32
33
34
35
36
37 public NumberDuration(final Number duration)
38 {
39 this.duration = duration;
40 this.doubleScalar = null;
41 this.floatScalar = null;
42 }
43
44
45
46
47
48 public NumberDuration(final Duration duration)
49 {
50 this.duration = null;
51 this.doubleScalar = duration;
52 this.floatScalar = null;
53 }
54
55
56
57
58
59 public NumberDuration(final FloatDuration duration)
60 {
61 this.duration = null;
62 this.doubleScalar = null;
63 this.floatScalar = duration;
64 }
65
66
67 @Override
68 public int intValue()
69 {
70 return this.duration.intValue();
71 }
72
73
74 @Override
75 public long longValue()
76 {
77 return this.duration.longValue();
78 }
79
80
81 @Override
82 public float floatValue()
83 {
84 return this.duration.floatValue();
85 }
86
87
88 @Override
89 public double doubleValue()
90 {
91 return this.duration.doubleValue();
92 }
93
94
95
96
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
115 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
116 }
117 }
118
119
120
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
139 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
140 }
141 }
142
143
144
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
163 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
164 }
165 }
166
167
168
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
187 throw new RuntimeException("NumberDuration is neither Number, nor Duration, nor FloatDuration");
188 }
189 }
190 }