You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2008/10/07 01:53:36 UTC

svn commit: r702307 - in /activemq/activemq-cpp/trunk/src: main/ main/decaf/lang/ main/decaf/util/concurrent/ test/ test/decaf/lang/ test/decaf/util/concurrent/

Author: tabish
Date: Mon Oct  6 16:53:35 2008
New Revision: 702307

URL: http://svn.apache.org/viewvc?rev=702307&view=rev
Log:
Added implementation and tests for TimeUnit.
Added nanoTime to System

Modified:
    activemq/activemq-cpp/trunk/src/main/Makefile.am
    activemq/activemq-cpp/trunk/src/main/decaf/lang/System.cpp
    activemq/activemq-cpp/trunk/src/main/decaf/lang/System.h
    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/Makefile.am
    activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.cpp
    activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.h
    activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.cpp
    activemq/activemq-cpp/trunk/src/test/decaf/util/concurrent/TimeUnitTest.h
    activemq/activemq-cpp/trunk/src/test/testRegistry.cpp

Modified: activemq/activemq-cpp/trunk/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/Makefile.am?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/main/Makefile.am Mon Oct  6 16:53:35 2008
@@ -129,6 +129,7 @@
     decaf/util/concurrent/Mutex.cpp \
     decaf/util/concurrent/CountDownLatch.cpp \
     decaf/util/concurrent/PooledThread.cpp \
+    decaf/util/concurrent/TimeUnit.cpp \
     decaf/util/concurrent/ThreadPool.cpp \
     decaf/util/concurrent/atomic/AtomicBoolean.cpp \
     decaf/util/concurrent/atomic/AtomicInteger.cpp \
@@ -388,6 +389,7 @@
     decaf/util/concurrent/PooledThread.h \
     decaf/util/concurrent/PooledThreadListener.h \
     decaf/util/concurrent/TaskListener.h \
+    decaf/util/concurrent/TimeUnit.h \
     decaf/util/concurrent/ThreadPool.h \
     decaf/util/concurrent/atomic/AtomicBoolean.h \
     decaf/util/concurrent/atomic/AtomicInteger.h \

Modified: activemq/activemq-cpp/trunk/src/main/decaf/lang/System.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/lang/System.cpp?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/lang/System.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/lang/System.cpp Mon Oct  6 16:53:35 2008
@@ -126,6 +126,11 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+long long System::nanoTime() {
+    return apr_time_now() * 1000;
+}
+
+////////////////////////////////////////////////////////////////////////////////
 Map<string, string> System::getenv() throw ( Exception ) {
 
     Map<string, string> values;

Modified: activemq/activemq-cpp/trunk/src/main/decaf/lang/System.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/lang/System.h?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/lang/System.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/lang/System.h Mon Oct  6 16:53:35 2008
@@ -76,6 +76,31 @@
          */
         static long long currentTimeMillis();
 
+        /**
+         * Returns the current value of the most precise available system timer, in
+         * nanoseconds.
+         * <p>
+         * This method can only be used to measure elapsed time and is not related to
+         * any other notion of system or wall-clock time. The value returned represents
+         * nanoseconds since some fixed but arbitrary time (perhaps in the future, so
+         * values may be negative). This method provides nanosecond precision, but not
+         * necessarily nanosecond accuracy. No guarantees are made about how frequently
+         * values change. Differences in successive calls that span greater than
+         * approximately 292 years (263 nanoseconds) will not accurately compute elapsed
+         * time due to numerical overflow.
+         * <p>
+         * For example, to measure how long some code takes to execute:
+         * <p>
+         *   long long startTime = System::nanoTime();
+         *   // ... the code being measured ...
+         *   long long estimatedTime = System::nanoTime() - startTime;
+         * <p>
+         *
+         * @returns
+         *     The current value of the system timer, in nanoseconds.
+         */
+        static long long nanoTime();
+
     private:
 
         /**

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=702307&r1=702306&r2=702307&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 16:53:35 2008
@@ -22,19 +22,31 @@
 
 using namespace decaf;
 using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
 using namespace decaf::util;
 using namespace decaf::util::concurrent;
 
 ////////////////////////////////////////////////////////////////////////////////
 const TimeUnit TimeUnit::NANOSECONDS( 0, "NANOSECONDS" );
 const TimeUnit TimeUnit::MICROSECONDS( 1, "MICROSECONDS" );
-const TimeUnit TimeUnit::MILLISECONDS( 2, "MILLISEONDS" );
+const TimeUnit TimeUnit::MILLISECONDS( 2, "MILLISECONDS" );
 const TimeUnit TimeUnit::SECONDS( 3, "SECONDS" );
 const TimeUnit TimeUnit::MINUTES( 4, "MINUTES" );
 const TimeUnit TimeUnit::HOURS( 5, "HOURS" );
 const TimeUnit TimeUnit::DAYS( 6, "DAYS" );
 
 ////////////////////////////////////////////////////////////////////////////////
+const TimeUnit* const TimeUnit::values[] = {
+    &NANOSECONDS,
+    &MICROSECONDS,
+    &MILLISECONDS,
+    &SECONDS,
+    &MINUTES,
+    &HOURS,
+    &DAYS
+};
+
+////////////////////////////////////////////////////////////////////////////////
 const long long TimeUnit::multipliers[] = {
     1,
     1000LL,
@@ -71,86 +83,6 @@
 
     // 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; }
-    };
-    */
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -222,3 +154,19 @@
 
     return duration * multiplier;
 }
