本文整理了Java中java.time.Duration.ofNanos()
方法的一些代码示例,展示了Duration.ofNanos()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Duration.ofNanos()
方法的具体详情如下:
包路径:java.time.Duration
类名称:Duration
方法名:ofNanos
[英]Obtains an instance of Duration from a number of nanoseconds.
The seconds and nanoseconds are extracted from the specified nanoseconds.
[中]从纳秒数获取持续时间的实例。
秒和纳秒从指定的纳秒中提取。
代码示例来源:origin: hibernate/hibernate-orm
@Override
public Duration fromString(String string) {
if ( string == null ) {
return null;
}
return Duration.ofNanos( Long.valueOf( string ) );
}
代码示例来源:origin: apache/flink
/**
* Returns the time left between the deadline and now. The result is negative if the deadline
* has passed.
*/
public Duration timeLeft() {
return Duration.ofNanos(Math.subtractExact(timeNanos, System.nanoTime()));
}
代码示例来源:origin: lettuce-io/lettuce-core
/**
* Set the default timeout for connections created by this client. The timeout applies to connection attempts and
* non-blocking commands.
*
* @param timeout Default connection timeout.
* @param unit Unit of time for the timeout.
* @deprecated since 5.0, use {@link #setDefaultTimeout(Duration)}.
*/
@Deprecated
public void setDefaultTimeout(long timeout, TimeUnit unit) {
setDefaultTimeout(Duration.ofNanos(unit.toNanos(timeout)));
}
代码示例来源:origin: lettuce-io/lettuce-core
/**
* Set the command timeout for this connection.
*
* @param timeout Command timeout.
* @param unit Unit of time for the timeout.
* @deprecated since 5.0, use {@link #setTimeout(Duration)}
*/
@Deprecated
public void setTimeout(long timeout, TimeUnit unit) {
setTimeout(Duration.ofNanos(unit.toNanos(timeout)));
}
代码示例来源:origin: apache/ignite
/**
* Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link
* #elapsed(TimeUnit)}, this method does not lose any precision due to rounding.
*/
public Duration elapsed() {
return Duration.ofNanos(elapsedNanos());
}
代码示例来源:origin: lettuce-io/lettuce-core
@Override
public Duration createDelay(long attempt) {
long value = randomBetween(base, Math.max(base, prevDelay * 3));
Duration delay = applyBounds(Duration.ofNanos(targetTimeUnit.toNanos(value)), lower, upper);
prevDelay = delay.toNanos();
return delay;
}
代码示例来源:origin: google/guava
/**
* Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link
* #elapsed(TimeUnit)}, this method does not lose any precision due to rounding.
*
* @since 22.0
*/
@GwtIncompatible
@J2ObjCIncompatible
public Duration elapsed() {
return Duration.ofNanos(elapsedNanos());
}
代码示例来源:origin: ben-manes/caffeine
/**
* Returns the fixed duration used to determine if an entry should be automatically removed due
* to elapsing this time bound. An entry is considered fresh if its age is less than this
* duration, and stale otherwise. The expiration policy determines when the entry's age is
* reset.
*
* @return the length of time after which an entry should be automatically removed
*/
@NonNull
default Duration getExpiresAfter() {
// This method will be abstract in version 3.0.0
return Duration.ofNanos(getExpiresAfter(TimeUnit.NANOSECONDS));
}
代码示例来源:origin: resilience4j/resilience4j
private void publishSuccessEvent(final long durationInNanos) {
final CircuitBreakerOnSuccessEvent event = new CircuitBreakerOnSuccessEvent(name, Duration.ofNanos(durationInNanos));
publishEventIfPossible(event);
}
代码示例来源:origin: ben-manes/caffeine
/**
* Returns the duration until the entry should be automatically removed. The expiration policy
* determines when the entry's duration is reset.
*
* @param key the key for the entry being queried
* @return the duration if the entry is present in the cache
*/
@NonNull
default Optional<Duration> getExpiresAfter(@NonNull K key) {
// This method will be abstract in version 3.0.0
OptionalLong duration = getExpiresAfter(key, TimeUnit.NANOSECONDS);
return duration.isPresent()
? Optional.of(Duration.ofNanos(duration.getAsLong()))
: Optional.empty();
}
代码示例来源:origin: prestodb/presto
/**
* Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link
* #elapsed(TimeUnit)}, this method does not lose any precision due to rounding.
*
* @since 22.0
*/
@GwtIncompatible
@J2ObjCIncompatible
public Duration elapsed() {
return Duration.ofNanos(elapsedNanos());
}
代码示例来源:origin: lettuce-io/lettuce-core
@Override
public Duration createDelay(long attempt) {
long delay;
if (attempt <= 0) { // safeguard against underflow
delay = 0;
} else if (powersOf == 2) {
delay = calculatePowerOfTwo(attempt);
} else {
delay = calculateAlternatePower(attempt);
}
return applyBounds(Duration.ofNanos(targetTimeUnit.toNanos(delay)));
}
代码示例来源:origin: lettuce-io/lettuce-core
@Override
public Duration createDelay(long attempt) {
long value = randomBetween(0, base * calculatePowerOfTwo(attempt));
return applyBounds(Duration.ofNanos(targetTimeUnit.toNanos(value)));
}
}
代码示例来源:origin: wildfly/wildfly
/**
* Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link
* #elapsed(TimeUnit)}, this method does not lose any precision due to rounding.
*
* @since 22.0
*/
@GwtIncompatible
@J2ObjCIncompatible
public Duration elapsed() {
return Duration.ofNanos(elapsedNanos());
}
代码示例来源:origin: google/guava
@GwtIncompatible
public void testElapsed_duration() {
stopwatch.start();
ticker.advance(999999);
assertEquals(Duration.ofNanos(999999), stopwatch.elapsed());
ticker.advance(1);
assertEquals(Duration.ofMillis(1), stopwatch.elapsed());
}
代码示例来源:origin: ben-manes/caffeine
@Test
public void expireAfterAccess_duration_large() {
Caffeine<?, ?> builder = Caffeine.newBuilder()
.expireAfterAccess(Duration.ofNanos(Integer.MAX_VALUE));
assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE));
Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get();
assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}
代码示例来源:origin: ben-manes/caffeine
@Test
public void expireAfterWrite_duration_large() {
Caffeine<?, ?> builder = Caffeine.newBuilder()
.expireAfterWrite(Duration.ofNanos(Integer.MAX_VALUE));
assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE));
Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get();
assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}
代码示例来源:origin: reactor/reactor-core
@Test
public void monoProcessorBlockIsUnbounded() {
long start = System.nanoTime();
String result = Mono.just("foo")
.delayElement(Duration.ofMillis(500))
.toProcessor()
.block();
assertThat(result).isEqualTo("foo");
assertThat(Duration.ofNanos(System.nanoTime() - start))
.isGreaterThanOrEqualTo(Duration.ofMillis(500));
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void validateWhenBothExpiryAndNotBeforeAreValidThenReturnsSuccessfulResult() {
Jwt jwt = new Jwt(
MOCK_TOKEN_VALUE,
MOCK_ISSUED_AT,
Instant.now(MOCK_NOW),
MOCK_HEADER,
Collections.singletonMap(JwtClaimNames.NBF, Instant.now(MOCK_NOW)));
JwtTimestampValidator jwtValidator = new JwtTimestampValidator(Duration.ofNanos(0));
jwtValidator.setClock(MOCK_NOW);
assertThat(jwtValidator.validate(jwt).hasErrors()).isFalse();
}
代码示例来源:origin: resilience4j/resilience4j
@Before
public void setup() {
rateLimiterConfig = RateLimiterConfig.custom()
.limitForPeriod(PERMISSIONS_RER_CYCLE)
.limitRefreshPeriod(Duration.ofNanos(CYCLE_IN_NANOS))
.timeoutDuration(Duration.ZERO)
.build();
AtomicRateLimiter testLimiter = new AtomicRateLimiter(LIMITER_NAME, rateLimiterConfig);
rateLimiter = PowerMockito.spy(testLimiter);
metrics = rateLimiter.getDetailedMetrics();
}
内容来源于网络,如有侵权,请联系作者删除!