下面列出了java.time.Duration#multipliedBy ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Duration getNextAttemptDelay(Duration firstBackoff, Duration maxBackoff, int iteration) {
Duration nextBackoff;
try {
nextBackoff = firstBackoff.multipliedBy((long) Math.pow(2, iteration));
if (nextBackoff.compareTo(maxBackoff) > 0) {
nextBackoff = maxBackoff;
}
} catch (ArithmeticException overflow) {
nextBackoff = maxBackoff;
}
return nextBackoff;
}
@Test(dataProvider="MultipliedBy")
public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
Duration t = Duration.ofSeconds(seconds, nanos);
t = t.multipliedBy(multiplicand);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanos);
}
@Test(dataProvider="MultipliedBy")
public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
Duration t = Duration.ofSeconds(seconds, nanos);
t = t.multipliedBy(multiplicand);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanos);
}
static Duration computeBackoff(long iteration, Duration minBackoff, Duration maxBackoff) {
Duration nextBackoff;
try {
nextBackoff = minBackoff.multipliedBy((long) Math.pow(2, iteration));
if (nextBackoff.compareTo(maxBackoff) > 0) {
nextBackoff = maxBackoff;
}
} catch (ArithmeticException overflow) {
nextBackoff = maxBackoff;
}
return nextBackoff;
}
private void backOff(final BackOffWithAnswer<?> backOffWithAnswer) {
final Duration backOffTimeout = this.retryTimeoutStrategy.getNextTimeout();
final Duration resetBackOffTimeout = backOffTimeout.multipliedBy(2L);
log.debug("Going to back off for <{}> until sending answer: <{}>", backOffTimeout, backOffWithAnswer.getAnswer());
this.getTimers().startSingleTimer(InternalTimers.BACK_OFF, new BackOffWithSender<>(getSender(), backOffWithAnswer), backOffTimeout);
this.getTimers().startSingleTimer(InternalTimers.RESET_BACK_OFF, RESET_BACK_OFF, resetBackOffTimeout);
}
@Test(dataProvider="MultipliedBy")
public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
Duration t = Duration.ofSeconds(seconds, nanos);
t = t.multipliedBy(multiplicand);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanos);
}
@Override
public Duration getDelay(int executionCount) {
Duration nextDelay = startDelay;
for (int i = 1; i < executionCount; ++i) {
nextDelay = nextDelay.multipliedBy(2);
}
return maxDelay.compareTo(nextDelay) > 0 ? nextDelay : maxDelay;
}
@Test()
@UseDataProvider("provider_multipliedBy")
public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
Duration t = Duration.ofSeconds(seconds, nanos);
t = t.multipliedBy(multiplicand);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanos);
}
@Test(dataProvider="MultipliedBy")
public void multipliedBy(long seconds, int nanos, int multiplicand, long expectedSeconds, int expectedNanos) {
Duration t = Duration.ofSeconds(seconds, nanos);
t = t.multipliedBy(multiplicand);
assertEquals(t.getSeconds(), expectedSeconds);
assertEquals(t.getNano(), expectedNanos);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MAX_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig_negative() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MIN_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig_negative() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MIN_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MAX_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig_negative() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MIN_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig_negative() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MIN_VALUE);
}
private static Duration nextPollInterval(final Duration duration) {
final Duration nextInterval = duration.multipliedBy(2);
return nextInterval.compareTo(MAX_POLL_INTERVAL) <= 0 ? nextInterval : MAX_POLL_INTERVAL;
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MAX_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MAX_VALUE);
}
@Test(expectedExceptions=ArithmeticException.class)
public void multipliedBy_tooBig() {
Duration test = Duration.ofSeconds(1, 1);
test.multipliedBy(Long.MAX_VALUE);
}
/**
* Supports the multiplication operator; equivalent to calling the {@link Duration#multipliedBy(long)} method.
*
* @param self a Duration
* @param scalar the value to multiply by
* @return a Duration
* @since 2.5.0
*/
public static Duration multiply(final Duration self, long scalar) {
return self.multipliedBy(scalar);
}