00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_CONCURRENT_TIMEUNIT_H_
00019 #define _DECAF_UTIL_CONCURRENT_TIMEUNIT_H_
00020
00021 #include <string>
00022 #include <decaf/util/Config.h>
00023 #include <decaf/lang/Comparable.h>
00024 #include <decaf/util/concurrent/Synchronizable.h>
00025 #include <decaf/lang/exceptions/IllegalArgumentException.h>
00026 #include <decaf/lang/exceptions/InterruptedException.h>
00027 #include <decaf/lang/exceptions/NullPointerException.h>
00028
00029 namespace decaf {
00030 namespace lang {
00031 class Thread;
00032 }
00033 namespace util {
00034 namespace concurrent {
00035
00062 class DECAF_API TimeUnit : public decaf::lang::Comparable<TimeUnit> {
00063 private:
00064
00066 int index;
00067
00069 std::string name;
00070
00072 static const long long multipliers[];
00073
00074 public:
00075
00077 static const TimeUnit NANOSECONDS;
00078 static const TimeUnit MICROSECONDS;
00079 static const TimeUnit MILLISECONDS;
00080 static const TimeUnit SECONDS;
00081 static const TimeUnit MINUTES;
00082 static const TimeUnit HOURS;
00083 static const TimeUnit DAYS;
00084
00086 static const TimeUnit* const values[];
00087
00088 protected:
00089
00095 TimeUnit( int index, const std::string& name );
00096
00097 public:
00098
00099 virtual ~TimeUnit() {}
00100
00116 long long convert( long long sourceDuration, const TimeUnit& sourceUnit ) const;
00117
00126 long long toNanos( long long duration ) const {
00127 return doConvert( this->index, NANOSECONDS.index, duration );
00128 }
00129
00138 long long toMicros( long long duration ) const {
00139 return doConvert( this->index, MICROSECONDS.index, duration );
00140 }
00141
00150 long long toMillis( long long duration ) const {
00151 return doConvert( this->index, MILLISECONDS.index, duration );
00152 }
00153
00160 long long toSeconds( long long duration ) const {
00161 return doConvert( this->index, SECONDS.index, duration );
00162 }
00163
00170 long long toMinutes( long long duration ) const {
00171 return doConvert( this->index, MINUTES.index, duration );
00172 }
00173
00180 long long toHours( long long duration ) const {
00181 return doConvert( this->index, HOURS.index, duration );
00182 }
00183
00190 long long toDays( long long duration ) const {
00191 return doConvert( this->index, DAYS.index, duration );
00192 }
00193
00220 void timedWait( Synchronizable* obj, long long timeout ) const;
00221
00235 void timedJoin( decaf::lang::Thread* thread, long long timeout );
00236
00244 void sleep( long long timeout ) const;
00245
00250 virtual std::string toString() const;
00251
00252 public:
00253
00266 static const TimeUnit& valueOf( const std::string& name );
00267
00268 public:
00269
00270 virtual int compareTo( const TimeUnit& value ) const;
00271
00272 virtual bool equals( const TimeUnit& value ) const;
00273
00274 virtual bool operator==( const TimeUnit& value ) const;
00275
00276 virtual bool operator<( const TimeUnit& value ) const;
00277
00278 private:
00279
00280
00281 long long doConvert( int srcIndex, int destIndex, long long duration ) const;
00282
00283
00284
00285
00286
00287 int excessNanos( long long time, long long ms ) const;
00288
00295 static long long scale( long long duration, long long multiplier, long long overflow );
00296
00297 };
00298
00299 }}}
00300
00301 #endif