decaf::util::concurrent::TimeUnit Class Reference

A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units. More...

#include <src/main/decaf/util/concurrent/TimeUnit.h>

Inheritance diagram for decaf::util::concurrent::TimeUnit:
Inheritance graph
[legend]

Public Member Functions

virtual ~TimeUnit ()
long long convert (long long sourceDuration, const TimeUnit &sourceUnit) const
 Convert the given time duration in the given unit to this unit.
long long toNanos (long long duration) const
 Equivalent to NANOSECONDS.convert(duration, this).
long long toMicros (long long duration) const
 Equivalent to MICROSECONDS.convert(duration, this).
long long toMillis (long long duration) const
 Equivalent to MILLISECONDS.convert(duration, this).
long long toSeconds (long long duration) const
 Equivalent to SECONDS.convert(duration, this).
long long toMinutes (long long duration) const
 Equivalent to MINUTES.convert(duration, this).
long long toHours (long long duration) const
 Equivalent to HOURS.convert(duration, this).
long long toDays (long long duration) const
 Equivalent to DAYS.convert(duration, this).
void timedWait (Synchronizable *obj, long long timeout) const
 Perform a timed Object.wait using this time unit.
void timedJoin (decaf::lang::Thread *thread, long long timeout)
 Perform a timed Thread.join using this time unit.
void sleep (long long timeout) const
 Perform a Thread.sleep using this unit.
virtual std::string toString () const
 Converts the TimeUnit type to the Name of the TimeUnit.
virtual int compareTo (const TimeUnit &value) const
virtual bool equals (const TimeUnit &value) const
virtual bool operator== (const TimeUnit &value) const
virtual bool operator< (const TimeUnit &value) const

Static Public Member Functions

static const TimeUnitvalueOf (const std::string &name)
 Returns the TimeUnit constant of this type with the specified name.

Static Public Attributes

static const TimeUnit NANOSECONDS
 The Actual TimeUnit enumerations.
static const TimeUnit MICROSECONDS
static const TimeUnit MILLISECONDS
static const TimeUnit SECONDS
static const TimeUnit MINUTES
static const TimeUnit HOURS
static const TimeUnit DAYS
static const TimeUnit *const values []
 The An Array of TimeUnit Instances.

Protected Member Functions

 TimeUnit (int index, const std::string &name)
 Hidden Constructor, this class can not be instantiated directly.

Detailed Description

A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units.

A TimeUnit does not maintain time information, but only helps organize and use time representations that may be maintained separately across various contexts. A nanosecond is defined as one thousandth of a microsecond, a microsecond as one thousandth of a millisecond, a millisecond as one thousandth of a second, a minute as sixty seconds, an hour as sixty minutes, and a day as twenty four hours.

A TimeUnit is mainly used to inform time-based methods how a given timing parameter should be interpreted. For example, the following code will timeout in 50 milliseconds if the lock is not available:

Lock lock = ...; if ( lock.tryLock( 50, TimeUnit.MILLISECONDS ) ) ...

while this code will timeout in 50 seconds:

Lock lock = ...; if ( lock.tryLock( 50, TimeUnit.SECONDS ) ) ...

Note however, that there is no guarantee that a particular timeout implementation will be able to notice the passage of time at the same granularity as the given TimeUnit.


Constructor & Destructor Documentation

decaf::util::concurrent::TimeUnit::TimeUnit ( int  index,
const std::string &  name 
) [protected]

Hidden Constructor, this class can not be instantiated directly.

Parameters:
index - Index into the Time Unit set.
name - Name of the unit type being represented.
virtual decaf::util::concurrent::TimeUnit::~TimeUnit (  )  [inline, virtual]

Member Function Documentation

virtual int decaf::util::concurrent::TimeUnit::compareTo ( const TimeUnit value  )  const [virtual]
long long decaf::util::concurrent::TimeUnit::convert ( long long  sourceDuration,
const TimeUnit sourceUnit 
) const

Convert the given time duration in the given unit to this unit.

Conversions from finer to coarser granularities truncate, so lose precision. For example converting 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to Long.MIN_VALUE if negative or Long.MAX_VALUE if positive.

For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)