+
+////////////////////////////////////////////////////////////////////////////////
+const TimeUnit& TimeUnit::valueOf( const std::string& name )
+    throw ( decaf::lang::exceptions::IllegalArgumentException ) {
+
+    for( int i = 0; i < 7; ++i ) {
+        if( values[i]->name == name ) {
+            return *values[i];
+        }
+    }
+
+    throw IllegalArgumentException(
+        __FILE__, __LINE__,
+        "Passed TimeUnit name; %s, Does not match any instances of TimeUnit",
+        name.c_str() );
+}

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=702307&r1=702306&r2=702307&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 16:53:35 2008
@@ -21,6 +21,7 @@
 #include <string>
 #include <decaf/lang/Comparable.h>
 #include <decaf/util/concurrent/Synchronizable.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
 
 namespace decaf {
 namespace util {
@@ -75,6 +76,9 @@
         static const TimeUnit HOURS;
         static const TimeUnit DAYS;
 
+        /** The An Array of TimeUnit Instances */
+        static const TimeUnit* const values[];
+
     protected:
 
         /**
@@ -238,6 +242,23 @@
          */
         virtual std::string toString() const;
 
+    public:  // Static Methods
+
+        /**
+         * 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.)
+         *
+         * @param name
+         *          The Name of the TimeUnit constant to be returned.
+         * @returns
+         *          A constant reference to the TimeUnit Constant with the given name.
+         * @throws IllegalArgumentException
+         *          if this enum type has no constant with the specified name
+         */
+        static const TimeUnit& valueOf( const std::string& name )
+            throw ( decaf::lang::exceptions::IllegalArgumentException );
+
     public:
 
         /**

Modified: activemq/activemq-cpp/trunk/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/Makefile.am?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test/Makefile.am Mon Oct  6 16:53:35 2008
@@ -125,6 +125,7 @@
   decaf/util/concurrent/CountDownLatchTest.cpp \
   decaf/util/concurrent/MutexTest.cpp \
   decaf/util/concurrent/ThreadPoolTest.cpp \
+  decaf/util/concurrent/TimeUnitTest.cpp \
   decaf/util/concurrent/atomic/AtomicBooleanTest.cpp \
   decaf/util/concurrent/atomic/AtomicIntegerTest.cpp \
   decaf/nio/BufferTest.cpp \
@@ -241,6 +242,7 @@
   decaf/util/concurrent/CountDownLatchTest.h \
   decaf/util/concurrent/MutexTest.h \
   decaf/util/concurrent/ThreadPoolTest.h \
+  decaf/util/concurrent/TimeUnitTest.h \
   decaf/util/concurrent/atomic/AtomicBooleanTest.h \
   decaf/util/concurrent/atomic/AtomicIntegerTest.h \
   decaf/nio/BufferTest.h  

Modified: activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.cpp?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.cpp Mon Oct  6 16:53:35 2008
@@ -79,3 +79,8 @@
 void SystemTest::test_currentTimeMillis() {
     CPPUNIT_ASSERT( System::currentTimeMillis() != 0 );
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void SystemTest::test_nanoTime() {
+    CPPUNIT_ASSERT( System::nanoTime() != 0 );
+}

Modified: activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.h?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.h (original)
+++ activemq/activemq-cpp/trunk/src/test/decaf/lang/SystemTest.h Mon Oct  6 16:53:35 2008
@@ -32,6 +32,7 @@
         CPPUNIT_TEST( test_setenv );
         CPPUNIT_TEST( test_unsetenv );
         CPPUNIT_TEST( test_currentTimeMillis );
+        CPPUNIT_TEST( test_nanoTime );
         CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -44,6 +45,7 @@
         void test_setenv();
         void test_unsetenv();
         void test_currentTimeMillis();
+        void test_nanoTime();
 
     };
 

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=702307&r1=702306&r2=702307&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 16:53:35 2008
@@ -61,6 +61,12 @@
         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 ) );
+        CPPUNIT_ASSERT( t * 24 == TimeUnit::HOURS.convert( t, TimeUnit::DAYS ) );
+        CPPUNIT_ASSERT( t * 60 == TimeUnit::MINUTES.convert( t, TimeUnit::HOURS ) );
+        CPPUNIT_ASSERT( t * 60 == TimeUnit::SECONDS.convert( t, TimeUnit::MINUTES ) );
+        CPPUNIT_ASSERT( t * 1000 == TimeUnit::MILLISECONDS.convert( t, TimeUnit::SECONDS ) );
+        CPPUNIT_ASSERT( t * 1000 == TimeUnit::MICROSECONDS.convert( t, TimeUnit::MILLISECONDS ) );
+        CPPUNIT_ASSERT( t * 1000 == TimeUnit::NANOSECONDS.convert( t, TimeUnit::MICROSECONDS ) );
     }
 }
 
@@ -191,3 +197,20 @@
     long long later = System::currentTimeMillis();
     CPPUNIT_ASSERT( later - now + 10 >= TimeUnit::SECONDS.toMillis( 1 ) );
 }
+
+//////////////////////////////////////////////////////////////////////////////////
+void TimeUnitTest::testValueOf() {
+
+    CPPUNIT_ASSERT( TimeUnit::NANOSECONDS == TimeUnit::valueOf( "NANOSECONDS" ) );
+    CPPUNIT_ASSERT( TimeUnit::MICROSECONDS == TimeUnit::valueOf( "MICROSECONDS" ) );
+    CPPUNIT_ASSERT( TimeUnit::MILLISECONDS == TimeUnit::valueOf( "MILLISECONDS" ) );
+    CPPUNIT_ASSERT( TimeUnit::SECONDS == TimeUnit::valueOf( "SECONDS" ) );
+    CPPUNIT_ASSERT( TimeUnit::MINUTES == TimeUnit::valueOf( "MINUTES" ) );
+    CPPUNIT_ASSERT( TimeUnit::DAYS == TimeUnit::valueOf( "DAYS" ) );
+    CPPUNIT_ASSERT( TimeUnit::HOURS == TimeUnit::valueOf( "HOURS" ) );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should Throw an IllegalArgumentException",
+        TimeUnit::valueOf( "FOO" ),
+        decaf::lang::exceptions::IllegalArgumentException );
+}

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=702307&r1=702306&r2=702307&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 16:53:35 2008
@@ -42,6 +42,7 @@
         CPPUNIT_TEST( testToMinutes );
         CPPUNIT_TEST( testToHours );
         CPPUNIT_TEST( testToDays );
+        CPPUNIT_TEST( testValueOf );
         CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -63,6 +64,7 @@
         void testToString();
         void testTimedWait();
         void testSleep();
+        void testValueOf();
 
     };
 

Modified: activemq/activemq-cpp/trunk/src/test/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/testRegistry.cpp?rev=702307&r1=702306&r2=702307&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/testRegistry.cpp Mon Oct  6 16:53:35 2008
@@ -240,6 +240,8 @@
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::concurrent::MutexTest );
 #include <decaf/util/concurrent/ThreadPoolTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::concurrent::ThreadPoolTest );
+#include <decaf/util/concurrent/TimeUnitTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::concurrent::TimeUnitTest );
 
 #include <decaf/util/concurrent/atomic/AtomicBooleanTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::concurrent::atomic::AtomicBooleanTest );