Author: tabish
Date: Mon Oct 6 14:12:39 2008
New Revision: 702265
URL: http://svn.apache.org/viewvc?rev=702265&view=rev
Log:
Adding Start of the TimeUnit class.
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.cpp
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.h
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.cpp
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.h
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.cpp?rev=702265&r1=702264&r2=702265&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.cpp
(original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.cpp Mon
Oct 6 14:12:39 2008
@@ -46,21 +46,6 @@
};
////////////////////////////////////////////////////////////////////////////////
-// Lookup table to check saturation. Note that because we are
-// dividing these down, we don't have to deal with asymmetry of
-// MIN/MAX values.
-//
-const long long TimeUnit::overflows[] = {
- 0, // unused
- Long::MAX_VALUE / 1000LL,
- Long::MAX_VALUE / ( 1000LL * 1000LL ),
- Long::MAX_VALUE / ( 1000LL * 1000LL * 1000LL ),
- Long::MAX_VALUE / ( 1000LL * 1000LL * 1000LL * 60LL ),
- Long::MAX_VALUE / ( 1000LL * 1000LL * 1000LL * 60LL * 60LL ),
- Long::MAX_VALUE / ( 1000LL * 1000LL * 1000LL * 60LL * 60LL * 24LL )
-};
-
-////////////////////////////////////////////////////////////////////////////////
TimeUnit::TimeUnit( int index, const std::string& name ) {
this->index = index;
this->name = name;
@@ -68,23 +53,104 @@
////////////////////////////////////////////////////////////////////////////////
long long TimeUnit::convert( long long sourceDuration, const TimeUnit&
sourceUnit ) const {
- return this->doConvert( sourceUnit.index - this->index, sourceDuration );
+ return this->doConvert( sourceUnit.index, this->index, sourceDuration );
}
////////////////////////////////////////////////////////////////////////////////
-long long TimeUnit::doConvert( long long delta, long long duration ) const {
+long long TimeUnit::doConvert( int srcIndex, int destIndex, long long duration
) const {
- if( delta == 0 ) {
+ if( duration == 0 ) {
return duration;
- } else if( delta < 0 ) {
- return duration / multipliers[-delta];
- } else if( duration > overflows[delta] ) {
- return Long::MAX_VALUE;
- } else if( duration < -overflows[delta] ) {
- return Long::MIN_VALUE;
+ } else if( srcIndex > destIndex ) {
+ return scale( duration,
+ multipliers[srcIndex] / multipliers[destIndex],
+ Long::MAX_VALUE / ( multipliers[srcIndex] /
multipliers[destIndex] ) );
+ } else if( srcIndex < destIndex ) {
+ return duration / ( multipliers[destIndex] / multipliers[srcIndex] );
}
- return duration * multipliers[delta];
+ // Same unit, no conversion.
+ return duration;
+
+ /*
+ NANOSECONDS {
+ public long toNanos(long d) { return d; }
+ public long toMicros(long d) { return d/(C1/C0); }
+ public long toMillis(long d) { return d/(C2/C0); }
+ public long toSeconds(long d) { return d/(C3/C0); }
+ public long toMinutes(long d) { return d/(C4/C0); }
+ public long toHours(long d) { return d/(C5/C0); }
+ public long toDays(long d) { return d/(C6/C0); }
+ public long convert(long d, TimeUnit u) { return u.toNanos(d); }
+ int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
+ },
+ MICROSECONDS {
+ public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); }
+ public long toMicros(long d) { return d; }
+ public long toMillis(long d) { return d/(C2/C1); }
+ public long toSeconds(long d) { return d/(C3/C1); }
+ public long toMinutes(long d) { return d/(C4/C1); }
+ public long toHours(long d) { return d/(C5/C1); }
+ public long toDays(long d) { return d/(C6/C1); }
+ public long convert(long d, TimeUnit u) { return u.toMicros(d); }
+ int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
+ },
+ MILLISECONDS {
+ public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); }
+ public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); }
+ public long toMillis(long d) { return d; }
+ public long toSeconds(long d) { return d/(C3/C2); }
+ public long toMinutes(long d) { return d/(C4/C2); }
+ public long toHours(long d) { return d/(C5/C2); }
+ public long toDays(long d) { return d/(C6/C2); }
+ public long convert(long d, TimeUnit u) { return u.toMillis(d); }
+ int excessNanos(long d, long m) { return 0; }
+ },
+ SECONDS {
+ public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); }
+ public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); }
+ public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); }
+ public long toSeconds(long d) { return d; }
+ public long toMinutes(long d) { return d/(C4/C3); }
+ public long toHours(long d) { return d/(C5/C3); }
+ public long toDays(long d) { return d/(C6/C3); }
+ public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
+ int excessNanos(long d, long m) { return 0; }
+ },
+ MINUTES {
+ public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); }
+ public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); }
+ public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); }
+ public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
+ public long toMinutes(long d) { return d; }
+ public long toHours(long d) { return d/(C5/C4); }
+ public long toDays(long d) { return d/(C6/C4); }
+ public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
+ int excessNanos(long d, long m) { return 0; }
+ },
+ HOURS {
+ public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); }
+ public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); }
+ public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); }
+ public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
+ public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
+ public long toHours(long d) { return d; }
+ public long toDays(long d) { return d/(C6/C5); }
+ public long convert(long d, TimeUnit u) { return u.toHours(d); }
+ int excessNanos(long d, long m) { return 0; }
+ },
+ DAYS {
+ public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); }
+ public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); }
+ public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); }
+ public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
+ public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
+ public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); }
+ public long toDays(long d) { return d; }
+ public long convert(long d, TimeUnit u) { return u.toDays(d); }
+ int excessNanos(long d, long m) { return 0; }
+ };
+ */
}
////////////////////////////////////////////////////////////////////////////////
@@ -145,3 +211,14 @@
bool TimeUnit::operator<( const TimeUnit& value ) const {
return this->compareTo( value ) == -1;
}
+
+////////////////////////////////////////////////////////////////////////////////
+long long TimeUnit::scale( long long duration, long long multiplier, long long
overflow ) {
+ if( duration > overflow ) {
+ return Long::MAX_VALUE;
+ } else if( duration < -overflow ) {
+ return Long::MIN_VALUE;
+ }
+
+ return duration * multiplier;
+}
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.h?rev=702265&r1=702264&r2=702265&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.h
(original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/concurrent/TimeUnit.h Mon
Oct 6 14:12:39 2008
@@ -61,23 +61,12 @@
/** Array of Time Unit multipliers */
static const long long multipliers[];
- /**
- * Lookup table to check saturation. Note that because we are
- * dividing these down, we don't have to deal with asymmetry of
- * MIN/MAX values.
- */
- static const long long overflows[];
-
- /**
- * Name of the Unit being represented.
- */
+ /** Name of the Unit being represented. */
std::string name;
public:
- /**
- * The Actual TimeUnit enumerations
- */
+ /** The Actual TimeUnit enumerations */
static const TimeUnit NANOSECONDS;
static const TimeUnit MICROSECONDS;
static const TimeUnit MILLISECONDS;
@@ -125,7 +114,7 @@
* @see #convert
*/
long long toNanos( long long duration ) const {
- return doConvert( index, duration );
+ return doConvert( this->index, NANOSECONDS.index, duration );
}
/**
@@ -137,7 +126,7 @@
* @see #convert
*/
long long toMicros( long long duration ) const {
- return doConvert( index - MICROSECONDS.index, duration );
+ return doConvert( this->index, MICROSECONDS.index, duration );
}
/**
@@ -149,7 +138,7 @@
* @see #convert
*/
long long toMillis( long long duration ) const {
- return doConvert( index - MILLISECONDS.index, duration );
+ return doConvert( this->index, MILLISECONDS.index, duration );
}
/**
@@ -159,7 +148,37 @@
* @see #convert
*/
long long toSeconds( long long duration ) const {
- return doConvert( index - SECONDS.index, duration );
+ return doConvert( this->index, SECONDS.index, duration );
+ }
+
+ /**
+ * Equivalent to <tt>MINUTES.convert(duration, this)</tt>.
+ * @param duration the duration
+ * @return the converted duration.
+ * @see #convert
+ */
+ long long toMinutes( long long duration ) const {
+ return doConvert( this->index, MINUTES.index, duration );
+ }
+
+ /**
+ * Equivalent to <tt>HOURS.convert(duration, this)</tt>.
+ * @param duration the duration
+ * @return the converted duration.
+ * @see #convert
+ */
+ long long toHours( long long duration ) const {
+ return doConvert( this->index, HOURS.index, duration );
+ }
+
+ /**
+ * Equivalent to <tt>DAYS.convert(duration, this)</tt>.
+ * @param duration the duration
+ * @return the converted duration.
+ * @see #convert
+ */
+ long long toDays( long long duration ) const {
+ return doConvert( this->index, DAYS.index, duration );
}
/**
@@ -274,7 +293,7 @@
private:
/* Perform the actual conversion */
- long long doConvert( long long delta, long long duration ) const;
+ long long doConvert( int srcIndex, int destIndex, long long duration )
const;
/*
* Utility method to compute the excess-nanosecond argument to
@@ -282,6 +301,14 @@
*/
int excessNanos( long long time, long long ms ) const;
+ /**
+ * Scale d by m, checking for overflow.
+ * @param duration - The amount of time to scale by the multiplier.
+ * @param multiplier - The scaling factor.
+ * @param overflow - The value at which d * m would cause an overflow.
+ */
+ static long long scale( long long duration, long long multiplier, long
long overflow );
+
};
}}}
Modified:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.cpp?rev=702265&r1=702264&r2=702265&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.cpp
(original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.cpp
Mon Oct 6 14:12:39 2008
@@ -29,10 +29,9 @@
using namespace decaf::util::concurrent;
////////////////////////////////////////////////////////////////////////////////
-void TimeUnitTest::testConvert() {
+void TimeUnitTest::testConvert1() {
for( long long t = 0; t < 10; ++t ) {
-
CPPUNIT_ASSERT( t == TimeUnit::SECONDS.convert( t, TimeUnit::SECONDS )
);
CPPUNIT_ASSERT( t == TimeUnit::SECONDS.convert( 1000 * t,
TimeUnit::MILLISECONDS ) );
CPPUNIT_ASSERT( t == TimeUnit::SECONDS.convert( 1000000 * t,
TimeUnit::MICROSECONDS ) );
@@ -53,10 +52,23 @@
}
////////////////////////////////////////////////////////////////////////////////
+void TimeUnitTest::testConvert2() {
+
+ for( long long t = 0; t < 10; ++t ) {
+ CPPUNIT_ASSERT( t == TimeUnit::DAYS.convert( t * 24, TimeUnit::HOURS )
);
+ CPPUNIT_ASSERT( t == TimeUnit::HOURS.convert( t * 60,
TimeUnit::MINUTES ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MINUTES.convert( t * 60,
TimeUnit::SECONDS ) );
+ CPPUNIT_ASSERT( t == TimeUnit::SECONDS.convert( t * 1000,
TimeUnit::MILLISECONDS ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MILLISECONDS.convert( t * 1000,
TimeUnit::MICROSECONDS ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MICROSECONDS.convert( t * 1000,
TimeUnit::NANOSECONDS ) );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
void TimeUnitTest::testToNanos() {
+
for( long long t = 0; t < 10; ++t ) {
CPPUNIT_ASSERT( 1000000000 * t == TimeUnit::SECONDS.toNanos( t ) );
-
CPPUNIT_ASSERT( 1000000 * t == TimeUnit::MILLISECONDS.toNanos( t ) );
CPPUNIT_ASSERT( 1000 * t == TimeUnit::MICROSECONDS.toNanos( t ) );
CPPUNIT_ASSERT( t == TimeUnit::NANOSECONDS.toNanos( t ) );
@@ -76,8 +88,8 @@
////////////////////////////////////////////////////////////////////////////////
void TimeUnitTest::testToMillis() {
- for( long long t = 0; t < 10; ++t ) {
+ for( long long t = 0; t < 10; ++t ) {
CPPUNIT_ASSERT( 1000 * t == TimeUnit::SECONDS.toMillis( t ) );
CPPUNIT_ASSERT( t == TimeUnit::MILLISECONDS.toMillis( t ) );
CPPUNIT_ASSERT( t == TimeUnit::MICROSECONDS.toMillis( t * 1000 ) );
@@ -87,8 +99,8 @@
////////////////////////////////////////////////////////////////////////////////
void TimeUnitTest::testToSeconds() {
- for( long long t = 0; t < 10; ++t ) {
+ for( long long t = 0; t < 10; ++t ) {
CPPUNIT_ASSERT( t == TimeUnit::SECONDS.toSeconds( t ) );
CPPUNIT_ASSERT( t == TimeUnit::MILLISECONDS.toSeconds( t * 1000 ) );
CPPUNIT_ASSERT( t == TimeUnit::MICROSECONDS.toSeconds( t * 1000000 ) );
@@ -97,6 +109,45 @@
}
////////////////////////////////////////////////////////////////////////////////
+void TimeUnitTest::testToMinutes() {
+
+ for( long long t = 0; t < 10; ++t ) {
+ CPPUNIT_ASSERT( t == TimeUnit::MINUTES.toMinutes( t ) );
+ CPPUNIT_ASSERT( t == TimeUnit::SECONDS.toMinutes( t * 60 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MILLISECONDS.toMinutes( t * 1000LL * 60
) );
+ CPPUNIT_ASSERT( t == TimeUnit::MICROSECONDS.toMinutes( t * 1000000LL *
60 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::NANOSECONDS.toMinutes( t * 1000000000LL
* 60 ) );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TimeUnitTest::testToHours() {
+
+ for( long long t = 0; t < 10; ++t ) {
+ CPPUNIT_ASSERT( t == TimeUnit::HOURS.toHours( t ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MINUTES.toHours( t * 60 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::SECONDS.toHours( t * 60 * 60 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MILLISECONDS.toHours( t * 1000LL * 60 *
60 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MICROSECONDS.toHours( t * 1000000LL *
60 * 60 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::NANOSECONDS.toHours( t * 1000000000LL *
60 * 60 ) );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void TimeUnitTest::testToDays() {
+
+ for( long long t = 0; t < 10; ++t ) {
+ CPPUNIT_ASSERT( t == TimeUnit::DAYS.toDays( t ) );
+ CPPUNIT_ASSERT( t == TimeUnit::HOURS.toDays( t * 24 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MINUTES.toDays( t * 60 * 24 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::SECONDS.toDays( t * 60 * 60 * 24 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MILLISECONDS.toDays( t * 1000LL * 60 *
60 * 24 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::MICROSECONDS.toDays( t * 1000000LL * 60
* 60 * 24 ) );
+ CPPUNIT_ASSERT( t == TimeUnit::NANOSECONDS.toDays( t * 1000000000LL *
60 * 60 * 24 ) );
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
void TimeUnitTest::testConvertSaturate() {
CPPUNIT_ASSERT( Long::MAX_VALUE ==
TimeUnit::NANOSECONDS.convert( Long::MAX_VALUE / 2, TimeUnit::SECONDS
) );
@@ -115,7 +166,7 @@
////////////////////////////////////////////////////////////////////////////////
void TimeUnitTest::testToString() {
std::string s = TimeUnit::SECONDS.toString();
- CPPUNIT_ASSERT( s.find_first_of( "ECOND" ) >= (std::size_t)0 );
+ CPPUNIT_ASSERT( s.find_first_of( "ECOND" ) != (std::size_t)0 );
}
//////////////////////////////////////////////////////////////////////////////////
Modified:
activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.h?rev=702265&r1=702264&r2=702265&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.h
(original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.h
Mon Oct 6 14:12:39 2008
@@ -28,7 +28,8 @@
class TimeUnitTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( TimeUnitTest );
- CPPUNIT_TEST( testConvert );
+ CPPUNIT_TEST( testConvert1 );
+ CPPUNIT_TEST( testConvert2 );
CPPUNIT_TEST( testToNanos );
CPPUNIT_TEST( testToMicros );
CPPUNIT_TEST( testToMillis );
@@ -38,6 +39,9 @@
CPPUNIT_TEST( testToString );
CPPUNIT_TEST( testTimedWait );
CPPUNIT_TEST( testSleep );
+ CPPUNIT_TEST( testToMinutes );
+ CPPUNIT_TEST( testToHours );
+ CPPUNIT_TEST( testToDays );
CPPUNIT_TEST_SUITE_END();
public:
@@ -45,11 +49,15 @@
TimeUnitTest() {}
virtual ~TimeUnitTest() {}
- void testConvert();
+ void testConvert1();
+ void testConvert2();
void testToNanos();
void testToMicros();
void testToMillis();
void testToSeconds();
+ void testToMinutes();
+ void testToHours();
+ void testToDays();
void testConvertSaturate();
void testToNanosSaturate();
void testToString();