Author: tabish
Date: Fri Feb 13 22:47:59 2009
New Revision: 744273
URL: http://svn.apache.org/viewvc?rev=744273&view=rev
Log:
Refine the Collections classes some more.
Added:
activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractCollection.h
(with props)
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/util/Collection.h
activemq/activemq-cpp/trunk/src/main/decaf/util/List.h
activemq/activemq-cpp/trunk/src/main/decaf/util/StlList.h
Added: activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractCollection.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractCollection.h?rev=744273&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractCollection.h (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractCollection.h Fri
Feb 13 22:47:59 2009
@@ -0,0 +1,207 @@
+/*
+ * 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 _DECAF_UTIL_ABSTRACTCOLLECTION_H_
+#define _DECAF_UTIL_ABSTRACTCOLLECTION_H_
+
+#include <decaf/util/Config.h>
+#include <decaf/lang/exceptions/UnsupportedOperationException.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
+#include <decaf/lang/Iterable.h>
+#include <decaf/util/Iterator.h>
+#include <decaf/util/Collection.h>
+#include <decaf/util/concurrent/Synchronizable.h>
+#include <memory>
+
+namespace decaf {
+namespace util {
+
+ /**
+ * This class provides a skeletal implementation of the Collection
interface, to
+ * minimize the effort required to implement this interface.
+ *
+ * To implement an unmodifiable collection, the programmer needs only to
extend this
+ * class and provide implementations for the iterator and size methods.
(The iterator
+ * returned by the iterator method must implement hasNext and next.)
+ *
+ * To implement a modifiable collection, the programmer must additionally
override
+ * this class's add method (which otherwise throws an
UnsupportedOperationException),
+ * and the iterator returned by the iterator method must additionally
implement its
+ * remove method.
+ *
+ * The programmer should generally provide a void (no argument) and
Collection
+ * constructor, as per the recommendation in the Collection interface
specification.
+ *
+ * The documentation for each non-abstract method in this class describes
its
+ * implementation in detail. Each of these methods may be overridden if
the collection
+ * being implemented admits a more efficient implementation.
+ *
+ * @since 1.0
+ */
+ template< typename E >
+ class AbstractCollection : public decaf::util::Collection<E> {
+ public:
+
+ virtual ~AbstractCollection() {}
+
+ virtual bool add( const E& value DECAF_UNUSED )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) {
+
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "AbstractCollection add is not
implemented.");
+ }
+
+ virtual bool addAll( const Collection<E>& collection )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) {
+
+ bool result = false;
+ std::auto_ptr< Iterator<E> > iter( collection.iterator() );
+ while( iter->hasNext() ) {
+ if( this->add( iter->next() ) ) {
+ result = true;
+ }
+ }
+
+ return result;
+ }
+
+ virtual void clear()
+ throw ( lang::exceptions::UnsupportedOperationException ) {
+
+ std::auto_ptr< Iterator<E> > iter( this->iterator() );
+ while( iter->hasNext() ) {
+ iter->next();
+ iter->remove();
+ }
+ }
+
+ virtual void copy( const Collection<E>& collection ) {
+ this->clear();
+
+ std::auto_ptr< Iterator<E> > iter( collection.iterator() );
+ while( iter->hasNext() ) {
+ this->add( iter->next() );
+ }
+ }
+
+ virtual bool contains( const E& value ) const throw ( lang::Exception
) {
+
+ bool result = false;
+ std::auto_ptr< Iterator<E> > iter( this->iterator() );
+ while( iter->hasNext() ) {
+ if( iter->next() == value ) {
+ result = true;
+ }
+ }
+
+ return result;
+ }
+
+ virtual bool containsAll( const Collection<E>& collection ) const
+ throw ( lang::Exception ) {
+
+ bool result = false;
+ std::auto_ptr< Iterator<E> > iter( collection.iterator() );
+ while( iter->hasNext() ) {
+ if( this->contains( iter->next() ) ) {
+ result = true;
+ }
+ }
+
+ return result;
+ }
+
+ virtual bool equals( const Collection<E>& collection ) const {
+ if( this->size() != collection.size() ) {
+ return false;
+ }
+
+ std::auto_ptr< Iterator<E> > srcIter( collection.iterator() );
+ std::auto_ptr< Iterator<E> > thisIter( this->iterator() );
+
+ while( srcIter->hasNext() ) {
+ if( !( thisIter->next() == srcIter->next() ) ) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ virtual bool isEmpty() const {
+ return this->size() == 0;
+ }
+
+ virtual bool remove( const E& value DECAF_UNUSED )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) {
+
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "AbstractCollection add is not
implemented.");
+ }
+
+ virtual bool removeAll( const Collection<E>& collection )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) {
+
+ bool result = false;
+ std::auto_ptr< Iterator<E> > iter( collection.iterator() );
+ while( iter->hasNext() ) {
+ if( this->remove( iter->next() ) ) {
+ result = true;
+ }
+ }
+
+ return result;
+ }
+
+ virtual bool retainAll( const Collection<E>& collection )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) {
+
+ bool result = false;
+ std::auto_ptr< Iterator<E> > iter( this->iterator() );
+ while( iter->hasNext() ) {
+ if( !collection.contains( iter->next() ) ) {
+ iter->remove();
+ result = true;
+ }
+ }
+
+ return result;
+ }
+
+ virtual std::vector<E> toArray() const {
+ std::vector<E> valueArray;
+ valueArray.reserve( this->size() );
+
+ std::auto_ptr< Iterator<E> > iter( this->iterator() );
+ while( iter->hasNext() ) {
+ valueArray.push_back( iter->next() );
+ }
+
+ return valueArray;
+ }
+
+ };
+
+}}
+
+#endif /*_DECAF_UTIL_ABSTRACTCOLLECTION_H_*/
Propchange: activemq/activemq-cpp/trunk/src/main/decaf/util/AbstractCollection.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/Collection.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/Collection.h?rev=744273&r1=744272&r2=744273&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/Collection.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/Collection.h Fri Feb 13
22:47:59 2009
@@ -72,24 +72,61 @@
virtual ~Collection() {}
/**
- * Returns the number of elements in this collection. If this
collection
- * contains more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
- * @returns the number of elements in this collection
+ * Returns true if this collection changed as a result of the call.
+ * (Returns false if this collection does not permit duplicates and
+ * already contains the specified element.)
+ *
+ * Collections that support this operation may place limitations on
+ * what elements may be added to this collection. In particular,
+ * some collections will refuse to add null elements, and others
+ * will impose restrictions on the type of elements that may be
+ * added. Collection classes should clearly specify in their
+ * documentation any restrictions on what elements may be added.
+ *
+ * If a collection refuses to add a particular element for any
+ * reason other than that it already contains the element, it must
+ * throw an exception (rather than returning false). This preserves
+ * the invariant that a collection always contains the specified
+ * element after this call returns.
+ *
+ * For non-pointer values, i.e. class instances or string's the object
+ * will be copied into the collection, thus the object must support
+ * being copied, must not hide the copy constructor and assignment
+ * operator.
+ *
+ * @param value - reference to the element to add.
+ * @returns true if the element was added
+ * @throw UnsupportedOperationException
+ * @throw IllegalArgumentException
*/
- virtual std::size_t size() const = 0;
+ virtual bool add( const E& value )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) = 0;
/**
- * @returns true if this collection contains no elements.
+ * Adds all of the elements in the specified collection to this
+ * collection. The behavior of this operation is undefined if the
+ * specified collection is modified while the operation is in progress.
+ * (This implies that the behavior of this call is undefined if the
+ * specified collection is this collection, and this collection is
+ * nonempty.)
+ * @param source - Collection whose elements are added to this one.
+ * @return true if this collection changed as a result of the call
+ * @throw UnsupportedOperationException
+ * @throw IllegalArgumentException
*/
- virtual bool isEmpty() const = 0;
+ virtual bool addAll( const Collection<E>& source )
+ throw ( lang::exceptions::UnsupportedOperationException,
+ lang::exceptions::IllegalArgumentException ) = 0;
/**
- * Compares the passed collection to this one, if they contain the
- * same elements, i.e. all their elements are equivalent, then it
- * returns true.
- * @returns true if the Collections contain the same elements.
+ * Removes all of the elements from this collection (optional
operation).
+ * This collection will be empty after this method returns unless it
throws
+ * an exception.
+ * @throw UnsupportedOperationException
*/
- virtual bool equals( const Collection<E>& value ) const = 0;
+ virtual void clear()
+ throw ( lang::exceptions::UnsupportedOperationException ) = 0;
/**
* Returns true if this collection contains the specified element. More
@@ -111,47 +148,17 @@
throw ( lang::Exception ) = 0;
/**
- * Returns an array containing all of the elements in this collection.
If
- * the collection makes any guarantees as to what order its elements
are
- * returned by its iterator, this method must return the elements in
the
- * same order.
- *
- * This method acts as bridge between array-based and collection-based
APIs.
- * @returns an array of the elements in this collection.
+ * Compares the passed collection to this one, if they contain the
+ * same elements, i.e. all their elements are equivalent, then it
+ * returns true.
+ * @returns true if the Collections contain the same elements.
*/
- virtual std::vector<E> toArray() const = 0;
+ virtual bool equals( const Collection<E>& value ) const = 0;
/**
- * Returns true if this collection changed as a result of the call.
- * (Returns false if this collection does not permit duplicates and
- * already contains the specified element.)
- *
- * Collections that support this operation may place limitations on
- * what elements may be added to this collection. In particular,
- * some collections will refuse to add null elements, and others
- * will impose restrictions on the type of elements that may be
- * added. Collection classes should clearly specify in their
- * documentation any restrictions on what elements may be added.
- *
- * If a collection refuses to add a particular element for any
- * reason other than that it already contains the element, it must
- * throw an exception (rather than returning false). This preserves
- * the invariant that a collection always contains the specified
- * element after this call returns.
- *
- * For non-pointer values, i.e. class instances or string's the object
- * will be copied into the collection, thus the object must support
- * being copied, must not hide the copy constructor and assignment
- * operator.
- *
- * @param value - reference to the element to add.
- * @returns true if the element was added
- * @throw UnsupportedOperationException
- * @throw IllegalArgumentException
+ * @returns true if this collection contains no elements.
*/
- virtual bool add( const E& value )
- throw ( lang::exceptions::UnsupportedOperationException,
- lang::exceptions::IllegalArgumentException ) = 0;
+ virtual bool isEmpty() const = 0;
/**
* Removes a single instance of the specified element from the
@@ -170,22 +177,6 @@
lang::exceptions::IllegalArgumentException ) = 0;
/**
- * Adds all of the elements in the specified collection to this
- * collection. The behavior of this operation is undefined if the
- * specified collection is modified while the operation is in progress.
- * (This implies that the behavior of this call is undefined if the
- * specified collection is this collection, and this collection is
- * nonempty.)
- * @param source - Collection whose elements are added to this one.
- * @return true if this collection changed as a result of the call
- * @throw UnsupportedOperationException
- * @throw IllegalArgumentException
- */
- virtual bool addAll( const Collection<E>& source )
- throw ( lang::exceptions::UnsupportedOperationException,
- lang::exceptions::IllegalArgumentException ) = 0;
-
- /**
* Removes all this collection's elements that are also contained in
* the specified collection (optional operation). After this call
returns,
* this collection will contain no elements in common with the
specified
@@ -214,13 +205,22 @@
lang::exceptions::IllegalArgumentException ) = 0;
/**
- * Removes all of the elements from this collection (optional
operation).
- * This collection will be empty after this method returns unless it
throws
- * an exception.
- * @throw UnsupportedOperationException
+ * Returns the number of elements in this collection. If this
collection
+ * contains more than Integer.MAX_VALUE elements, returns
Integer.MAX_VALUE.
+ * @returns the number of elements in this collection
*/
- virtual void clear()
- throw ( lang::exceptions::UnsupportedOperationException ) = 0;
+ virtual std::size_t size() const = 0;
+
+ /**
+ * Returns an array containing all of the elements in this collection.
If
+ * the collection makes any guarantees as to what order its elements
are
+ * returned by its iterator, this method must return the elements in
the
+ * same order.
+ *
+ * This method acts as bridge between array-based and collection-based
APIs.
+ * @returns an array of the elements in this collection.
+ */
+ virtual std::vector<E> toArray() const = 0;
};
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/List.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/List.h?rev=744273&r1=744272&r2=744273&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/List.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/List.h Fri Feb 13 22:47:59
2009
@@ -23,7 +23,7 @@
#include <decaf/util/concurrent/Synchronizable.h>
#include <decaf/util/Config.h>
#include <decaf/util/Iterator.h>
-#include <decaf/util/Collection.h>
+#include <decaf/util/AbstractCollection.h>
#include <decaf/util/ListIterator.h>
namespace decaf{
@@ -43,7 +43,7 @@
* to insert them, but we expect this usage to be rare.
*/
template <typename E>
- class DECAF_API List : public decaf::util::Collection<E> {
+ class DECAF_API List : public decaf::util::AbstractCollection<E> {
public:
virtual ~List() {}
Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/StlList.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/StlList.h?rev=744273&r1=744272&r2=744273&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/StlList.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/StlList.h Fri Feb 13
22:47:59 2009
@@ -35,9 +35,8 @@
namespace util{
/**
- * Set template that wraps around a std::set to provide a more
- * user-friendly interface and to provide common functions that do
- * not exist in std::list.
+ * List class that wraps the STL list object to provide a simpler
interface and
+ * additional methods not provided by the STL type.
*/
template <typename E>
class DECAF_API StlList : public decaf::util::List<E> {
@@ -57,11 +56,6 @@
public:
- StlListIterator( typename std::list<E>* list ) {
- this->current = list->begin();
- this->prev = list->end();
- this->list = list;
- }
StlListIterator( typename std::list<E>* list, std::size_t index ) {
this->current = list->begin();
std::advance( this->current, index );
@@ -162,11 +156,6 @@
public:
- ConstStlListIterator( const typename std::list<E>* list ) {
- this->current = list->begin();
- this->prev = list->end();
- this->list = list;
- }
ConstStlListIterator( const typename std::list<E>* list,
std::size_t index ) {
this->current = list->begin();
std::advance( this->current, index );
@@ -282,41 +271,25 @@
virtual bool equals( const StlList& source ) const {
return this->values == source.values;
}
- virtual bool equals( const Collection<E>& source ) const {
- if( this->values.size() != source.size() ) {
- return false;
- }
-
- std::auto_ptr< Iterator<E> > srcIter( source.iterator() );
- std::auto_ptr< Iterator<E> > thisIter( this->iterator() );
-
- while( srcIter->hasNext() ) {
- if( !( thisIter->next() == srcIter->next() ) ) {
- return false;
- }
- }
-
- return true;
- }
/**
* {...@inheritdoc}
*/
virtual Iterator<E>* iterator() {
- return new StlListIterator( &values );
+ return new StlListIterator( &values, 0 );
}
virtual Iterator<E>* iterator() const {
- return new ConstStlListIterator( &values );
+ return new ConstStlListIterator( &values, 0 );
}
/**
* {...@inheritdoc}
*/
virtual ListIterator<E>* listIterator() {
- return new StlListIterator( &values );
+ return new StlListIterator( &values, 0 );
}
virtual ListIterator<E>* listIterator() const {
- return new ConstStlListIterator( &values );
+ return new ConstStlListIterator( &values, 0 );
}
/**
@@ -352,14 +325,6 @@
this->values.clear();
this->values = source.values;
}
- virtual void copy( const Collection<E>& source ) {
- this->values.clear();
-
- std::auto_ptr< Iterator<E> > srcIter( source.iterator() );
- while( srcIter->hasNext() ) {
- this->add( srcIter->next() );
- }
- }
/**
* {...@inheritdoc}
@@ -380,22 +345,6 @@
/**
* {...@inheritdoc}
*/
- virtual bool containsAll( const Collection<E>& source ) const
- throw ( lang::Exception ) {
-
- std::auto_ptr< Iterator<E> > srcIter( source.iterator() );
- while( srcIter->hasNext() ) {
- if( !this->contains( srcIter->next() ) ) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * {...@inheritdoc}
- */
virtual std::size_t indexOf( const E& value )
throw ( decaf::lang::exceptions::NoSuchElementException ) {
@@ -518,16 +467,6 @@
/**
* {...@inheritdoc}
*/
- virtual bool addAll( const Collection<E>& source )
- throw ( lang::exceptions::UnsupportedOperationException,
- lang::exceptions::IllegalArgumentException ) {
-
- return this->addAll( 0, source );
- }
-
- /**
- * {...@inheritdoc}
- */
virtual bool addAll( std::size_t index, const Collection<E>& source )
throw ( decaf::lang::exceptions::UnsupportedOperationException,
decaf::lang::exceptions::IndexOutOfBoundsException ) {
@@ -580,56 +519,6 @@
return oldValue;
}
- /**
- * {...@inheritdoc}
- */
- virtual bool removeAll( const Collection<E>& source )
- throw ( lang::exceptions::UnsupportedOperationException,
- lang::exceptions::IllegalArgumentException ) {
-
- std::size_t origSize = this->size();
- std::auto_ptr< Iterator<E> > srcIter( source.iterator() );
- while( srcIter->hasNext() ) {
- this->remove( srcIter->next() );
- }
-
- return origSize == this->size();
- }
-
- /**
- * {...@inheritdoc}
- */
- virtual bool retainAll( const Collection<E>& collection )
- throw ( lang::exceptions::UnsupportedOperationException,
- lang::exceptions::IllegalArgumentException ) {
-
- bool result = false;
- std::auto_ptr< Iterator<E> > iter( this->iterator() );
- while( iter->hasNext() ) {
- if( !collection.contains( iter->next() ) ) {
- iter->remove();
- result = true;
- }
- }
-
- return result;
- }
-
- /**
- * {...@inheritdoc}
- */
- virtual std::vector<E> toArray() const {
- std::vector<E> valueArray( values.size() );
-
- typename std::list<E>::const_iterator iter;
- iter=values.begin();
- for( int ix=0; iter != values.end(); ++iter, ++ix ){
- valueArray[ix] = *iter;
- }
-
- return valueArray;
- }
-
public:
virtual void lock() throw( lang::Exception ) {