activemq-cpp-3.6.0
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:

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
- Public Member Functions inherited from decaf::lang::Comparable< TimeUnit >
virtual ~Comparable ()
virtual int compareTo (const TimeUnit &value) const =0
 Compares this object with the specified object for order.
virtual bool equals (const TimeUnit &value) const =0
virtual bool operator== (const TimeUnit &value) const =0
 Compares equality between this object and the one passed.
virtual bool operator< (const TimeUnit &value) const =0
 Compares this object to another and returns true if this object is considered to be less than the one passed.

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 ( )
inlinevirtual

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
timeoutthe 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
threadthe thread to wait for
timeoutthe maximum time to wait
Exceptions
InterruptedExceptionif interrupted while waiting.
NullPointerExceptionif 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
objthe object to wait on
timeoutthe maximum time to wait.
Exceptions
InterruptedExceptionif interrupted while waiting.
NullPointerExceptionif 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
durationthe 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
durationthe 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
durationthe 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
durationthe 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
durationthe 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
durationthe 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
durationthe 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
nameThe Name of the TimeUnit constant to be returned.
Returns
A constant reference to the TimeUnit Constant with the given name.
Exceptions
IllegalArgumentExceptionif this enum type has no constant with the specified name

Field Documentation

const TimeUnit decaf::util::concurrent::TimeUnit::DAYS
static
const TimeUnit decaf::util::concurrent::TimeUnit::HOURS
static
const TimeUnit decaf::util::concurrent::TimeUnit::MICROSECONDS
static
const TimeUnit decaf::util::concurrent::TimeUnit::MILLISECONDS
static
const TimeUnit decaf::util::concurrent::TimeUnit::MINUTES
static
const TimeUnit decaf::util::concurrent::TimeUnit::NANOSECONDS
static

The Actual TimeUnit enumerations.

const TimeUnit decaf::util::concurrent::TimeUnit::SECONDS
static
const TimeUnit* const decaf::util::concurrent::TimeUnit::values[]
static

The An Array of TimeUnit Instances.


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