activemq-cpp-3.4.0

src/main/decaf/util/concurrent/TimeUnit.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
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 
00221         void timedWait( Synchronizable* obj, long long timeout ) const;
00222 
00236         void timedJoin( decaf::lang::Thread* thread, long long timeout );
00237 
00245         void sleep( long long timeout ) const;
00246 
00251         virtual std::string toString() const;
00252 
00253     public:  // Static Methods
00254 
00267         static const TimeUnit& valueOf( const std::string& name );
00268 
00269     public:
00270 
00271         virtual int compareTo( const TimeUnit& value ) const;
00272 
00273         virtual bool equals( const TimeUnit& value ) const;
00274 
00275         virtual bool operator==( const TimeUnit& value ) const;
00276 
00277         virtual bool operator<( const TimeUnit& value ) const;
00278 
00279     private:
00280 
00281         /* Perform the actual conversion */
00282         long long doConvert( int srcIndex, int destIndex, long long duration ) const;
00283 
00284         /*
00285          * Utility method to compute the excess-nanosecond argument to
00286          * wait, sleep, join.
00287          */
00288         int excessNanos( long long time, long long ms ) const;
00289 
00296         static long long scale( long long duration, long long multiplier, long long overflow );
00297 
00298     };
00299 
00300 }}}
00301 
00302 #endif /*_DECAF_UTIL_CONCURRENT_TIMEUNIT_H_*/