JXTA

net.jxta.impl.util
Class TimeUtils

java.lang.Object
  extended by net.jxta.impl.util.TimeUtils

public final class TimeUtils
extends Object

Utilities for manipulating absolute and relative times and for accelerating and decelerating time for testing purposes.

The "time warp" functionality is useful for debugging and is scoped to the class loader in which the TimeUtils class is loaded.


Field Summary
static long ADAY
          The number of milliseconds in a day.
static long ADECEMBER
          The number of milliseconds in the month of December.
static long AFEBRUARY
          The number of milliseconds in the month of February in a non-leap year.
static long AFORTNIGHT
          The number of milliseconds in a fortnight (two weeks).
static long AHUNDREDTHOFASECOND
          The number of milliseconds in a hundredth of a second.
static long AJANUARY
          The number of milliseconds in the month of January.
static long AJULY
          The number of milliseconds in the month of July.
static long AJUNE
          The number of milliseconds in the month of June.
static long ALEAPFEBRUARY
          The number of milliseconds in the month of February in a leap year.
static long ALEAPYEAR
          The number of milliseconds in a leap year.
static long AMARCH
          The number of milliseconds in the month of March.
static long AMAY
          The number of milliseconds in the month of May.
static long AMILLISECOND
          The number of milliseconds in a millisecond.
static long AMINUTE
          The number of milliseconds in a minute.
static long ANAPRIL
          The number of milliseconds in the month of April.
static long ANAUGUST
          The number of milliseconds in the month of August.
static long ANHOUR
          The number of milliseconds in an hour.
static long ANOCTOBER
          The number of milliseconds in the month of October.
static long ANOVEMBER
          The number of milliseconds in the month of November.
static long ASECOND
          The number of milliseconds in a second.
static long ASEPTEMBER
          The number of milliseconds in the month of September.
static long ATENTHOFASECOND
          The number of milliseconds in a tenth of a second.
static long AWEEK
          The number of milliseconds in a week.
static long AYEAR
          The number of milliseconds in a non-leap year.
(package private) static long TIMEWARP
          This odd little guy is for use in testing. it is applied anywhere the current time is used and allows modules which use timeutils to be tested through long (simulated) periods of time passing.
(package private) static long WARPBEGAN
          Absolute time in millis at which we began timewarping.
(package private) static double WARPFACTOR
          The rate at which time is warped using the auto-warper.
static long ZEROMILLISECONDS
          Zero milliseconds (yes its redundant).
 
Method Summary
static void autoWarp(double warpfactor)
          A utility for automagically adjusting the time dialation of the time warp.
static long multiplyRelativeTimeMillis(long duration, long multiplier)
          Multiplies a duration in relative milliseconds by a multiplier while accounting for overflow and underflow of the magnitude value.
static long timeNow()
          Return the current time.
static void timeWarp(long advanceby)
          A utility for advancing the timewarp by the number of milliseconds specified.
static long toAbsoluteTimeMillis(long duration)
          Convert a duration into a duration expressed in milliseconds to absolute time realtive to the current real time.
static long toAbsoluteTimeMillis(long duration, long fromWhen)
          Convert a duration into a duration expressed in milliseconds to absolute time realtive to the provided absolute time.
static long toRelativeTimeMillis(long whence)
          Convert an absolute real time in milliseconds to a duration relative to the current real time.
static long toRelativeTimeMillis(long whence, long fromWhen)
          Convert an absolute real time in milliseconds to a duration relative to the specified absolute real time.
static long warpedRelativeTime(long initial)
          Return a relative time adjusted by the current warping factor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZEROMILLISECONDS

public static final long ZEROMILLISECONDS
Zero milliseconds (yes its redundant).

See Also:
Constant Field Values

AMILLISECOND

public static final long AMILLISECOND
The number of milliseconds in a millisecond. (yes its redundant).

See Also:
Constant Field Values

AHUNDREDTHOFASECOND

public static final long AHUNDREDTHOFASECOND
The number of milliseconds in a hundredth of a second.

See Also:
Constant Field Values

ATENTHOFASECOND

public static final long ATENTHOFASECOND
The number of milliseconds in a tenth of a second.

See Also:
Constant Field Values

ASECOND

public static final long ASECOND
The number of milliseconds in a second.

See Also:
Constant Field Values

AMINUTE

public static final long AMINUTE
The number of milliseconds in a minute.

See Also:
Constant Field Values

ANHOUR

public static final long ANHOUR
The number of milliseconds in an hour.

See Also:
Constant Field Values

ADAY

public static final long ADAY
The number of milliseconds in a day.

See Also:
Constant Field Values

AWEEK

public static final long AWEEK
The number of milliseconds in a week.

See Also:
Constant Field Values

AFORTNIGHT

public static final long AFORTNIGHT
The number of milliseconds in a fortnight (two weeks).

See Also:
Constant Field Values

AJANUARY

public static final long AJANUARY
The number of milliseconds in the month of January.

See Also:
Constant Field Values

AFEBRUARY

public static final long AFEBRUARY
The number of milliseconds in the month of February in a non-leap year.

See Also:
Constant Field Values

ALEAPFEBRUARY

public static final long ALEAPFEBRUARY
The number of milliseconds in the month of February in a leap year.

See Also:
Constant Field Values

