Author: tabish
Date: Sat Feb 14 17:11:02 2009
New Revision: 744531
URL: http://svn.apache.org/viewvc?rev=744531&view=rev
Log:
Further implementation of the Decaf Collections API
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractQueue.h
activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractSet.h
activemq/activemq-cpp/trunk/src/main/decaf/util/Queue.h
activemq/activemq-cpp/trunk/src/main/decaf/util/StlSet.h
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractQueue.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractQueue.h?rev=744531&r1=744530&r2=744531&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractQueue.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractQueue.h Sat Feb 14
17:11:02 2009
@@ -22,6 +22,7 @@
#include <decaf/lang/exceptions/UnsupportedOperationException.h>
#include <decaf/lang/exceptions/NullPointerException.h>
#include <decaf/lang/exceptions/IllegalArgumentException.h>
+#include <decaf/lang/exceptions/IllegalStateException.h>
#include <decaf/lang/Iterable.h>
#include <decaf/util/Iterator.h>
#include <decaf/util/Queue.h>
@@ -45,10 +46,125 @@
*/
template< typename E >
class DECAF_API AbstractQueue : public decaf::util::Queue<E> {
+ private:
+
+ E emptyMarker;
+
public:
+ AbstractQueue() : emptyMarker() {}
+
virtual ~AbstractQueue() {}
+ /**
+ * Inserts the specified element into this queue if it is possible to
do so
+ * immediately without violating capacity restrictions, returning true
upon
+ * success and throwing an IllegalStateException if no space is
currently available.
+ *
+ * This implementation returns true if offer succeeds, else throws an
+ * IllegalStateException.
+ *
+ * @param value - the element to offer to the Queue.
+ *
+ * @return true if the add succeeds.
+ *
+ * @throw IllegalArgumentException if the element cannot be added.
+ */
+ virtual bool add( const E& value )
+ throw ( decaf::lang::exceptions::UnsupportedOperationException,
+ decaf::lang::exceptions::IllegalArgumentException ) {
+
+ if( offer( value ) ) {
+ return true;
+ }
+
+ throw decaf::lang::exceptions::IllegalArgumentException(
+ __FILE__, __LINE__, "Unable to add specified element to the
Queue." );
+ }
+
+ /**
+ * Adds all the elements of a collection to the queue. If the
collection is
+ * the queue itself, then an IllegalArgumentException will be thrown
out. If
+ * during the process, some runtime exception is thrown out, then part
of
+ * the elements in the collection that have successfully added will
remain
+ * in the queue.
+ *
+ * The result of the method is undefined if the collection is modified
+ * during the process of the method.
+ *
+ * @param collection - the collection to be added to the queue.
+ * @return true if the operation succeeds.
+ *
+ * @throws IllegalArgumentException If the collection to be added to
the
+ * queue is the queue itself.
+ */
+ virtual bool addAll( const Collection<E>& collection )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) {
+
+ if( this == &collection ) {
+ throw decaf::lang::exceptions::IllegalArgumentException(
+ __FILE__, __LINE__, "A Queue cannot be added to itself." );
+ }
+
+ return Queue<E>::addAll( collection );
+ }
+
+ /**
+ * Retrieves and removes the head of this queue. This method differs
from poll
+ * only in that it throws an exception if this queue is empty.
+ *
+ * This implementation returns the result of poll unless the queue is
empty.
+ *
+ * @return a copy of the element in the head of the queue.
+ *
+ * @throws NoSuchElementException if the queue is empty.
+ */
+ virtual E remove() throw (
decaf::lang::exceptions::NoSuchElementException ) {
+
+ E result = this->poll();
+ if( result == this->getEmptyMarker() ) {
+ throw decaf::lang::exceptions::NoSuchElementException(
+ __FILE__, __LINE__, "Queue is empty." );
+ }
+
+ return result;
+ }
+
+ /**
+ * Retrieves, but does not remove, the head of this queue. This method
differs
+ * from peek only in that it throws an exception if this queue is
empty.
+ *
+ * This implementation returns the result of peek unless the queue is
empty.
+ *
+ * @return the element in the head of the queue.
+ * @throws NoSuchElementException if the queue is empty.
+ */
+ virtual const E& element() const
+ throw( decaf::lang::exceptions::NoSuchElementException ) {
+
+ const E& result = this->peek();
+ if( result == this->getEmptyMarker() ) {
+ throw decaf::lang::exceptions::NoSuchElementException(
+ __FILE__, __LINE__, "Queue is empty." );
+ }
+
+ return result;
+ }
+
+ /**
+ * Removes all elements of the queue.
+ *
+ * This implementation repeatedly invokes poll until it returns the
empty marker.
+ */
+ virtual void clear() throw (
lang::exceptions::UnsupportedOperationException ) {
+
+ E result;
+ do {
+ result = this->poll();
+ } while( result != this->getEmptyMarker() );
+ }
+
};
}}
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractSet.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractSet.h?rev=744531&r1=744530&r2=744531&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractSet.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractSet.h Sat Feb 14
17:11:02 2009
@@ -40,8 +40,7 @@
* constraints imposed by the Set interface (for instance, the add method
must not
* permit addition of multiple instances of an object to a set).
*
- * Note that this class does not override any of the implementations from
the
- * AbstractCollection class. It merely adds implementations for equals and
removeAll.
+ * @since 1.0
*/
template<typename E >
class DECAF_API AbstractSet : public decaf::util::Set<E> {
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/Queue.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/Queue.h?rev=744531&r1=744530&r2=744531&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/Queue.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/Queue.h Sat Feb 14 17:11:02
2009
@@ -53,6 +53,11 @@
/**
* Returns a reference to the Marker value that is returned from
methods that
* do not throw an exception when there is no element in the Queue to
return.
+ * The empty marker is usually an instance of the contained element
initialized
+ * using the default constructor (if its a Class) or the default value
for the
+ * primitive type.
+ *
+ * @return a value that indicates that the Queue is empty.
*/
virtual const E& getEmptyMarker() const = 0;
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/StlSet.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/StlSet.h?rev=744531&r1=744530&r2=744531&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/StlSet.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/StlSet.h Sat Feb 14
17:11:02 2009
@@ -25,7 +25,7 @@
#include <decaf/util/concurrent/Synchronizable.h>
#include <decaf/util/concurrent/Mutex.h>
#include <decaf/util/Iterator.h>
-#include <decaf/util/Set.h>
+#include <decaf/util/AbstractSet.h>
namespace decaf{
namespace util{
@@ -36,7 +36,7 @@
* functions that do not exist in std::set.
*/
template <typename E>
- class StlSet : public decaf::util::Set<E> {
+ class StlSet : public decaf::util::AbstractSet<E> {
private:
std::set<E> values;