Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.gh.bmd.jrt.time;
 
 
 
Utility class for handling a time duration in different time units.

Created by davide on 9/9/14.

 
 public class TimeDuration extends Time {

    
Time duration instance representing the infinity.
 
     public static final TimeDuration INFINITY = seconds(.);

    
Time duration instance representing the zero.
 
     public static final TimeDuration ZERO = seconds(0);
 
     private static final long MILLI_DAYS_OVERFLOW = 106751991167L;
 
     private static final long NANO_DAYS_OVERFLOW = 106750L;
 
     private static final long ONE_MILLI_NANOS = ..toNanos(1);

    
Constructor.

Parameters:
duration the time value.
unit the time unit.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
java.lang.NullPointerException if the time unit is null.
 
     protected TimeDuration(final long duration, @Nonnull final TimeUnit unit) {
 
         super(durationunit);
 
         if (duration < 0) {
 
             throw new IllegalArgumentException("the time duration cannot be negative: " + duration);
         }
     }

    
Creates and returns an object representing the specified number of days.

Parameters:
days the number of days.
Returns:
the time instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
 
     @Nonnull
     public static TimeDuration days(final long days) {
 
         if ((days > ) || (days < -)) {
 
             throw new IllegalArgumentException("time value overflow: " + days + " days");
         }
 
         return new TimeDuration(days * .);
     }

    
Creates and returns an object representing the specified time value in the specified time unit.

Parameters:
time the time value.
unit the time unit.
Returns:
the time instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
java.lang.NullPointerException if the time unit is null.
 
     @Nonnull
     @SuppressWarnings("ConstantConditions")
     public static TimeDuration fromUnit(final long time, @Nonnull final TimeUnit unit) {
 
         if (unit == null) {
 
             throw new NullPointerException("the time unit cannot be null");
         }
 
        return new TimeDuration(timeunit);
    }

    
Creates and returns an object representing the specified number of hours.

Parameters:
hours the number of hours
Returns:
the time instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
    @Nonnull
    public static TimeDuration hours(final long hours) {
        if ((hours > ) || (hours < -)) {
            throw new IllegalArgumentException("time value overflow: " + hours + " hours");
        }
        return new TimeDuration(hours * .);
    }

    
Creates and returns an object representing the specified number of microseconds.

Parameters:
micros the number of microseconds.
Returns:
the time duration instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
    @Nonnull
    public static TimeDuration micros(final long micros) {
        return new TimeDuration(micros.);
    }

    
Creates and returns an object representing the specified number of milliseconds.

Parameters:
millis the number of milliseconds.
Returns:
the time duration instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
    @Nonnull
    public static TimeDuration millis(final long millis) {
        return new TimeDuration(millis.);
    }

    
Creates and returns an object representing the specified number of minutes.

Parameters:
minutes the number of minutes.
Returns:
the time duration instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
    @Nonnull
    public static TimeDuration minutes(final long minutes) {
        if ((minutes > ) || (minutes < -)) {
            throw new IllegalArgumentException("time value overflow: " + minutes + " minutes");
        }
        return new TimeDuration(minutes * .);
    }

    
Creates and returns an object representing the specified number of nanoseconds.

Parameters:
nanos the number of nanoseconds.
Returns:
the time duration instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
    @Nonnull
    public static TimeDuration nanos(final long nanos) {
        return new TimeDuration(nanos.);
    }

    
Creates and returns an object representing the specified number of seconds.

Parameters:
seconds the number of seconds.
Returns:
the time duration instance.
Throws:
java.lang.IllegalArgumentException if the specified duration is negative.
    @Nonnull
    public static TimeDuration seconds(final long seconds) {
        return new TimeDuration(seconds.);
    }

    
Converts this duration in days.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration daysTime() {
        return days(toDays());
    }

    
Converts this duration in hours.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration hoursTime() {
        return hours(toHours());
    }

    
Converts this duration in microseconds.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration microsTime() {
        return micros(toMicros());
    }

    
Converts this duration in milliseconds.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration millisTime() {
        return millis(toMillis());
    }

    
Converts this duration in minutes.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration minutesTime() {
        return minutes(toMinutes());
    }

    
Converts this duration in nanoseconds.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration nanosTime() {
        return nanos(toNanos());
    }

    
Converts this duration in seconds.

Returns:
the time duration instance.
    @Nonnull
    public TimeDuration secondsTime() {
        return seconds(toSeconds());
    }

    
Checks if this duration represents the infinity.

Returns:
whether this duration is infinite.
    public boolean isInfinite() {
        return equals();
    }

    
Performs a java.lang.Thread.join() using this duration as timeout.

Parameters:
target the target thread.
Throws:
java.lang.InterruptedException if the current thread is interrupted.
java.lang.NullPointerException if the time unit is null.
    public void join(@Nonnull final Thread targetthrows InterruptedException {
        .timedJoin(target);
    }

    
Performs a java.lang.Thread.sleep(long,int) using this duration as timeout.

Throws:
java.lang.InterruptedException if the current thread is interrupted.
    public void sleep() throws InterruptedException {
        .sleep();
    }

    
Performs a java.lang.Thread.sleep(long,int) using this duration as timeout, ensuring that the sleep time is respected even if spurious wake ups happen in the while.

Throws:
java.lang.InterruptedException if the current thread is interrupted.
    public void sleepAtLeast() throws InterruptedException {
        if (isZero()) {
            return;
        }
        if (((toNanos() % ) == 0) || (toDays() > )) {
            final long startMillis = System.currentTimeMillis();
            while (true) {
                if (!sleepSinceMillis(startMillis)) {
                    return;
                }
            }
        }
        final long startNanos = System.nanoTime();
        while (true) {
            if (!sleepSinceNanos(startNanos)) {
                return;
            }
        }
    }

    
Performs a java.lang.Thread.sleep(long,int) as if started from the specified system time in milliseconds, by using this duration as timeout.

Parameters:
milliTime the starting system time in milliseconds.
Returns:
whether the sleep happened at all.
Throws:
java.lang.IllegalStateException if this duration overflows the maximum sleep time.
java.lang.InterruptedException if the current thread is interrupted.
See also:
java.lang.System.currentTimeMillis()
    public boolean sleepSinceMillis(final long milliTimethrows InterruptedException {
        if (isZero()) {
            return false;
        }
        if (toDays() > ) {
            throw new IllegalStateException("the duration overflows the maximum sleep time: " +
                                                    toDays() + " days");
        }
        final long millisToSleep = milliTime - System.currentTimeMillis() + toMillis();
        if (millisToSleep <= 0) {
            return false;
        }
        ..sleep(millisToSleep);
        return true;
    }

    
Performs a java.lang.Thread.sleep(long,int) as if started from the specified high precision system time in nanoseconds, by using this duration as timeout.

Parameters:
nanoTime the starting system time in nanoseconds.
Returns:
whether the sleep happened at all.
Throws:
java.lang.IllegalStateException if this duration overflows the maximum sleep time.
java.lang.InterruptedException if the current thread is interrupted.
See also:
java.lang.System.nanoTime()
    public boolean sleepSinceNanos(final long nanoTimethrows InterruptedException {
        if (isZero()) {
            return false;
        }
        if (toDays() > ) {
            throw new IllegalStateException("the duration overflows the maximum sleep time: " +
                                                    toDays() + " days");
        }
        final long nanosToSleep = nanoTime - System.nanoTime() + toNanos();
        if (nanosToSleep <= 0) {
            return false;
        }
        ..sleep(nanosToSleep);
        return true;
    }

    
Performs an java.lang.Object.wait() using this duration as timeout.

Parameters:
target the target object.
Throws:
java.lang.InterruptedException if the current thread is interrupted.
java.lang.NullPointerException if the target object is null.
    @SuppressFBWarnings(value = "WA_NOT_IN_LOOP",
            justification = "this is just a wrapper on an Object.wait() call")
    public void wait(@Nonnull final Object targetthrows InterruptedException {
        if (isZero()) {
            return;
        }
        if (isInfinite()) {
            target.wait();
            return;
        }
        .timedWait(target);
    }

    
Performs an java.lang.Object.wait() as if started from the specified system time in milliseconds, by using this duration as timeout.

Parameters:
target the target object.
milliTime the starting system time in milliseconds.
Returns:
whether the wait happened at all.
Throws:
java.lang.InterruptedException if the current thread is interrupted.
java.lang.NullPointerException if the target object is null.
See also:
java.lang.System.currentTimeMillis()
    @SuppressFBWarnings(value = "WA_NOT_IN_LOOP",
            justification = "this is just a wrapper on an Object.wait() call")
    public boolean waitSinceMillis(@Nonnull final Object targetfinal long milliTimethrows
            InterruptedException {
        if (isZero()) {
            return false;
        }
        if (isInfinite() || (toDays() > )) {
            target.wait();
            return true;
        }
        final long millisToWait = milliTime - System.currentTimeMillis() + toMillis();
        if (millisToWait <= 0) {
            return false;
        }
        ..timedWait(targetmillisToWait);
        return true;
    }

    
Performs an java.lang.Object.wait() as if started from the specified high precision system time in nanoseconds, by using this duration as timeout.

Parameters:
target the target object.
nanoTime the starting system time in nanoseconds.
Returns:
whether the wait happened at all.
Throws:
java.lang.InterruptedException if the current thread is interrupted.
java.lang.NullPointerException if the target object is null.
See also:
java.lang.System.nanoTime()
    @SuppressFBWarnings(value = "WA_NOT_IN_LOOP",
            justification = "this is just a wrapper on an Object.wait() call")
    public boolean waitSinceNanos(@Nonnull final Object targetfinal long nanoTimethrows
            InterruptedException {
        if (isZero()) {
            return false;
        }
        if (isInfinite() || (toDays() > )) {
            target.wait();
            return true;
        }
        final long nanosToWait = nanoTime - System.nanoTime() + toNanos();
        if (nanosToWait <= 0) {
            return false;
        }
        ..timedWait(targetnanosToWait);
        return true;
    }

    
Waits for the specified check to be true by performing an java.lang.Object.wait() and using this duration as timeout.

Parameters:
target the target object.
check the check to verify.
Returns:
whether the check became true before the timeout elapsed.
Throws:
java.lang.InterruptedException if the current thread is interrupted.
java.lang.NullPointerException if the target object or the specified check are null.
    public boolean waitTrue(@Nonnull final Object target, @Nonnull final Check checkthrows
            InterruptedException {
        if (isZero()) {
            return check.isTrue();
        }
        if (isInfinite()) {
            while (!check.isTrue()) {
                target.wait();
            }
            return true;
        }
        if ((toNanos() % ) == 0) {
            final long startMillis = System.currentTimeMillis();
            while (!check.isTrue()) {
                if (!waitSinceMillis(targetstartMillis)) {
                    return false;
                }
            }
        } else {
            final long startNanos = System.nanoTime();
            while (!check.isTrue()) {
                if (!waitSinceNanos(targetstartNanos)) {
                    return false;
                }
            }
        }
        return true;
    }

    
Interface defining a check to be performed.
    public interface Check {

        
Checks if true.

Returns:
whether the check is verified.
        boolean isTrue();
    }
New to GrepCode? Check out our FAQ X