21 package org.opensplice.dds.core;
23 import java.util.concurrent.TimeUnit;
31 private static final long serialVersionUID = -5170318400663698911L;
34 private long nanoseconds;
35 private long totalNanos;
40 long duration, TimeUnit unit) {
45 "Illegal TimeUnit provided (null).");
48 if (duration == Long.MAX_VALUE) {
49 this.seconds = INFINITE_SECONDS;
50 this.nanoseconds = INFINITE_NANOSECONDS;
51 this.totalNanos = Long.MAX_VALUE;
53 this.seconds = TimeUnit.SECONDS.convert(duration, unit);
54 this.totalNanos = TimeUnit.NANOSECONDS.convert(duration, unit);
55 this.nanoseconds = this.totalNanos
56 - (this.seconds * 1000 * 1000 * 1000);
64 this.seconds = seconds;
65 this.nanoseconds = nanoseconds;
67 if (this.seconds == INFINITE_SECONDS
68 && this.nanoseconds == INFINITE_NANOSECONDS) {
69 this.totalNanos = Long.MAX_VALUE;
71 this.totalNanos = this.seconds * 1000 * 1000 * 1000
77 long sec = this.seconds;
78 long nsec = this.nanoseconds;
80 if (sec == INFINITE_SECONDS) {
82 INFINITE_NANOSECONDS);
84 while (nsec >= 1000000000) {
88 if (sec == INFINITE_SECONDS) {
90 INFINITE_SECONDS, INFINITE_NANOSECONDS);
98 this.seconds = normalized.seconds;
99 this.nanoseconds = normalized.nanoseconds;
100 this.totalNanos = normalized.totalNanos;
108 long sec = o.
getTime(TimeUnit.SECONDS);
109 long nsec = o.
getRemainder(TimeUnit.SECONDS, TimeUnit.NANOSECONDS);
111 if ((this.nanoseconds >= 1000000000)
112 && ((this.nanoseconds != INFINITE_NANOSECONDS) || (this.seconds != INFINITE_SECONDS))) {
114 "Illegal Time " + this.seconds +
" seconds, " 115 + this.nanoseconds +
" nanoseconds.");
117 if ((nsec >= 1000000000)
118 && ((nsec != Long.MAX_VALUE) || (sec != Long.MAX_VALUE))) {
120 "Illegal Time " + sec +
" seconds, " + nsec
123 if (this.seconds > sec)
125 if (this.seconds < sec)
127 if (this.nanoseconds > nsec)
129 if (this.nanoseconds < nsec)
137 if (((ModifiableTimeImpl) other).
compareTo(
this) == 0) {
146 return 31 * 17 + (int) (this.totalNanos ^ (this.totalNanos >>> 32));
158 "Illegal Time (null) provided.");
160 this.seconds = src.
getTime(TimeUnit.SECONDS);
162 TimeUnit.NANOSECONDS);
163 this.totalNanos = this.nanoseconds + this.seconds * 1000 * 1000 * 1000;
169 return new TimeImpl(this.environment, this.seconds, this.nanoseconds);
173 public void setTime(
long time, TimeUnit unit) {
176 "Illegal TimeUnit (null) provided.");
178 this.seconds = TimeUnit.SECONDS.convert(time, unit);
179 this.totalNanos = TimeUnit.NANOSECONDS.convert(time, unit);
180 this.nanoseconds = this.totalNanos
181 - (this.seconds * 1000 * 1000 * 1000);
187 if (duration == null) {
189 "Illegal Duration (null) provided.");
193 "Cannot add duration to invalid time.");
197 TimeUnit.NANOSECONDS);
199 if (!((this.nanoseconds < 1000000000) || (this.nanoseconds == INFINITE_NANOSECONDS))) {
203 if (!((nsec < 1000000000) || (nsec == Long.MAX_VALUE))) {
208 if (this.nanoseconds == INFINITE_NANOSECONDS) {
209 if (this.seconds == INFINITE_SECONDS) {
210 this.seconds = INFINITE_SECONDS;
211 this.nanoseconds = INFINITE_NANOSECONDS;
212 this.totalNanos = Long.MAX_VALUE;
216 "Illegal duration " + this.seconds +
" seconds, " 217 + this.nanoseconds +
" nanoseconds.");
220 if (nsec == Long.MAX_VALUE) {
221 if (sec == Long.MAX_VALUE) {
222 this.seconds = INFINITE_SECONDS;
223 this.nanoseconds = INFINITE_NANOSECONDS;
224 this.totalNanos = Long.MAX_VALUE;
228 "Illegal duration " + sec +
" seconds, " + nsec
232 if (INFINITE_SECONDS - sec <= this.seconds) {
238 this.seconds = INFINITE_SECONDS;
239 this.nanoseconds = INFINITE_NANOSECONDS;
240 this.totalNanos = Long.MAX_VALUE;
245 this.nanoseconds += nsec;
251 public void add(
long duration, TimeUnit unit) {
259 return (this.totalNanos == Long.MAX_VALUE) ? true :
false;
264 if (duration == null) {
266 "Illegal Duration (null) provided.");
270 "Cannot subtract duration from invalid time.");
274 "Cannot subtract infinite duration from infinite time.");
278 TimeUnit.NANOSECONDS);
280 if (!((this.nanoseconds < 1000000000) || (this.nanoseconds == INFINITE_NANOSECONDS))) {
282 "Illegal duration " + this.seconds +
" seconds, " 283 + this.nanoseconds +
" nanoseconds.");
285 if (!((nsec < 1000000000) || (nsec == Long.MAX_VALUE))) {
287 "Illegal duration " + sec +
" seconds, " + nsec
291 if (this.nanoseconds == INFINITE_NANOSECONDS) {
292 if (this.seconds == INFINITE_SECONDS) {
293 this.seconds = INFINITE_SECONDS;
294 this.nanoseconds = INFINITE_NANOSECONDS;
295 this.totalNanos = Long.MAX_VALUE;
299 "Illegal duration " + this.seconds +
" seconds, " 300 + this.nanoseconds +
" nanoseconds.");
302 if (nsec == Long.MAX_VALUE) {
303 if (sec == Long.MAX_VALUE) {
304 this.seconds = INFINITE_SECONDS;
305 this.nanoseconds = INFINITE_NANOSECONDS;
306 this.totalNanos = Long.MAX_VALUE;
310 "Illegal duration " + sec +
" seconds, " + nsec
313 if ((sec <= 0) && (INFINITE_SECONDS + sec <= this.seconds)) {
319 this.seconds = INFINITE_SECONDS;
320 this.nanoseconds = INFINITE_NANOSECONDS;
321 this.totalNanos = Long.MAX_VALUE;
326 if (this.seconds == INFINITE_SECONDS) {
327 this.seconds = INFINITE_SECONDS;
328 this.nanoseconds = INFINITE_NANOSECONDS;
329 this.totalNanos = Long.MAX_VALUE;
332 this.nanoseconds -= nsec;
334 if (this.nanoseconds < 0) {
336 this.nanoseconds += 1000000000;
342 public void subtract(
long duration, TimeUnit unit) {
345 "Illegal TimeUnit (null) provided.");
355 if (inThisUnit == null) {
357 "Illegal TimeUnit (null) provided.");
360 return Long.MAX_VALUE;
362 return inThisUnit.convert(this.totalNanos, TimeUnit.NANOSECONDS);
366 public long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit) {
367 if ((primaryUnit == null) || (remainderUnit == null)) {
369 "Illegal TimeUnit (null) provided.");
371 if (remainderUnit.compareTo(primaryUnit) >= 0) {
375 return Long.MAX_VALUE;
377 long primaryDuration = primaryUnit.convert(this.totalNanos,
378 TimeUnit.NANOSECONDS);
379 long primaryNanoDuration = TimeUnit.NANOSECONDS.convert(
380 primaryDuration, primaryUnit);
381 long leftOverNanos = this.totalNanos - primaryNanoDuration;
383 return remainderUnit.convert(leftOverNanos, TimeUnit.NANOSECONDS);
388 if (this.seconds == DDS.TIMESTAMP_INVALID_SEC.value
389 &&
this.nanoseconds == DDS.TIMESTAMP_INVALID_NSEC.value) {
392 if ((this.nanoseconds >= 1000000000)
393 && ((this.nanoseconds != INFINITE_NANOSECONDS) || (this.seconds != INFINITE_SECONDS))) {
406 return new DDS.Time_t((
int) this.seconds, (
int) this.nanoseconds);
414 return this.totalNanos +
" ns";
abstract long getTime(TimeUnit inThisUnit)
Truncate this time to a whole-number quantity of the given time unit.
void setTime(long time, TimeUnit unit)
abstract long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit)
If getting the magnitude of this time in the given primaryUnit would cause truncation with respect to...
abstract boolean isInfinite()
Report whether this duration lasts forever.
ModifiableTimeImpl(OsplServiceEnvironment environment, long seconds, long nanoseconds)
ModifiableTimeImpl(OsplServiceEnvironment environment, long duration, TimeUnit unit)
static final int INFINITE_SECONDS
ModifiableTimeImpl normalize()
final transient OsplServiceEnvironment environment
boolean equals(Object other)
void subtract(long duration, TimeUnit unit)
abstract long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit)
If getting the magnitude of this duration in the given primaryUnit would cause truncation with respec...
void add(long duration, TimeUnit unit)
abstract long getDuration(TimeUnit inThisUnit)
Truncate this duration to a whole-number quantity of the given time unit.
long getTime(TimeUnit inThisUnit)
ServiceEnvironment getEnvironment()
ModifiableTime modifiableCopy()
A span of elapsed time expressed with nanosecond precision.
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
void subtract(Duration duration)
static final int INFINITE_NANOSECONDS
A moment in time expressed with nanosecond precision (though not necessarily nanosecond accuracy)...
void add(Duration duration)
long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit)