21 package org.opensplice.dds.core;
23 import java.util.concurrent.TimeUnit;
29 private static final long serialVersionUID = -3177410803404504645L;
31 private final long seconds;
32 private final long nanoseconds;
33 private final long totalNanos;
40 this.environment = environment;
44 "Invalid TimeUnit (null) provided.");
47 if (duration == Long.MAX_VALUE) {
50 this.totalNanos = Long.MAX_VALUE;
52 this.seconds = TimeUnit.SECONDS.convert(duration, unit);
53 this.totalNanos = TimeUnit.NANOSECONDS.convert(duration, unit);
54 this.nanoseconds = this.totalNanos
55 - (this.seconds * 1000 * 1000 * 1000);
62 this.environment = environment;
63 this.seconds = seconds;
64 this.nanoseconds = nanoseconds;
66 if (this.seconds == INFINITE_SECONDS
67 && this.nanoseconds == INFINITE_NANOSECONDS) {
68 this.totalNanos = Long.MAX_VALUE;
70 this.totalNanos = this.seconds * 1000 * 1000 * 1000
76 long sec = this.seconds;
77 long nsec = this.nanoseconds;
79 if (sec == INFINITE_SECONDS) {
80 return new DurationImpl(this.environment, INFINITE_SECONDS,
81 INFINITE_NANOSECONDS);
83 while (nsec >= 1000000000) {
87 if (sec == INFINITE_SECONDS) {
88 return new DurationImpl(this.environment, INFINITE_SECONDS,
89 INFINITE_NANOSECONDS);
104 long nsec = o.
getRemainder(TimeUnit.SECONDS, TimeUnit.NANOSECONDS);
106 if ((this.nanoseconds >= 1000000000)
107 && ((this.nanoseconds != INFINITE_NANOSECONDS))) {
109 + this.seconds +
" seconds, " + this.nanoseconds
112 if ((nsec >= 1000000000)
113 && ((nsec != Long.MAX_VALUE) || (sec != Long.MAX_VALUE))) {
115 +
" seconds, " + nsec +
" nanoseconds.");
117 if (this.seconds > sec)
119 if (this.seconds < sec)
121 if (this.nanoseconds > nsec)
123 if (this.nanoseconds < nsec)
130 return this.environment;
135 if (inThisUnit == null) {
136 throw new IllegalArgumentException(
137 "Invalid TimeUnit (null) provided.");
140 return Long.MAX_VALUE;
142 return inThisUnit.convert(this.totalNanos, TimeUnit.NANOSECONDS);
146 public long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit) {
147 if ((primaryUnit == null) || (remainderUnit == null)) {
148 throw new IllegalArgumentException(
149 "Invalid TimeUnit (null) provided.");
151 if (remainderUnit.compareTo(primaryUnit) >= 0) {
155 return Long.MAX_VALUE;
157 long primaryDuration = primaryUnit.convert(this.totalNanos,
158 TimeUnit.NANOSECONDS);
159 long primaryNanoDuration = TimeUnit.NANOSECONDS.convert(
160 primaryDuration, primaryUnit);
161 long leftOverNanos = this.totalNanos - primaryNanoDuration;
163 return remainderUnit.convert(leftOverNanos, TimeUnit.NANOSECONDS);
168 return (this.seconds == 0 && this.nanoseconds == 0) ? true :
false;
173 return (this.totalNanos == Long.MAX_VALUE) ? true
179 if (duration == null) {
180 throw new IllegalArgumentException(
181 "Invalid Duration (null) provided.");
185 TimeUnit.NANOSECONDS);
187 if (!((this.nanoseconds < 1000000000) || (this.nanoseconds == INFINITE_NANOSECONDS))) {
191 if (!((nsec < 1000000000) || (nsec == Long.MAX_VALUE))) {
195 if (this.nanoseconds == INFINITE_NANOSECONDS) {
196 if (this.seconds == INFINITE_SECONDS) {
197 return new DurationImpl(this.environment, INFINITE_SECONDS,
198 INFINITE_NANOSECONDS);
201 "Illegal duration " + this.seconds +
" seconds, " 202 + this.nanoseconds +
" nanoseconds.");
204 if (nsec == Long.MAX_VALUE) {
205 if (sec == Long.MAX_VALUE) {
206 return new DurationImpl(this.environment, INFINITE_SECONDS,
207 INFINITE_NANOSECONDS);
210 "Illegal duration " + sec +
" seconds, " + nsec
215 if (INFINITE_SECONDS - sec <= this.seconds) {
221 return new DurationImpl(this.environment, INFINITE_SECONDS,
222 INFINITE_NANOSECONDS);
225 long s = this.seconds + sec;
226 long ns = this.nanoseconds + nsec;
227 return new DurationImpl(this.environment, s, ns).normalize();
236 return this.
add(temp);
241 if (duration == null) {
243 "Invalid Duration (null) provided.");
247 "Cannot subtract two infinite times.");
251 TimeUnit.NANOSECONDS);
254 if (!(this.nanoseconds < 1000000000)
255 || (this.nanoseconds == INFINITE_NANOSECONDS)) {
257 + this.seconds +
" seconds, " + this.nanoseconds
260 if (!((nsec < 1000000000) || (nsec == Long.MAX_VALUE))) {
262 +
" seconds, " + nsec +
" nanoseconds.");
265 if (this.nanoseconds == INFINITE_NANOSECONDS) {
266 if (this.seconds == INFINITE_SECONDS) {
267 return new DurationImpl(this.environment, INFINITE_SECONDS,
268 INFINITE_NANOSECONDS);
271 "Illegal duration " + this.seconds +
" seconds, " 272 + this.nanoseconds +
" nanoseconds.");
274 if (nsec == Long.MAX_VALUE) {
275 if (sec == Long.MAX_VALUE) {
276 return new DurationImpl(this.environment, INFINITE_SECONDS,
277 INFINITE_NANOSECONDS);
280 "Illegal duration " + sec +
" seconds, " + nsec
283 if ((sec <= 0) && (INFINITE_SECONDS + sec <= this.seconds)) {
287 return new DurationImpl(this.environment, INFINITE_SECONDS,
288 INFINITE_NANOSECONDS);
290 long s = this.seconds - sec;
292 if (s == INFINITE_SECONDS) {
293 return new DurationImpl(this.environment, INFINITE_SECONDS,
294 INFINITE_NANOSECONDS);
296 long ns = this.nanoseconds - nsec;
302 return new DurationImpl(this.environment, s, ns).normalize();
315 return new DDS.Duration_t((
int) this.seconds, (
int) this.nanoseconds);
325 if (d.totalNanos !=
this.totalNanos) {
333 return 31 * 17 + (int) (this.totalNanos ^ (this.totalNanos >>> 32));
341 return this.totalNanos +
" ns";
DurationImpl(OsplServiceEnvironment environment, long seconds, long nanoseconds)
long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit)
abstract boolean isInfinite()
Report whether this duration lasts forever.
Duration add(long duration, TimeUnit unit)
int compareTo(Duration o)
static final int INFINITE_SECONDS
boolean equals(Object other)
Duration subtract(long duration, TimeUnit unit)
long getDuration(TimeUnit inThisUnit)
abstract long getRemainder(TimeUnit primaryUnit, TimeUnit remainderUnit)
If getting the magnitude of this duration in the given primaryUnit would cause truncation with respec...
abstract long getDuration(TimeUnit inThisUnit)
Truncate this duration to a whole-number quantity of the given time unit.
ServiceEnvironment getEnvironment()
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 ...
Duration subtract(Duration duration)
DurationImpl(OsplServiceEnvironment environment, long duration, TimeUnit unit)
Duration add(Duration duration)
static final int INFINITE_NANOSECONDS