Skip to content

Commit 1503036

Browse files
committed
Add methods to ParsedDuration for adding to Instant/calculating delay
1 parent 568a52a commit 1503036

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

SimpleAPI/src/main/java/com/bencodez/simpleapi/time/ParsedDuration.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bencodez.simpleapi.time;
22

33
import java.time.Duration;
4+
import java.time.Instant;
45
import java.util.Locale;
56
import java.util.Objects;
67
import java.util.regex.Matcher;
@@ -296,6 +297,28 @@ private static ParsedDuration applySuffix(long value, String suffixRaw, Unit def
296297
return applyDefaultUnit(Long.toString(value), defaultUnit);
297298
}
298299
}
300+
301+
/**
302+
* Adds this duration to the provided instant using fixed millis.
303+
*/
304+
public Instant addTo(Instant base) {
305+
if (base == null || isEmpty()) {
306+
return base;
307+
}
308+
return base.plusMillis(millis);
309+
}
310+
311+
/**
312+
* Delay in milliseconds from now until now + this duration.
313+
*
314+
* Guaranteed to return at least 1ms when not empty.
315+
*/
316+
public long delayMillisFromNow() {
317+
if (isEmpty()) {
318+
return 1L;
319+
}
320+
return millis <= 0L ? 1L : millis;
321+
}
299322

300323
private static long safeMul(long a, long b) {
301324
if (a == 0L || b == 0L) {

0 commit comments

Comments
 (0)