Java Code Examples for java.util.concurrent.TimeUnit#convert()

The following examples show how to use java.util.concurrent.TimeUnit#convert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the value at the given unit of granularity.
 *
 * <p> Conversion from a coarser granularity that would numerically overflow
 * saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE}
 * if positive.
 *
 * @param   unit
 *          the unit of granularity for the return value
 *
 * @return  value in the given unit of granularity, since the epoch
 *          since the epoch (1970-01-01T00:00:00Z); can be negative
 */
public long to(TimeUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (this.unit != null) {
        return unit.convert(this.value, this.unit);
    } else {
        long secs = unit.convert(instant.getEpochSecond(), TimeUnit.SECONDS);
        if (secs == Long.MIN_VALUE || secs == Long.MAX_VALUE) {
            return secs;
        }
        long nanos = unit.convert(instant.getNano(), TimeUnit.NANOSECONDS);
        long r = secs + nanos;
        // Math.addExact() variant
        if (((secs ^ r) & (nanos ^ r)) < 0) {
            return (secs < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
        }
        return r;
    }
}
 
Example 2
Source File: FileTime.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the value at the given unit of granularity.
 *
 * <p> Conversion from a coarser granularity that would numerically overflow
 * saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE}
 * if positive.
 *
 * @param   unit
 *          the unit of granularity for the return value
 *
 * @return  value in the given unit of granularity, since the epoch
 *          since the epoch (1970-01-01T00:00:00Z); can be negative
 */
public long to(TimeUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (this.unit != null) {
        return unit.convert(this.value, this.unit);
    } else {
        long secs = unit.convert(instant.getEpochSecond(), TimeUnit.SECONDS);
        if (secs == Long.MIN_VALUE || secs == Long.MAX_VALUE) {
            return secs;
        }
        long nanos = unit.convert(instant.getNano(), TimeUnit.NANOSECONDS);
        long r = secs + nanos;
        // Math.addExact() variant
        if (((secs ^ r) & (nanos ^ r)) < 0) {
            return (secs < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
        }
        return r;
    }
}
 
Example 3
Source File: FileTime.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the value at the given unit of granularity.
 *
 * <p> Conversion from a coarser granularity that would numerically overflow
 * saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE}
 * if positive.
 *
 * @param   unit
 *          the unit of granularity for the return value
 *
 * @return  value in the given unit of granularity, since the epoch
 *          since the epoch (1970-01-01T00:00:00Z); can be negative
 */
