public static Duration min(Duration x, Duration y) { return x.compareTo(y) <= 0 ? x : y; }
@Override public boolean performCommit(long numberOfMessagesSinceLastCommit, Duration timeSinceLastCommit) { return timeSinceLastCommit.compareTo(minimumTime) >= 0; } }
/** * Returns that duration from the given ones which represents the larger amount * of time ("is longer"). If both durations are equal, that same value will be * returned. */ public static Duration max(Duration d1, Duration d2) { return d1.compareTo(d2) == 1 ? d1 : d2; } }
public void setMaxOperationWaitTime(Duration waitTime) { Preconditions.checkArgument(Duration.ZERO.compareTo(waitTime) < 0, "Wait time must be nonnegative: %s", waitTime); this.maxOperationWaitTime = waitTime; }
public void setMaxOperationWaitTime(Duration waitTime) { Preconditions.checkArgument(Duration.ZERO.compareTo(waitTime) < 0, "Wait time must be nonnegative: %s", waitTime); this.maxOperationWaitTime = waitTime; }
static void validateGreaterThanOrEqual(Duration larger, String largerFieldName, Duration smaller, String smallerFieldName) { if (larger.compareTo(smaller) < 0) { throw new IllegalArgumentException(largerFieldName + " must be greater than or equal to" + smallerFieldName); } }
private static Duration checkLimitRefreshPeriod(Duration limitRefreshPeriod) { requireNonNull(limitRefreshPeriod, LIMIT_REFRESH_PERIOD_MUST_NOT_BE_NULL); boolean refreshPeriodIsTooShort = limitRefreshPeriod.compareTo(ACCEPTABLE_REFRESH_PERIOD) < 0; if (refreshPeriodIsTooShort) { throw new IllegalArgumentException("LimitRefreshPeriod is too short"); } return limitRefreshPeriod; }
@Override public int compareTo(final Message o) { int i; if (text != null && (i = text.compareTo(o.text)) != 0) { return i; } if (duration != null && (i = duration.compareTo(o.duration)) != 0) { return i; } return tags.size() - o.tags.size(); }
/** * Calculates how much time is left before the trap is collapsing. * * @return Value between 0 and 1. 0 means the trap was laid moments ago. * 1 is a trap that's about to collapse. */ public double getTrapTimeRelative() { Duration duration = Duration.between(placedOn, Instant.now()); return duration.compareTo(TRAP_TIME) < 0 ? (double) duration.toMillis() / TRAP_TIME.toMillis() : 1; }
double getRemainingFuseTimeRelative() { Duration duration = Duration.between(creationTime, Instant.now()); return duration.compareTo(FUSE_TIME) < 0 ? (double) duration.toMillis() / FUSE_TIME.toMillis() : 1; }
double getRemainingTimeRelative() { Duration duration = Duration.between(creationTime, Instant.now()); return duration.compareTo(PLANT_TIME) < 0 ? (double) duration.toMillis() / PLANT_TIME.toMillis() : 1; } }
public double getPlantTimeRelative() { Duration duration = Duration.between(planted, Instant.now()); return duration.compareTo(PLANT_TIME) < 0 ? (double) duration.toMillis() / PLANT_TIME.toMillis() : 1; } }
@Override protected void action() { MessageEnvelope msg; //Opportunistically drain the queue for up to the batch-send-size number of messages before evaluating condition while (toSend.size()<sendBatchSize && (msg=outgoingMsg.poll())!=null) { toSend.add(msg); } //Evaluate send condition: 1) Is the oldest message waiting longer than the delay? or 2) Do we have enough messages to send? if (!toSend.isEmpty() && (maxSendDelay.compareTo(timeSinceFirstMsg()) <= 0 || toSend.size() >= sendBatchSize)) { try { sendMessages(toSend); } finally { toSend.clear(); } } }
@Override public StepVerifier.Assertions tookMoreThan(Duration d) { return satisfies(() -> duration.compareTo(d) >= 0, () -> String.format("Expected scenario to be verified in more than %sms, took %sms.", d.toMillis(), duration.toMillis())); } }
@Override public StepVerifier.Assertions tookLessThan(Duration d) { return satisfies(() -> duration.compareTo(d) <= 0, () -> String.format("Expected scenario to be verified in less than %sms, took %sms.", d.toMillis(), duration.toMillis())); }
DelaySubscriber(CoreSubscriber<? super T> actual, Duration delay, Scheduler.Worker w) { super(); this.actual = new SerializedSubscriber<>(actual); this.w = w; if (delay.compareTo(Duration.ofMinutes(1)) < 0) { this.delay = delay.toNanos(); this.timeUnit = TimeUnit.NANOSECONDS; } else { this.delay = delay.toMillis(); this.timeUnit = TimeUnit.MILLISECONDS; } }
@Override protected Duration computeNextWaitTime() { Duration next = mNextSleep; mNextSleep = mNextSleep.multipliedBy(2); if (mNextSleep.compareTo(mMaxSleep) > 0) { mNextSleep = mMaxSleep; } // Add jitter. long jitter = Math.round(ThreadLocalRandom.current().nextDouble(0.1) * next.toMillis()); return next.plusMillis(jitter); }
private static SnapshotDto findNearestSnapshotToTargetDate(List<SnapshotDto> snapshots, Instant targetDate) { // FIXME shouldn't this be the first analysis after targetDate? Duration bestDuration = null; SnapshotDto nearest = null; for (SnapshotDto snapshot : snapshots) { Instant createdAt = Instant.ofEpochMilli(snapshot.getCreatedAt()); Duration duration = Duration.between(targetDate, createdAt).abs(); if (bestDuration == null || duration.compareTo(bestDuration) <= 0) { bestDuration = duration; nearest = snapshot; } } return nearest; }
/** * Wrap a limiter such that acquire will block up to a provided timeout if the limit was reached * instead of return an empty listener immediately * * @param delegate Non-blocking limiter to wrap * @param timeout Max amount of time to wait for the wait for the limit to be released. Cannot exceed {@link BlockingLimiter#MAX_TIMEOUT} * @return Wrapped limiter */ public static <ContextT> BlockingLimiter<ContextT> wrap(Limiter<ContextT> delegate, Duration timeout) { Preconditions.checkArgument(timeout.compareTo(MAX_TIMEOUT) < 0, "Timeout cannot be greater than " + MAX_TIMEOUT); return new BlockingLimiter<>(delegate, timeout); }