Example usage for java.time Duration toNanos

List of usage examples for java.time Duration toNanos

Introduction

In this page you can find the example usage for java.time Duration toNanos.

Prototype

public long toNanos() 

Source Link

Document

Converts this duration to the total length in nanoseconds expressed as a long .

Usage

From source file:Main.java

public static void main(String[] args) {
    Duration duration = Duration.between(LocalTime.MIDNIGHT, LocalTime.NOON);
    System.out.println(duration.toNanos());

}

From source file:net.havox.times.model.times.impl.WorkUnitImpl.java

@Override
public void setWorkUnitDuration(LocalDateTime start, Duration duration) {
    if ((start == null) || (duration == null)) {
        String message = "Neigther the parameter 'start'=" + start + " nor the parameter 'duration'=" + duration
                + "is allowed to be NULL.";
        throw new GuruMeditationWarning(ILLEGAL_ARGUMENT, message);
    }/*from   w w  w .j  a  v  a  2 s  .c  om*/

    this.workUnitStart = start;
    this.workUnitEnd = start.plus(duration.toNanos(), ChronoUnit.NANOS);
}

From source file:org.janusgraph.diskstorage.configuration.CommonConfigTest.java

@Test
public void testDateParsing() {
    BaseConfiguration base = new BaseConfiguration();
    CommonsConfiguration config = new CommonsConfiguration(base);

    for (ChronoUnit unit : Arrays.asList(ChronoUnit.NANOS, ChronoUnit.MICROS, ChronoUnit.MILLIS,
            ChronoUnit.SECONDS, ChronoUnit.MINUTES, ChronoUnit.HOURS, ChronoUnit.DAYS)) {
        base.setProperty("test", "100 " + unit.toString());
        Duration d = config.get("test", Duration.class);
        assertEquals(TimeUnit.NANOSECONDS.convert(100, Temporals.timeUnit(unit)), d.toNanos());
    }//w  w w  . ja  v a 2s  .com

    Map<ChronoUnit, String> mapping = ImmutableMap.of(ChronoUnit.MICROS, "us", ChronoUnit.DAYS, "d");
    for (Map.Entry<ChronoUnit, String> entry : mapping.entrySet()) {
        base.setProperty("test", "100 " + entry.getValue());
        Duration d = config.get("test", Duration.class);
        assertEquals(TimeUnit.NANOSECONDS.convert(100, Temporals.timeUnit(entry.getKey())), d.toNanos());
    }

}

From source file:org.janusgraph.graphdb.log.StandardTransactionLogProcessor.java

public StandardTransactionLogProcessor(StandardJanusGraph graph, Instant startTime) {
    Preconditions.checkArgument(graph != null && graph.isOpen());
    Preconditions.checkArgument(startTime != null);
    Preconditions.checkArgument(graph.getConfiguration().hasLogTransactions(),
            "Transaction logging must be enabled for recovery to work");
    Duration maxTxLength = graph.getConfiguration().getMaxCommitTime();
    if (maxTxLength.compareTo(MIN_TX_LENGTH) < 0)
        maxTxLength = MIN_TX_LENGTH;/*  ww  w  .j  av  a2  s.  co m*/
    Preconditions.checkArgument(maxTxLength != null && !maxTxLength.isZero(),
            "Max transaction time cannot be 0");
    this.graph = graph;
    this.serializer = graph.getDataSerializer();
    this.times = graph.getConfiguration().getTimestampProvider();
    this.txLog = graph.getBackend().getSystemTxLog();
    this.persistenceTime = graph.getConfiguration().getMaxWriteTime();
    this.verboseLogging = graph.getConfiguration().getConfiguration()
            .get(GraphDatabaseConfiguration.VERBOSE_TX_RECOVERY);
    this.txCache = CacheBuilder.newBuilder().concurrencyLevel(2).initialCapacity(100)
            .expireAfterWrite(maxTxLength.toNanos(), TimeUnit.NANOSECONDS)
            .removalListener(new RemovalListener<StandardTransactionId, TxEntry>() {
                @Override
                public void onRemoval(RemovalNotification<StandardTransactionId, TxEntry> notification) {
                    RemovalCause cause = notification.getCause();
                    Preconditions.checkArgument(cause == RemovalCause.EXPIRED,
                            "Unexpected removal cause [%s] for transaction [%s]", cause, notification.getKey());
                    TxEntry entry = notification.getValue();
                    if (entry.status == LogTxStatus.SECONDARY_FAILURE
                            || entry.status == LogTxStatus.PRIMARY_SUCCESS) {
                        failureTxCounter.incrementAndGet();
                        fixSecondaryFailure(notification.getKey(), entry);
                    } else {
                        successTxCounter.incrementAndGet();
                    }
                }
            }).build();

    ReadMarker start = ReadMarker.fromTime(startTime);
    this.txLog.registerReader(start, new TxLogMessageReader());

    cleaner = new BackgroundCleaner();
    cleaner.start();
}