public long to(TimeUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (this.unit != null) {
        return unit.convert(this.value, this.unit);
    } else {
        long secs = unit.convert(instant.getEpochSecond(), TimeUnit.SECONDS);
        if (secs == Long.MIN_VALUE || secs == Long.MAX_VALUE) {
            return secs;
        }
        long nanos = unit.convert(instant.getNano(), TimeUnit.NANOSECONDS);
        long r = secs + nanos;
        // Math.addExact() variant
        if (((secs ^ r) & (nanos ^ r)) < 0) {
            return (secs < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
        }
        return r;
    }
}
 
Example 4
Source File: AbstractTestTimeSource.java    From kieker with Apache License 2.0 5 votes vote down vote up
public final void testTime(final ITimeSource timesource, final TimeUnit timeunit) { // NOPMD (only used by other tests)
	final long before = System.currentTimeMillis();
	final long measured = timesource.getTime();
	final long after = System.currentTimeMillis();

	final long beforeTU = timeunit.convert(before - 2, TimeUnit.MILLISECONDS);
	final long afterTU = timeunit.convert(after + 2, TimeUnit.MILLISECONDS); // choosing 2 because 1 occasionally fails on some machines (with nanos)

	Assert.assertTrue("Measured time (" + measured + ") has to be >= " + beforeTU, beforeTU <= measured);
	Assert.assertTrue("Measured time (" + measured + ") has to be <= " + afterTU, measured <= afterTU);
}
 
Example 5
Source File: Event.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
@Override
public long getDelay(TimeUnit unit) {
    if (time == 0) {
        return 0;
    }
    long delay = time - System.currentTimeMillis();
    if (delay <= 0) {
        return 0;
    }
    return unit.convert(delay, TimeUnit.MILLISECONDS);
}
 
Example 6
Source File: HarTimings.java    From CapturePacket with MIT License 5 votes vote down vote up
public long getDns(TimeUnit timeUnit) {
    if (dnsNanos == -1) {
        return -1;
    } else {
        return timeUnit.convert(dnsNanos, TimeUnit.NANOSECONDS);
    }
}
 
Example 7
Source File: Basic.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void to(long v, TimeUnit unit) {
    FileTime t = FileTime.from(v, unit);
    for (TimeUnit u: TimeUnit.values()) {
        long result = t.to(u);
        long expected = u.convert(v, unit);
        if (result != expected) {
            throw new RuntimeException("unexpected result");
        }
    }
}
 
Example 8
Source File: HarTiming.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * @return Time required to create TCP connection.
 * -1 if the timing does not apply to the current request.
 * @param timeUnit param
 */
public long getConnect(TimeUnit timeUnit) {
    if (connectNanos == -1) {
        return -1;
    } else {
        return timeUnit.convert(connectNanos, TimeUnit.NANOSECONDS);
    }
}
 
Example 9
Source File: RepositoryConfiguration.java    From nifi with Apache License 2.0 4 votes vote down vote up
public long getMaintenanceFrequency(final TimeUnit timeUnit) {
    return timeUnit.convert(maintenanceFrequencyMillis, TimeUnit.MILLISECONDS);
}
 
Example 10
Source File: Backoffs.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public long maxDelay(TimeUnit unit) {
   return unit.convert(max, TimeUnit.NANOSECONDS);
}
 
Example 11
Source File: TagBuildStrategyImpl.java    From basic-branch-build-strategies-plugin with MIT License 4 votes vote down vote up
@CheckForNull
public Long getAtLeast(@NonNull TimeUnit unit) {
    return atLeastMillis >= 0L ? unit.convert(atLeastMillis, TimeUnit.MILLISECONDS) : null;
}
 
Example 12
Source File: ActiveRecordWriters.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public long getDelay(TimeUnit unit) {
  return unit.convert(writer.getExpiresOn() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
 
Example 13
Source File: ThriftTable.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public long getRpcTimeout(TimeUnit unit) {
  return unit.convert(operationTimeout, TimeUnit.MILLISECONDS);
}
 
Example 14
Source File: RealAudioBuffer.java    From jipes with GNU Lesser General Public License v2.1 4 votes vote down vote up
public long getTimestamp(final TimeUnit timeUnit) {
    if (audioFormat == null) return -1;
    else return timeUnit.convert((long) (frameNumber * 1000l * 1000l * 1000l / (double)audioFormat.getSampleRate()), TimeUnit.NANOSECONDS);
}
 
Example 15
Source File: ContainerSimulator.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public long getDelay(TimeUnit unit) {
  return unit.convert(endTime - System.currentTimeMillis(),
        TimeUnit.MILLISECONDS);
}
 
Example 16
Source File: UGICache.java    From pxf with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public long getDelay(TimeUnit unit) {
    return unit.convert(getDelayMillis(), TimeUnit.MILLISECONDS);
}
 
Example 17
Source File: OffsetTimeSource.java    From triava with Apache License 2.0 4 votes vote down vote up
@Override
public long time(TimeUnit tu)
{
	return tu.convert(getMillisFromSource(), TimeUnit.MILLISECONDS);
}
 
Example 18
Source File: JobExecutionResult.java    From flink with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the net execution time of the job, i.e., the execution time in the parallel system,
 * without the pre-flight steps like the optimizer in a desired time unit.
 *
 * @param desiredUnit the unit of the <tt>NetRuntime</tt>
 * @return The net execution time in the desired unit.
 */
public long getNetRuntime(TimeUnit desiredUnit) {
	return desiredUnit.convert(getNetRuntime(), TimeUnit.MILLISECONDS);
}
 
Example 19
Source File: StopWatch.java    From embedded-rabbitmq with Apache License 2.0 2 votes vote down vote up
/**
 * <p>
 * Get the time on the stopwatch in the specified TimeUnit.
 * </p>
 *
 * <p>
 * This is either the time between the start and the moment this method is called, or the amount of time between
 * start and stop. The resulting time will be expressed in the desired TimeUnit with any remainder rounded down.
 * For example, if the specified unit is {@code TimeUnit.HOURS} and the stopwatch time is 59 minutes, then the
 * result returned will be {@code 0}.
 * </p>
 *
 * @param timeUnit the unit of time, not null
 * @return the time in the specified TimeUnit, rounded down
 * @since 3.5
 */
public long getTime(final TimeUnit timeUnit) {
  return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS);
}
 
Example 20
Source File: StopWatch.java    From nifi with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the amount of time that has elapsed since the timer was started.
 *
 * @param timeUnit the unit for which the elapsed time should be computed
 * @return the elapsed time in the specified unit
 */
public long getElapsed(final TimeUnit timeUnit) {
    return timeUnit.convert(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
}