Parameters:
sourceDuration - Duration value to convert.
sourceUnit - Unit type of the source duration.
Returns:
the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
virtual bool decaf::util::concurrent::TimeUnit::equals ( const TimeUnit value  )  const [virtual]
virtual bool decaf::util::concurrent::TimeUnit::operator< ( const TimeUnit value  )  const [virtual]
virtual bool decaf::util::concurrent::TimeUnit::operator== ( const TimeUnit value  )  const [virtual]
void decaf::util::concurrent::TimeUnit::sleep ( long long  timeout  )  const

Perform a Thread.sleep using this unit.

This is a convenience method that converts time arguments into the form required by the Thread.sleep method.

Parameters:
timeout the minimum time to sleep
See also:
Thread::sleep
void decaf::util::concurrent::TimeUnit::timedJoin ( decaf::lang::Thread thread,
long long  timeout 
)

Perform a timed Thread.join using this time unit.

This is a convenience method that converts time arguments into the form required by the Thread.join method.

Parameters:
thread the thread to wait for
timeout the maximum time to wait
Exceptions:
InterruptedException if interrupted while waiting.
NullPointerException if the thread object is null.
See also:
Thread::join( long long, long long )
void decaf::util::concurrent::TimeUnit::timedWait ( Synchronizable obj,
long long  timeout 
) const

Perform a timed Object.wait using this time unit.

This is a convenience method that converts timeout arguments into the form required by the Object.wait method.

For example, you could implement a blocking poll method (see BlockingQueue.poll) using:

    Object poll( long long timeout, const TimeUnit& unit )
      while( empty ) {
        unit.timedWait(this, timeout);
        ...
      }
  }
Parameters:
obj the object to wait on
timeout the maximum time to wait.
Exceptions:
InterruptedException if interrupted while waiting.
NullPointerException if the Synchronizable object is null.
See also:
Synchronizable::wait( long long, long long )
long long decaf::util::concurrent::TimeUnit::toDays ( long long  duration  )  const [inline]

Equivalent to DAYS.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration.
See also:
convert
long long decaf::util::concurrent::TimeUnit::toHours ( long long  duration  )  const [inline]

Equivalent to HOURS.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration.
See also:
convert
long long decaf::util::concurrent::TimeUnit::toMicros ( long long  duration  )  const [inline]

Equivalent to MICROSECONDS.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also:
convert
long long decaf::util::concurrent::TimeUnit::toMillis ( long long  duration  )  const [inline]

Equivalent to MILLISECONDS.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also:
convert
long long decaf::util::concurrent::TimeUnit::toMinutes ( long long  duration  )  const [inline]

Equivalent to MINUTES.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration.
See also:
convert
long long decaf::util::concurrent::TimeUnit::toNanos ( long long  duration  )  const [inline]

Equivalent to NANOSECONDS.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See also:
convert

Referenced by decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::offer(), decaf::util::concurrent::LinkedBlockingQueue< Pointer< Transport > >::poll(), and decaf::util::concurrent::CopyOnWriteArrayList< E >::wait().

long long decaf::util::concurrent::TimeUnit::toSeconds ( long long  duration  )  const [inline]

Equivalent to SECONDS.convert(duration, this).

Parameters:
duration the duration
Returns:
the converted duration.
See also:
convert
virtual std::string decaf::util::concurrent::TimeUnit::toString (  )  const [virtual]

Converts the TimeUnit type to the Name of the TimeUnit.

Returns:
String name of the TimeUnit
static const TimeUnit& decaf::util::concurrent::TimeUnit::valueOf ( const std::string &  name  )  [static]

Returns the TimeUnit constant of this type with the specified name.

The string must match exactly an identifier used to declare an TimeUnit constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:
name The Name of the TimeUnit constant to be returned.
Returns:
A constant reference to the TimeUnit Constant with the given name.
Exceptions:
IllegalArgumentException if this enum type has no constant with the specified name

Field Documentation

The Actual TimeUnit enumerations.

The An Array of TimeUnit Instances.


The documentation for this class was generated from the following file:

Generated on 1 Dec 2014 for activemq-cpp-3.8.2 by  doxygen 1.6.1