activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r555082 - in /activemq/activemq-cpp/trunk/src: main/activemq/util/Iterator.h main/activemq/util/Set.h test/activemq/util/SetTest.cpp test/activemq/util/SetTest.h
Date Tue, 10 Jul 2007 21:21:06 GMT
Author: tabish
Date: Tue Jul 10 14:21:05 2007
New Revision: 555082

URL: http://svn.apache.org/viewvc?view=rev&rev=555082
Log:
http://issues.apache.org/activemq/browse/AMQCPP-128

Adding Iterator to the Collections classes to avoid having to call toArray as much.

Added:
    activemq/activemq-cpp/trunk/src/main/activemq/util/Iterator.h
    activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.h
Modified:
    activemq/activemq-cpp/trunk/src/main/activemq/util/Set.h

Added: activemq/activemq-cpp/trunk/src/main/activemq/util/Iterator.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/Iterator.h?view=auto&rev=555082
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/Iterator.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/Iterator.h Tue Jul 10 14:21:05 2007
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACTIVEMQ_UTIL_ITERATOR_H_
+#define _ACTIVEMQ_UTIL_ITERATOR_H_
+
+#include <activemq/exceptions/NoSuchElementException.h>
+#include <activemq/exceptions/IllegalStateException.h>
+#include <activemq/exceptions/UnsupportedOperationException.h>
+
+namespace activemq{
+namespace util{
+
+    /**
+     * Defines an object that can be used to iterate over the elements of a
+     * collection.  The iterator provides a way to access and remove elements
+     * with well defined semantics.
+     */
+    template< typename T>
+    class Iterator {
+    public:
+
+        virtual ~Iterator() {}
+
+        /**
+         * Returns the next element in the iteration. Calling this method
+         * repeatedly until the hasNext() method returns false will return
+         * each element in the underlying collection exactly once.
+         * @returns next element in the iteration of elements
+         * @throws NoSuchElementException - iteration has no more elements.
+         */
+        virtual T next() throw( exceptions::NoSuchElementException ) = 0;
+
+        /**
+         * Returns true if the iteration has more elements.  Returns false if
+         * the next call to next would result in an NoSuchElementException to
+         * be thrown.
+         */
+        virtual bool hasNext() const = 0;
+
+        /**
+         * Removes from the underlying collection the last element returned
+         * by the iterator (optional operation). This method can be called
+         * only once per call to next. The behavior of an iterator is
+         * unspecified if the underlying collection is modified while the
+         * iteration is in progress in any way other than by calling this
+         * method.
+         * @throws UnsupportedOperationException - if the remove  operation
+         *         is not supported by this Iterator.
+         * @throws IllegalStateException - if the next method has not yet been
+         *         called, or the remove method has already been called after
+         *         the last call to the next  method.
+         */
+        virtual void remove() throw ( exceptions::IllegalStateException,
+                                      exceptions::UnsupportedOperationException ) = 0;
+
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_UTIL_ITERATOR_H_*/

Modified: activemq/activemq-cpp/trunk/src/main/activemq/util/Set.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/Set.h?view=diff&rev=555082&r1=555081&r2=555082
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/Set.h (original)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/Set.h Tue Jul 10 14:21:05 2007
@@ -23,6 +23,7 @@
 #include <activemq/exceptions/NoSuchElementException.h>
 #include <activemq/concurrent/Synchronizable.h>
 #include <activemq/concurrent/Mutex.h>
+#include <activemq/util/Iterator.h>
 
 namespace activemq{
 namespace util{
@@ -39,12 +40,58 @@
         std::set<E> values;
         concurrent::Mutex mutex;
 
+    private:
+
+        class SetIterator : public Iterator<E> {
+        private:
+
+            typename std::set<E>::iterator current;
+            typename std::set<E>::iterator previous;
+            typename std::set<E>* set;
+
+        public:
+
+            SetIterator( typename std::set<E>* set ) {
+                this->current = set->begin();
+                this->previous = set->end();
+                this->set = set;
+            }
+            virtual ~SetIterator() {}
+
+            virtual E next() throw( exceptions::NoSuchElementException ){
+                if( this->current == set->end() ) {
+                    throw exceptions::NoSuchElementException(
+                        __FILE__, __LINE__,
+                        "Set::Iterator::next - No more elements to return" );
+                }
+
+                this->previous = this->current;
+                return *( this->current++ );
+            }
+
+            virtual bool hasNext() const {
+                return ( this->current != set->end() );
+            }
+
+            virtual void remove() throw ( exceptions::IllegalStateException,
+                                          exceptions::UnsupportedOperationException ){
+                if( this->previous == set->end() ) {
+                    throw exceptions::IllegalStateException(
+                        __FILE__, __LINE__,
+                        "Set::Iterator::remove - Invalid State to call remove" );
+                }
+
+                this->set->erase( this->previous );
+                this->previous = this->set->end();
+            }
+        };
+
     public:
 
         /**
          * Default constructor - does nothing.
          */
-        Set(){};
+        Set(){}
 
         /**
          * Copy constructor - copies the content of the given set into this
@@ -55,7 +102,17 @@
             copy( source );
         }
 
-        virtual ~Set(){};
+        virtual ~Set(){}
+
+        /**
+         * Returns an iterator for this collection.  The order of Iteration
+         * is in no particular order other than the natural ording of the
+         * elements in the Set class.
+         * @returns Iterator<E> for this collection
+         */
+        Iterator<E>* iterator() {
+            return new SetIterator( &values );
+        }
 
         /**
          * Copies the content of the source set into this set.  Erases

Added: activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.cpp?view=auto&rev=555082
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.cpp Tue Jul 10 14:21:05 2007
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "SetTest.h"
+
+CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::SetTest );
+
+using namespace std;
+using namespace activemq;
+using namespace activemq::util;
+
+////////////////////////////////////////////////////////////////////////////////
+SetTest::SetTest(){
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testContains(){
+
+    Set<string> set;
+    CPPUNIT_ASSERT( set.contains( "bob" ) == false);
+
+    set.add( "bob" );
+
+    CPPUNIT_ASSERT(set.contains( "bob" ) == true );
+    CPPUNIT_ASSERT(set.contains( "fred" ) == false );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testClear(){
+
+    Set<string> set;
+    set.add( "bob" );
+    set.add( "fred" );
+
+    CPPUNIT_ASSERT( set.size() == 2 );
+    set.clear();
+    CPPUNIT_ASSERT( set.size() == 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testIsEmpty(){
+
+    Set<string> set;
+    set.add( "bob" );
+    set.add( "fred" );
+
+    CPPUNIT_ASSERT(set.isEmpty() == false );
+    set.clear();
+    CPPUNIT_ASSERT(set.isEmpty() == true );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testSize(){
+
+    Set<string> set;
+
+    CPPUNIT_ASSERT( set.size() == 0 );
+    set.add( "bob" );
+    CPPUNIT_ASSERT( set.size() == 1 );
+    set.add( "fred" );
+    CPPUNIT_ASSERT( set.size() == 2 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testAdd(){
+    Set<string> set;
+
+    set.add( "fred" );
+    set.add( "fred" );
+    set.add( "fred" );
+    CPPUNIT_ASSERT( set.contains("fred") == true );
+    CPPUNIT_ASSERT( set.size() == 1 );
+    set.remove( "fred" );
+    CPPUNIT_ASSERT( set.contains("fred") == false );
+    CPPUNIT_ASSERT( set.isEmpty() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testRemove(){
+    Set<string> set;
+
+    set.add( "fred" );
+    CPPUNIT_ASSERT( set.contains( "fred" ) == true );
+    set.remove( "fred" );
+    CPPUNIT_ASSERT( set.contains( "fred" ) == false );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testToArray(){
+
+    Set<string> set;
+
+    set.add( "fred1" );
+    set.add( "fred2" );
+    set.add( "fred3" );
+    CPPUNIT_ASSERT( set.size() == 3 );
+
+    std::vector<std::string> array = set.toArray();
+
+    CPPUNIT_ASSERT( array.size() == 3 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SetTest::testIterator(){
+
+    Set<string> set;
+
+    set.add( "fred1" );
+    set.add( "fred2" );
+    set.add( "fred3" );
+
+    Iterator<string>* iterator = set.iterator();
+    CPPUNIT_ASSERT( iterator != NULL );
+    CPPUNIT_ASSERT( iterator->hasNext() == true );
+
+    size_t count = 0;
+    while( iterator->hasNext() ) {
+        iterator->next();
+        ++count;
+    }
+
+    CPPUNIT_ASSERT( count == set.size() );
+
+    delete iterator;
+}

Added: activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.h?view=auto&rev=555082
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/util/SetTest.h Tue Jul 10 14:21:05 2007
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACTIVEMQ_UTIL_SETTEST_H_
+#define _ACTIVEMQ_UTIL_SETTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <activemq/util/Set.h>
+#include <activemq/util/Iterator.h>
+
+namespace activemq{
+namespace util{
+
+    class SetTest : public CppUnit::TestFixture
+    {
+        CPPUNIT_TEST_SUITE( SetTest );
+        CPPUNIT_TEST( testContains );
+        CPPUNIT_TEST( testClear );
+        CPPUNIT_TEST( testSize );
+        CPPUNIT_TEST( testAdd );
+        CPPUNIT_TEST( testRemove );
+        CPPUNIT_TEST( testIsEmpty );
+        CPPUNIT_TEST( testToArray );
+        CPPUNIT_TEST( testIterator );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        SetTest();
+        virtual ~SetTest() {}
+
+        void testContains();
+        void testClear();
+        void testSize();
+        void testAdd();
+        void testRemove();
+        void testIsEmpty();
+        void testToArray();
+        void testIterator();
+
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_UTIL_SETTEST_H_*/



Mime
View raw message