Here you can find the source of durationToMillis(final long val, final TimeUnit unit)
Parameter | Description |
---|---|
IllegalArgumentException | if the duration argument is illegal.Thrown via API setter methods such as Transaction.setLockTimeout. |
public static int durationToMillis(final long val, final TimeUnit unit)
//package com.java2s; /*-//from w ww .j ava 2 s .co m * See the file LICENSE for redistribution information. * * Copyright (c) 2002-2010 Oracle. All rights reserved. * */ import java.util.concurrent.TimeUnit; public class Main { /** * Converts the given duration (interval value plus unit) to milliseconds, * ensuring that any given value greater than zero converts to at least one * millisecond to avoid a zero millisecond result, since Object.wait(0) * waits forever. * * @throws IllegalArgumentException if the duration argument is illegal. * Thrown via API setter methods such as Transaction.setLockTimeout. */ public static int durationToMillis(final long val, final TimeUnit unit) { if (val == 0) { /* Allow zero duration with null unit. */ return 0; } if (unit == null) { throw new IllegalArgumentException( "Duration TimeUnit argument may not be null if interval " + "is non-zero"); } if (val < 0) { throw new IllegalArgumentException("Duration argument may not be negative: " + val); } final long newVal = unit.toMillis(val); if (newVal == 0) { /* Input val is positive, so return at least one. */ return 1; } if (newVal > Integer.MAX_VALUE) { throw new IllegalArgumentException( "Duration argument may not be greater than " + "Integer.MAX_VALUE milliseconds: " + newVal); } return (int) newVal; } }