AMARCH

public static final long AMARCH
The number of milliseconds in the month of March.

See Also:
Constant Field Values

ANAPRIL

public static final long ANAPRIL
The number of milliseconds in the month of April.

See Also:
Constant Field Values

AMAY

public static final long AMAY
The number of milliseconds in the month of May.

See Also:
Constant Field Values

AJUNE

public static final long AJUNE
The number of milliseconds in the month of June.

See Also:
Constant Field Values

AJULY

public static final long AJULY
The number of milliseconds in the month of July.

See Also:
Constant Field Values

ANAUGUST

public static final long ANAUGUST
The number of milliseconds in the month of August.

See Also:
Constant Field Values

ASEPTEMBER

public static final long ASEPTEMBER
The number of milliseconds in the month of September.

See Also:
Constant Field Values

ANOCTOBER

public static final long ANOCTOBER
The number of milliseconds in the month of October.

See Also:
Constant Field Values

ANOVEMBER

public static final long ANOVEMBER
The number of milliseconds in the month of November.

See Also:
Constant Field Values

ADECEMBER

public static final long ADECEMBER
The number of milliseconds in the month of December.

See Also:
Constant Field Values

AYEAR

public static final long AYEAR
The number of milliseconds in a non-leap year.

See Also:
Constant Field Values

ALEAPYEAR

public static final long ALEAPYEAR
The number of milliseconds in a leap year.

See Also:
Constant Field Values

TIMEWARP

static volatile long TIMEWARP
This odd little guy is for use in testing. it is applied anywhere the current time is used and allows modules which use timeutils to be tested through long (simulated) periods of time passing.


WARPBEGAN

static long WARPBEGAN
Absolute time in millis at which we began timewarping.


WARPFACTOR

static double WARPFACTOR
The rate at which time is warped using the auto-warper.

Method Detail

timeNow

public static long timeNow()
Return the current time. This value may differ from the value returned by System.currentTimeMillis() if the timeWarp(long) or autoWarp(double) features are being used. Using timeNow() allows test harnesses to simulate long periods of time passing.

Returns:
The current time which has been possibly adjusted by a "time warp" factor.

toAbsoluteTimeMillis

public static long toAbsoluteTimeMillis(long duration)
Convert a duration into a duration expressed in milliseconds to absolute time realtive to the current real time. Special handling for the maximum and minimum durations converts them to the maximum and minimum absolute times.

Parameters:
duration - a time duration expressed in milliseconds.
Returns:
an absolute time in milliseconds based on the duration's relation to the current real time.

toAbsoluteTimeMillis

public static long toAbsoluteTimeMillis(long duration,
                                        long fromWhen)
Convert a duration into a duration expressed in milliseconds to absolute time realtive to the provided absolute time. Special handling for the maximum and minimum durations converts them to the maximum and minimum absolute times.

Parameters:
duration - a time duration expressed in milliseconds.
fromWhen - an absolute time expressed in milliseconds.
Returns:
an absolute time in milliseconds based on the duration's relation to the provided absolute time.

toRelativeTimeMillis

public static long toRelativeTimeMillis(long whence)
Convert an absolute real time in milliseconds to a duration relative to the current real time. Special handling for the maximum and minimum absolute times converts them to the maximum and minimum durations.

Parameters:
whence - an absolute real time expressed in milliseconds.
Returns:
a duration expressed in milliseconds relative to the current real time.

toRelativeTimeMillis

public static long toRelativeTimeMillis(long whence,
                                        long fromWhen)
Convert an absolute real time in milliseconds to a duration relative to the specified absolute real time. Special handling for the maximum and minimum absolute times converts them to the maximum and minimum durations.

Parameters:
whence - An absolute real time expressed in milliseconds.
fromWhen - The base time in absolute real time expressed in milliseconds from which the relative time will be calculated.
Returns:
a duration expressed in milliseconds relative to the provided absolute time.

multiplyRelativeTimeMillis

public static long multiplyRelativeTimeMillis(long duration,
                                              long multiplier)
Multiplies a duration in relative milliseconds by a multiplier while accounting for overflow and underflow of the magnitude value.

Parameters:
duration - a time duration expressed in milliseconds.
multiplier - a non-negative value which will be multiplied against the duration.
Returns:
a time duration expressed in milliseconds.

timeWarp

public static void timeWarp(long advanceby)
A utility for advancing the timewarp by the number of milliseconds specified. May not be used if you are also using autoWarp(double).

Parameters:
advanceby - Advance the timewarp by the number of milliseconds specified.

autoWarp

public static void autoWarp(double warpfactor)
A utility for automagically adjusting the time dialation of the time warp.

Parameters:
warpfactor - a decimal ratio at which artifical time will pass.

  • To have time pass at the rate of an hour every minute, initialize with: (double)(TimeUtils.ANHOUR / TimeUtils.AMINUTE).
  • To have time pass at five times normal rate, initialize with: 5.0.
  • etc.

warpedRelativeTime

public static long warpedRelativeTime(long initial)
Return a relative time adjusted by the current warping factor. Needed for external methods which do not use timeNow() such as Object.wait(long) and Socket.setSoTimeout(int).

Parameters:
initial - The initial time value to "warp".
Returns:
The provided initial time adjusted by the current warping factor.

JXSE