List of utility methods to do Duration Calculate
long | getExpirationMillis(long now, Duration duration) get Expiration Millis try { return duration.plusMillis(now).toMillis(); } catch (ArithmeticException e) { return Long.MAX_VALUE; |
T | getOrThrow(final ListenableFuture get Or Throw try { return Future.get(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { throw Throwables.propagate(e); |
int | getTTLSeconds(Duration duration) get TTL Seconds Preconditions.checkArgument(duration != null && !duration.isZero(), "Must provide non-zero TTL"); long ttlSeconds = Math.max(1, duration.getSeconds()); assert ttlSeconds > 0; Preconditions.checkArgument(ttlSeconds <= Integer.MAX_VALUE, "tll value is too large [%s] - value overflow", duration); return (int) ttlSeconds; |
boolean | isDuration(String value) is Duration try { Duration.parse(value); return true; } catch (Exception e) { return false; |
boolean | isFinite(Duration duration) is Finite return !isInfPositive(duration) && !isInfNegative(duration);
|
boolean | isInfPositive(Duration duration) is Inf Positive return INF_POSITIVE.equals(duration);
|
Duration | max(Duration a, Duration b) max return a.compareTo(b) >= 0 ? a : b;
|
Duration | min(Duration a, Duration b) min return (a.compareTo(b) <= 0) ? a : b;
|
Duration | minutesToDuration(long minutes) minutes To Duration return Duration.ofMinutes(minutes);
|
Duration | multiply(Duration duration, double factor) multiply final long nanosPerSecond = ChronoUnit.SECONDS.getDuration().toNanos(); final long nanos = (long) (duration.getNano() * factor); return Duration.ofSeconds( Math.addExact((long) (duration.getSeconds() * factor), Math.floorDiv(nanos, nanosPerSecond)), Math.floorMod(nanos, nanosPerSecond)); |