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 2012/07/26 00:57:12 UTC
svn commit: r1365834 [2/2] - in
/activemq/activemq-cpp/trunk/activemq-cpp/src: main/ main/activemq/cmsutil/
main/activemq/commands/ main/activemq/core/ main/activemq/state/
main/activemq/transport/ main/activemq/transport/failover/
main/activemq/util/ ...
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlMap.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlMap.h?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlMap.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlMap.h Wed Jul 25 22:57:11 2012
@@ -19,11 +19,19 @@
#define _DECAF_UTIL_STLMAP_H_
#include <map>
-#include <vector>
+#include <memory>
+#include <decaf/lang/Pointer.h>
+#include <decaf/lang/exceptions/UnsupportedOperationException.h>
+#include <decaf/util/ConcurrentModificationException.h>
#include <decaf/util/NoSuchElementException.h>
+#include <decaf/util/AbstractSet.h>
+#include <decaf/util/AbstractCollection.h>
#include <decaf/util/concurrent/Synchronizable.h>
#include <decaf/util/concurrent/Mutex.h>
#include <decaf/util/Map.h>
+#include <decaf/util/Collection.h>
+#include <decaf/util/Set.h>
+#include <decaf/util/Iterator.h>
namespace decaf{
namespace util{
@@ -41,6 +49,576 @@ namespace util{
std::map<K,V,COMPARATOR> valueMap;
mutable concurrent::Mutex mutex;
+ int modCount;
+
+ private:
+
+ class AbstractMapIterator {
+ protected:
+
+ mutable int position;
+ int expectedModCount;
+ typename std::map<K,V,COMPARATOR>::iterator futureEntry;
+ typename std::map<K,V,COMPARATOR>::iterator currentEntry;
+
+ StlMap* associatedMap;
+
+ public:
+
+ AbstractMapIterator(StlMap* parent) : position(0),
+ expectedModCount(parent->modCount),
+ futureEntry(parent->valueMap.begin()),
+ currentEntry(parent->valueMap.end()),
+ associatedMap(parent) {
+ }
+
+ virtual ~AbstractMapIterator() {}
+
+ virtual bool checkHasNext() const {
+ if (futureEntry != this->associatedMap->valueMap.end()) {
+ return true;
+ }
+ return false;
+ }
+
+ void checkConcurrentMod() const {
+ if (expectedModCount != this->associatedMap->modCount) {
+ throw ConcurrentModificationException(
+ __FILE__, __LINE__, "StlMap modified outside this iterator");
+ }
+ }
+
+ void makeNext() {
+ checkConcurrentMod();
+
+ if (!checkHasNext()) {
+ throw NoSuchElementException(__FILE__, __LINE__, "No next element");
+ }
+
+ currentEntry = futureEntry;
+ futureEntry++;
+ }
+
+ virtual void doRemove() {
+
+ checkConcurrentMod();
+
+ if (currentEntry == this->associatedMap->valueMap.end()) {
+ throw decaf::lang::exceptions::IllegalStateException(
+ __FILE__, __LINE__, "Remove called before call to next()");
+ }
+
+ this->associatedMap->valueMap.erase(currentEntry);
+ currentEntry = this->associatedMap->valueMap.end();
+
+ expectedModCount++;
+ associatedMap->modCount++;
+ }
+ };
+
+ class EntryIterator : public Iterator< MapEntry<K,V> >, public AbstractMapIterator {
+ private:
+
+ EntryIterator(const EntryIterator&);
+ EntryIterator& operator= (const EntryIterator&);
+
+ public:
+
+ EntryIterator(StlMap* parent) : AbstractMapIterator(parent) {
+ }
+
+ virtual ~EntryIterator() {}
+
+ virtual bool hasNext() const {
+ return this->checkHasNext();
+ }
+
+ virtual MapEntry<K, V> next() {
+ this->makeNext();
+ return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
+ }
+
+ virtual void remove() {
+ this->doRemove();
+ }
+ };
+
+ class KeyIterator : public Iterator<K>, public AbstractMapIterator {
+ private:
+
+ KeyIterator(const KeyIterator&);
+ KeyIterator& operator= (const KeyIterator&);
+
+ public:
+
+ KeyIterator(StlMap* parent) : AbstractMapIterator(parent) {
+ }
+
+ virtual ~KeyIterator() {}
+
+ virtual bool hasNext() const {
+ return this->checkHasNext();
+ }
+
+ virtual K next() {
+ this->makeNext();
+ return this->currentEntry->first;
+ }
+
+ virtual void remove() {
+ this->doRemove();
+ }
+ };
+
+ class ValueIterator : public Iterator<V>, public AbstractMapIterator {
+ private:
+
+ ValueIterator(const ValueIterator&);
+ ValueIterator& operator= (const ValueIterator&);
+
+ public:
+
+ ValueIterator(StlMap* parent) : AbstractMapIterator(parent) {
+ }
+
+ virtual ~ValueIterator() {}
+
+ virtual bool hasNext() const {
+ return this->checkHasNext();
+ }
+
+ virtual V next() {
+ this->makeNext();
+ return this->currentEntry->second;
+ }
+
+ virtual void remove() {
+ this->doRemove();
+ }
+ };
+
+ private:
+
+ class ConstAbstractMapIterator {
+ protected:
+
+ mutable int position;
+ int expectedModCount;
+ typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
+ typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
+
+ const StlMap* associatedMap;
+
+ public:
+
+ ConstAbstractMapIterator(const StlMap* parent) : position(0),
+ expectedModCount(parent->modCount),
+ futureEntry(parent->valueMap.begin()),
+ currentEntry(parent->valueMap.end()),
+ associatedMap(parent) {
+ }
+
+ virtual ~ConstAbstractMapIterator() {}
+
+ virtual bool checkHasNext() const {
+ if (futureEntry != this->associatedMap->valueMap.end()) {
+ return true;
+ }
+ return false;
+ }
+
+ void checkConcurrentMod() const {
+ if (expectedModCount != this->associatedMap->modCount) {
+ throw ConcurrentModificationException(
+ __FILE__, __LINE__, "StlMap modified outside this iterator");
+ }
+ }
+
+ void makeNext() {
+ checkConcurrentMod();
+
+ if (!checkHasNext()) {
+ throw NoSuchElementException(__FILE__, __LINE__, "No next element");
+ }
+
+ currentEntry = futureEntry;
+ futureEntry++;
+ }
+ };
+
+ class ConstEntryIterator : public Iterator< MapEntry<K,V> >, public ConstAbstractMapIterator {
+ private:
+
+ ConstEntryIterator(const ConstEntryIterator&);
+ ConstEntryIterator& operator= (const ConstEntryIterator&);
+
+ public:
+
+ ConstEntryIterator(const StlMap* parent) : ConstAbstractMapIterator(parent) {
+ }
+
+ virtual ~ConstEntryIterator() {}
+
+ virtual bool hasNext() const {
+ return this->checkHasNext();
+ }
+
+ virtual MapEntry<K, V> next() {
+ this->makeNext();
+ return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
+ }
+
+ virtual void remove() {
+ throw lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Cannot write to a const Iterator." );
+ }
+ };
+
+ class ConstKeyIterator : public Iterator<K>, public ConstAbstractMapIterator {
+ private:
+
+ ConstKeyIterator(const ConstKeyIterator&);
+ ConstKeyIterator& operator= (const ConstKeyIterator&);
+
+ public:
+
+ ConstKeyIterator(const StlMap* parent) : ConstAbstractMapIterator(parent) {
+ }
+
+ virtual ~ConstKeyIterator() {}
+
+ virtual bool hasNext() const {
+ return this->checkHasNext();
+ }
+
+ virtual K next() {
+ this->makeNext();
+ return this->currentEntry->first;
+ }
+
+ virtual void remove() {
+ throw lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Cannot write to a const Iterator." );
+ }
+ };
+
+ class ConstValueIterator : public Iterator<V>, public ConstAbstractMapIterator {
+ private:
+
+ ConstValueIterator(const ConstValueIterator&);
+ ConstValueIterator& operator= (const ConstValueIterator&);
+
+ public:
+
+ ConstValueIterator(const StlMap* parent) : ConstAbstractMapIterator(parent) {
+ }
+
+ virtual ~ConstValueIterator() {}
+
+ virtual bool hasNext() const {
+ return this->checkHasNext();
+ }
+
+ virtual V next() {
+ this->makeNext();
+ return this->currentEntry->second;
+ }
+
+ virtual void remove() {
+ throw lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Cannot write to a const Iterator." );
+ }
+ };
+
+ private:
+
+ // Special Set implementation that is backed by this HashMap
+ class StlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
+ private:
+
+ StlMap* associatedMap;
+
+ private:
+
+ StlMapEntrySet(const StlMapEntrySet&);
+ StlMapEntrySet& operator= (const StlMapEntrySet&);
+
+ public:
+
+ StlMapEntrySet(StlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
+ }
+
+ virtual ~StlMapEntrySet() {}
+
+ virtual int size() const {
+ return associatedMap->size();
+ }
+
+ virtual void clear() {
+ associatedMap->clear();
+ }
+
+ virtual bool remove(const MapEntry<K,V>& entry) {
+ if (this->associatedMap->containsKey(entry.getKey()) &&
+ this->associatedMap->get(entry.getKey()) == entry.getValue()) {
+ associatedMap->remove(entry.getKey());
+ return true;
+ }
+
+ return false;
+ }
+
+ virtual bool contains(const MapEntry<K,V>& entry) {
+ if (this->associatedMap->containsKey(entry.getKey()) &&
+ this->associatedMap->get(entry.getKey()) == entry.getValue()) {
+ return true;
+ }
+ return false;
+ }
+
+ virtual Iterator< MapEntry<K, V> >* iterator() {
+ return new EntryIterator(associatedMap);
+ }
+
+ virtual Iterator< MapEntry<K, V> >* iterator() const {
+ return new ConstEntryIterator(associatedMap);
+ }
+ };
+
+ // Special Set implementation that is backed by this HashMap
+ class ConstStlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
+ private:
+
+ const StlMap* associatedMap;
+
+ private:
+
+ ConstStlMapEntrySet(const ConstStlMapEntrySet&);
+ ConstStlMapEntrySet& operator= (const ConstStlMapEntrySet&);
+
+ public:
+
+ ConstStlMapEntrySet(const StlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
+ }
+
+ virtual ~ConstStlMapEntrySet() {}
+
+ virtual int size() const {
+ return associatedMap->size();
+ }
+
+ virtual void clear() {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't clear a const collection");
+ }
+
+ virtual bool remove(const MapEntry<K,V>& entry DECAF_UNUSED) {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't remove from const collection");
+ }
+
+ virtual bool contains(const MapEntry<K,V>& entry) {
+ if (this->associatedMap->containsKey(entry.getKey()) &&
+ this->associatedMap->get(entry.getKey()) == entry.getValue()) {
+ return true;
+ }
+ return false;
+ }
+
+ virtual Iterator< MapEntry<K, V> >* iterator() {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
+ }
+
+ virtual Iterator< MapEntry<K, V> >* iterator() const {
+ return new ConstEntryIterator(associatedMap);
+ }
+ };
+
+ private:
+
+ class StlMapKeySet : public AbstractSet<K> {
+ private:
+
+ StlMap* associatedMap;
+
+ private:
+
+ StlMapKeySet(const StlMapKeySet&);
+ StlMapKeySet& operator= (const StlMapKeySet&);
+
+ public:
+
+ StlMapKeySet(StlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
+ }
+
+ virtual ~StlMapKeySet() {}
+
+ virtual bool contains(const K& key) const {
+ return this->associatedMap->containsKey(key);
+ }
+
+ virtual int size() const {
+ return this->associatedMap->size();
+ }
+
+ virtual void clear() {
+ this->associatedMap->clear();
+ }
+
+ virtual bool remove(const K& key) {
+ if (this->associatedMap->containsKey(key)) {
+ associatedMap->remove(key);
+ return true;
+ }
+ return false;
+ }
+
+ virtual Iterator<K>* iterator() {
+ return new KeyIterator(this->associatedMap);
+ }
+
+ virtual Iterator<K>* iterator() const {
+ return new ConstKeyIterator(this->associatedMap);
+ }
+ };
+
+ class ConstStlMapKeySet : public AbstractSet<K> {
+ private:
+
+ const StlMap* associatedMap;
+
+ private:
+
+ ConstStlMapKeySet(const ConstStlMapKeySet&);
+ ConstStlMapKeySet& operator= (const ConstStlMapKeySet&);
+
+ public:
+
+ ConstStlMapKeySet(const StlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
+ }
+
+ virtual ~ConstStlMapKeySet() {}
+
+ virtual bool contains(const K& key) const {
+ return this->associatedMap->containsKey(key);
+ }
+
+ virtual int size() const {
+ return this->associatedMap->size();
+ }
+
+ virtual void clear() {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't modify a const collection");
+ }
+
+ virtual bool remove(const K& key DECAF_UNUSED) {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't modify a const collection");
+ }
+
+ virtual Iterator<K>* iterator() {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
+ }
+
+ virtual Iterator<K>* iterator() const {
+ return new ConstKeyIterator(this->associatedMap);
+ }
+ };
+
+ private:
+
+ class StlMapValueCollection : public AbstractCollection<V> {
+ private:
+
+ StlMap* associatedMap;
+
+ private:
+
+ StlMapValueCollection(const StlMapValueCollection&);
+ StlMapValueCollection& operator= (const StlMapValueCollection&);
+
+ public:
+
+ StlMapValueCollection(StlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
+ }
+
+ virtual ~StlMapValueCollection() {}
+
+ virtual bool contains(const V& value) const {
+ return this->associatedMap->containsValue(value);
+ }
+
+ virtual int size() const {
+ return this->associatedMap->size();
+ }
+
+ virtual void clear() {
+ this->associatedMap->clear();
+ }
+
+ virtual Iterator<V>* iterator() {
+ return new ValueIterator(this->associatedMap);
+ }
+
+ virtual Iterator<V>* iterator() const {
+ return new ConstValueIterator(this->associatedMap);
+ }
+ };
+
+ class ConstStlMapValueCollection : public AbstractCollection<V> {
+ private:
+
+ const StlMap* associatedMap;
+
+ private:
+
+ ConstStlMapValueCollection(const ConstStlMapValueCollection&);
+ ConstStlMapValueCollection& operator= (const ConstStlMapValueCollection&);
+
+ public:
+
+ ConstStlMapValueCollection(const StlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
+ }
+
+ virtual ~ConstStlMapValueCollection() {}
+
+ virtual bool contains(const V& value) const {
+ return this->associatedMap->containsValue(value);
+ }
+
+ virtual int size() const {
+ return this->associatedMap->size();
+ }
+
+ virtual void clear() {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't modify a const collection");
+ }
+
+ virtual Iterator<V>* iterator() {
+ throw decaf::lang::exceptions::UnsupportedOperationException(
+ __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
+ }
+
+ virtual Iterator<V>* iterator() const {
+ return new ConstValueIterator(this->associatedMap);
+ }
+ };
+
+ private:
+
+ // Cached values that are only initialized once a request for them is made.
+ decaf::lang::Pointer<StlMapEntrySet> cachedEntrySet;
+ decaf::lang::Pointer<StlMapKeySet> cachedKeySet;
+ decaf::lang::Pointer<StlMapValueCollection> cachedValueCollection;
+
+ // Cached values that are only initialized once a request for them is made.
+ mutable decaf::lang::Pointer<ConstStlMapEntrySet> cachedConstEntrySet;
+ mutable decaf::lang::Pointer<ConstStlMapKeySet> cachedConstKeySet;
+ mutable decaf::lang::Pointer<ConstStlMapValueCollection> cachedConstValueCollection;
public:
@@ -50,21 +628,23 @@ namespace util{
StlMap() : Map<K,V>(), valueMap(), mutex() {}
/**
- * Copy constructor - copies the content of the given map into this
- * one.
- * @param source The source map.
+ * Copy constructor - copies the content of the given map into this one.
+ *
+ * @param source
+ * The source StlMap whose entries are copied into this Map.
*/
- StlMap( const StlMap& source ) : Map<K,V>(), valueMap(), mutex() {
- copy( source );
+ StlMap(const StlMap& source ) : Map<K,V>(), valueMap(), mutex() {
+ copy(source);
}
/**
- * Copy constructor - copies the content of the given map into this
- * one.
- * @param source The source map.
+ * Copy constructor - copies the content of the given map into this one.
+ *
+ * @param source
+ * The source ma whose entries are copied into this Map..
*/
- StlMap( const Map<K,V>& source ) : Map<K,V>(), valueMap(), mutex() {
- copy( source );
+ StlMap(const Map<K,V>& source) : Map<K,V>(), valueMap(), mutex() {
+ copy(source);
}
virtual ~StlMap() {}
@@ -72,23 +652,23 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual bool equals( const StlMap& source ) const {
+ virtual bool equals(const StlMap& source) const {
return this->valueMap == source.valueMap;
}
/**
* {@inheritDoc}
*/
- virtual bool equals( const Map<K,V>& source ) const {
- std::vector<K> keys = source.keySet();
+ virtual bool equals(const Map<K,V>& source) const {
- typename std::vector<K>::const_iterator iter = keys.begin();
- for( ; iter != keys.end(); ++iter ) {
- if( !this->containsKey( *iter ) ) {
+ typename std::auto_ptr< Iterator<K> > iterator(this->keySet().iterator());
+ while (iterator->hasNext()) {
+ K key = iterator->next();
+ if (!this->containsKey(key)) {
return false;
}
- if( !( this->get( *iter ) == source.get( *iter ) ) ) {
+ if (!(this->get(key) == source.get(key))) {
return false;
}
}
@@ -99,7 +679,7 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual void copy( const StlMap& source ) {
+ virtual void copy(const StlMap& source) {
this->valueMap.clear();
this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
}
@@ -107,9 +687,9 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual void copy( const Map<K,V>& source ) {
+ virtual void copy(const Map<K, V>& source) {
this->clear();
- this->putAll( source );
+ this->putAll(source);
}
/**
@@ -122,8 +702,13 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual bool containsKey( const K& key ) const {
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
+ virtual bool containsKey(const K& key) const {
+
+ if (valueMap.empty()) {
+ return false;
+ }
+
+ typename std::map<K, V, COMPARATOR>::const_iterator iter;
iter = valueMap.find(key);
return iter != valueMap.end();
}
@@ -131,15 +716,15 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual bool containsValue( const V& value ) const {
+ virtual bool containsValue(const V& value) const {
- if( valueMap.empty() ){
+ if (valueMap.empty()) {
return false;
}
- typename std::map<K,V,COMPARATOR>::const_iterator iter = valueMap.begin();
- for( ; iter != valueMap.end(); ++iter ){
- if( (*iter).second == value ) {
+ typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
+ for (; iter != valueMap.end(); ++iter) {
+ if ((*iter).second == value) {
return true;
}
}
@@ -164,13 +749,12 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual V& get( const K& key ) {
+ virtual V& get(const K& key) {
- typename std::map<K,V,COMPARATOR>::iterator iter;
- iter = valueMap.find( key );
- if( iter == valueMap.end() ){
- throw NoSuchElementException(
- __FILE__, __LINE__, "Key does not exist in map" );
+ typename std::map<K, V, COMPARATOR>::iterator iter;
+ iter = valueMap.find(key);
+ if (iter == valueMap.end()) {
+ throw NoSuchElementException(__FILE__, __LINE__, "Key does not exist in map");
}
return iter->second;
@@ -179,13 +763,12 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual const V& get( const K& key ) const {
+ virtual const V& get(const K& key) const {
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter = valueMap.find( key );
- if( iter == valueMap.end() ){
- throw NoSuchElementException(
- __FILE__, __LINE__, "Key does not exist in map" );
+ typename std::map<K, V, COMPARATOR>::const_iterator iter;
+ iter = valueMap.find(key);
+ if (iter == valueMap.end()) {
+ throw NoSuchElementException(__FILE__, __LINE__, "Key does not exist in map");
}
return iter->second;
@@ -194,83 +777,101 @@ namespace util{
/**
* {@inheritDoc}
*/
- virtual void put( const K& key, const V& value ) {
+ virtual bool put(const K& key, const V& value) {
+ bool result = false;
+ if (this->containsKey(key)) {
+ result = true;
+ }
valueMap[key] = value;
+ return result;
}
/**
* {@inheritDoc}
*/
- virtual void putAll( const StlMap<K,V,COMPARATOR>& other ) {
- this->valueMap.insert( other.valueMap.begin(), other.valueMap.end() );
+ virtual bool put(const K& key, const V& value, V& oldValue) {
+ bool result = false;
+ if (this->containsKey(key)) {
+ result = true;
+ oldValue = valueMap[key];
+ }
+ valueMap[key] = value;
+ return result;
}
/**
* {@inheritDoc}
*/
- virtual void putAll( const Map<K,V>& other ) {
-
- std::vector<K> keys = other.keySet();
-
- typename std::vector<K>::const_iterator iter = keys.begin();
- for( ; iter != keys.end(); ++iter ) {
+ virtual void putAll(const StlMap<K, V, COMPARATOR>& other) {
+ this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
+ }
- this->put( *iter, other.get( *iter ) );
+ /**
+ * {@inheritDoc}
+ */
+ virtual void putAll(const Map<K, V>& other) {
+ typename std::auto_ptr< Iterator<K> > iterator(other.keySet().iterator());
+ while (iterator->hasNext()) {
+ K key = iterator->next();
+ this->put(key, other.get(key));
}
}
/**
* {@inheritDoc}
*/
- virtual V remove( const K& key ) {
+ virtual V remove(const K& key) {
- typename std::map<K,V,COMPARATOR>::iterator iter = valueMap.find( key );
- if( iter == valueMap.end() ) {
+ typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
+ if (iter == valueMap.end()) {
throw NoSuchElementException(
- __FILE__, __LINE__, "Key is not present in this Map." );
+ __FILE__, __LINE__, "Key is not present in this Map.");
}
V result = iter->second;
- valueMap.erase( iter );
+ valueMap.erase(iter);
return result;
}
- /**
- * {@inheritDoc}
- */
- virtual std::vector<K> keySet() const{
- std::vector<K> keys( valueMap.size() );
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- keys[ix] = iter->first;
+ virtual Set< MapEntry<K, V> >& entrySet() {
+ if (this->cachedEntrySet == NULL) {
+ this->cachedEntrySet.reset(new StlMapEntrySet(this));
}
-
- return keys;
+ return *(this->cachedEntrySet);
+ }
+ virtual const Set< MapEntry<K, V> >& entrySet() const {
+ if (this->cachedConstEntrySet == NULL) {
+ this->cachedConstEntrySet.reset(new ConstStlMapEntrySet(this));
+ }
+ return *(this->cachedConstEntrySet);
}
- /**
- * {@inheritDoc}
- */
- virtual std::vector<V> values() const {
- std::vector<V> values( valueMap.size() );
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- values[ix] = iter->second;
+ virtual Set<K>& keySet() {
+ if (this->cachedKeySet == NULL) {
+ this->cachedKeySet.reset(new StlMapKeySet(this));
}
+ return *(this->cachedKeySet);
+ }
- return values;
+ virtual const Set<K>& keySet() const {
+ if (this->cachedConstKeySet == NULL) {
+ this->cachedConstKeySet.reset(new ConstStlMapKeySet(this));
+ }
+ return *(this->cachedConstKeySet);
}
- virtual Set< MapEntry<K, V> >* entrySet() {
- throw decaf::lang::exceptions::UnsupportedOperationException();
+ virtual Collection<V>& values() {
+ if (this->cachedValueCollection == NULL) {
+ this->cachedValueCollection.reset(new StlMapValueCollection(this));
+ }
+ return *(this->cachedValueCollection);
}
- virtual Set< MapEntry<K, V> >* entrySet() const {
- throw decaf::lang::exceptions::UnsupportedOperationException();
+ virtual const Collection<V>& values() const {
+ if (this->cachedConstValueCollection == NULL) {
+ this->cachedConstValueCollection.reset(new ConstStlMapValueCollection(this));
+ }
+ return *(this->cachedConstValueCollection);
}
public:
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ConcurrentStlMap.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ConcurrentStlMap.h?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ConcurrentStlMap.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ConcurrentStlMap.h Wed Jul 25 22:57:11 2012
@@ -25,6 +25,9 @@
#include <decaf/util/concurrent/ConcurrentMap.h>
#include <decaf/util/concurrent/Mutex.h>
#include <decaf/util/Map.h>
+#include <decaf/util/Collection.h>
+#include <decaf/util/Set.h>
+#include <decaf/util/Iterator.h>
namespace decaf{
namespace util{
@@ -61,10 +64,8 @@ namespace concurrent{
* one.
* @param source The source map.
*/
- ConcurrentStlMap( const ConcurrentStlMap& source ) :
- ConcurrentMap<K,V>(), valueMap(), mutex() {
-
- copy( source );
+ ConcurrentStlMap(const ConcurrentStlMap& source) : ConcurrentMap<K, V>(), valueMap(), mutex() {
+ copy(source);
}
/**
@@ -72,10 +73,8 @@ namespace concurrent{
* one.
* @param source The source map.
*/
- ConcurrentStlMap( const Map<K,V>& source ) :
- ConcurrentMap<K,V>(), valueMap(), mutex() {
-
- copy( source );
+ ConcurrentStlMap(const Map<K, V>& source) : ConcurrentMap<K, V>(), valueMap(), mutex() {
+ copy(source);
}
virtual ~ConcurrentStlMap() {}
@@ -83,26 +82,25 @@ namespace concurrent{
/**
* {@inheritDoc}
*/
- virtual bool equals( const ConcurrentStlMap& source ) const {
- synchronized( &mutex ) {
+ virtual bool equals(const ConcurrentStlMap& source) const {
+ synchronized(&mutex) {
return this->valueMap == source.valueMap;
}
return false;
}
- virtual bool equals( const Map<K,V>& source ) const {
-
- synchronized( &mutex ) {
- std::vector<K> keys = source.keySet();
+ virtual bool equals(const Map<K, V>& source) const {
- typename std::vector<K>::const_iterator iter = keys.begin();
- for( ; iter != keys.end(); ++iter ) {
- if( !this->containsKey( *iter ) ) {
+ synchronized(&mutex) {
+ typename std::auto_ptr< Iterator<K> > iterator(this->keySet().iterator());
+ while (iterator->hasNext()) {
+ K key = iterator->next();
+ if (!this->containsKey(key)) {
return false;
}
- if( !( this->get( *iter ) == source.get( *iter ) ) ) {
+ if (!(this->get(key) == source.get(key))) {
return false;
}
}
@@ -114,17 +112,17 @@ namespace concurrent{
/**
* {@inheritDoc}
*/
- virtual void copy( const ConcurrentStlMap& source ) {
- synchronized( &mutex ) {
+ virtual void copy(const ConcurrentStlMap& source) {
+ synchronized(&mutex) {
this->valueMap.clear();
- this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
+ this->valueMap.insert(source.valueMap.begin(), source.valueMap.end());
}
}
- virtual void copy( const Map<K,V>& source ) {
+ virtual void copy(const Map<K, V>& source) {
synchronized( &mutex ) {
this->clear();
- this->putAll( source );
+ this->putAll(source);
}
}
@@ -132,7 +130,7 @@ namespace concurrent{
* {@inheritDoc}
*/
virtual void clear() {
- synchronized( &mutex ) {
+ synchronized(&mutex) {
valueMap.clear();
}
}
@@ -140,10 +138,10 @@ namespace concurrent{
/**
* {@inheritDoc}
*/
- virtual bool containsKey( const K& key ) const {
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
+ virtual bool containsKey(const K& key) const {
+ typename std::map<K, V, COMPARATOR>::const_iterator iter;
- synchronized( &mutex ) {
+ synchronized(&mutex) {
iter = valueMap.find(key);
return iter != valueMap.end();
}
@@ -154,17 +152,17 @@ namespace concurrent{
/**
* {@inheritDoc}
*/
- virtual bool containsValue( const V& value ) const {
+ virtual bool containsValue(const V& value) const {
- synchronized( &mutex ) {
+ synchronized(&mutex) {
- if( valueMap.empty() ){
+ if (valueMap.empty()) {
return false;
}
- typename std::map<K,V,COMPARATOR>::const_iterator iter = valueMap.begin();
- for( ; iter != valueMap.end(); ++iter ){
- if( (*iter).second == value ) {
+ typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
+ for (; iter != valueMap.end(); ++iter) {
+ if ((*iter).second == value) {
return true;
}
}
@@ -177,7 +175,7 @@ namespace concurrent{
* {@inheritDoc}
*/
virtual bool isEmpty() const {
- synchronized( &mutex ) {
+ synchronized(&mutex) {
return valueMap.empty();
}
@@ -188,7 +186,7 @@ namespace concurrent{
* {@inheritDoc}
*/
virtual int size() const {
- synchronized( &mutex ) {
+ synchronized(&mutex) {
return (int)valueMap.size();
}
@@ -198,70 +196,86 @@ namespace concurrent{
/**
* {@inheritDoc}
*/
- virtual V& get( const K& key ) {
+ virtual V& get(const K& key) {
typename std::map<K,V,COMPARATOR>::iterator iter;
synchronized( &mutex ) {
- iter = valueMap.find( key );
- if( iter != valueMap.end() ){
+ iter = valueMap.find(key);
+ if (iter != valueMap.end()) {
return iter->second;
}
}
throw NoSuchElementException(
- __FILE__, __LINE__, "Key does not exist in map" );
+ __FILE__, __LINE__, "Key does not exist in map");
}
/**
* {@inheritDoc}
*/
- virtual const V& get( const K& key ) const {
+ virtual const V& get(const K& key) const {
typename std::map<K,V,COMPARATOR>::const_iterator iter;
- synchronized( &mutex ) {
- iter = valueMap.find( key );
- if( iter != valueMap.end() ){
+ synchronized(&mutex) {
+ iter = valueMap.find(key);
+ if (iter != valueMap.end()) {
return iter->second;
}
}
throw NoSuchElementException(
- __FILE__, __LINE__, "Key does not exist in map" );
+ __FILE__, __LINE__, "Key does not exist in map");
}
/**
* {@inheritDoc}
*/
- virtual void put( const K& key, const V& value ) {
-
- synchronized( &mutex ) {
+ virtual bool put(const K& key, const V& value) {
+ bool result = false;
+ synchronized(&mutex) {
+ if (this->containsKey(key)) {
+ result = true;
+ }
valueMap[key] = value;
}
+ return result;
}
/**
* {@inheritDoc}
*/
- virtual void putAll( const ConcurrentStlMap<K,V,COMPARATOR>& other ) {
-
- synchronized( &mutex ) {
- this->valueMap.insert( other.valueMap.begin(), other.valueMap.end() );
+ virtual bool put(const K& key, const V& value, V& oldValue) {
+ bool result = false;
+ synchronized(&mutex) {
+ if (this->containsKey(key)) {
+ result = true;
+ oldValue = valueMap[key];
+ }
+ valueMap[key] = value;
}
+ return result;
}
/**
* {@inheritDoc}
*/
- virtual void putAll( const Map<K,V>& other ) {
-
- synchronized( &mutex ) {
- std::vector<K> keys = other.keySet();
+ virtual void putAll(const ConcurrentStlMap<K, V, COMPARATOR>& other) {
+ synchronized(&mutex) {
+ this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
+ }
+ }
- typename std::vector<K>::const_iterator iter = keys.begin();
- for( ; iter != keys.end(); ++iter ) {
- this->put( *iter, other.get( *iter ) );
+ /**
+ * {@inheritDoc}
+ */
+ virtual void putAll(const Map<K, V>& other) {
+ synchronized(&mutex) {
+ typename std::auto_ptr< Iterator<K> > iterator(this->keySet().iterator());
+ while (iterator->hasNext()) {
+ K key = iterator->next();
+ this->put(key, other.get(key));
}
}
}
@@ -270,9 +284,7 @@ namespace concurrent{
* {@inheritDoc}
*/
virtual V remove(const K& key) {
-
V result = V();
-
synchronized(&mutex) {
typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
if (iter == valueMap.end()) {
@@ -285,41 +297,40 @@ namespace concurrent{
return result;
}
- /**
- * {@inheritDoc}
- */
- virtual std::vector<K> keySet() const {
-
- std::vector<K> keys( valueMap.size() );
- synchronized( &mutex ) {
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- keys[ix] = iter->first;
- }
- }
-
- return keys;
- }
-
- /**
- * {@inheritDoc}
- */
- virtual std::vector<V> values() const {
-
- std::vector<V> values( valueMap.size() );
- synchronized( &mutex ) {
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- values[ix] = iter->second;
- }
-
- }
- return values;
- }
+// /**
+// * {@inheritDoc}
+// */
+// virtual std::vector<K> keySet() const {
+// std::vector<K> keys( valueMap.size() );
+// synchronized( &mutex ) {
+//
+// typename std::map<K,V,COMPARATOR>::const_iterator iter;
+// iter=valueMap.begin();
+// for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
+// keys[ix] = iter->first;
+// }
+// }
+//
+// return keys;
+// }
+//
+// /**
+// * {@inheritDoc}
+// */
+// virtual std::vector<V> values() const {
+//
+// std::vector<V> values( valueMap.size() );
+// synchronized( &mutex ) {
+//
+// typename std::map<K,V,COMPARATOR>::const_iterator iter;
+// iter=valueMap.begin();
+// for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
+// values[ix] = iter->second;
+// }
+//
+// }
+// return values;
+// }
/**
* If the specified key is not already associated with a value, associate it with
@@ -449,11 +460,24 @@ namespace concurrent{
__FILE__, __LINE__, "Value to Replace was not in the Map." );
}
- virtual Set< MapEntry<K, V> >* entrySet() {
+ virtual Set< MapEntry<K, V> >& entrySet() {
+ throw decaf::lang::exceptions::UnsupportedOperationException();
+ }
+ virtual const Set< MapEntry<K, V> >& entrySet() const {
throw decaf::lang::exceptions::UnsupportedOperationException();
}
- virtual Set< MapEntry<K, V> >* entrySet() const {
+ virtual Set<K>& keySet() {
+ throw decaf::lang::exceptions::UnsupportedOperationException();
+ }
+ virtual const Set<K>& keySet() const {
+ throw decaf::lang::exceptions::UnsupportedOperationException();
+ }
+
+ virtual Collection<V>& values() {
+ throw decaf::lang::exceptions::UnsupportedOperationException();
+ }
+ virtual const Collection<V>& values() const {
throw decaf::lang::exceptions::UnsupportedOperationException();
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test-benchmarks/decaf/util/MapBenchmark.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test-benchmarks/decaf/util/MapBenchmark.cpp?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test-benchmarks/decaf/util/MapBenchmark.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test-benchmarks/decaf/util/MapBenchmark.cpp Wed Jul 25 22:57:11 2012
@@ -57,14 +57,15 @@ void MapBenchmark::run() {
intMap.put( 100 + i, 100 + i );
}
- std::vector<std::string> stringVec;
- std::vector<int> intVec;
-
for( int i = 0; i < numRuns / 2; ++i ) {
- stringVec = stringMap.keySet();
- stringVec = stringMap.values();
- intVec = intMap.keySet();
- intVec = intMap.values();
+ Set<std::string>& stringSet = stringMap.keySet();
+ stringSet.size();
+ Collection<std::string>& stringCol = stringMap.values();
+ stringCol.size();
+ Set<int>& intSet = intMap.keySet();
+ intSet.size();
+ Collection<int>& intCol = intMap.values();
+ intCol.size();
}
for( int i = 0; i < numRuns / 2; ++i ) {
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/commands/BrokerIdTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/commands/BrokerIdTest.cpp?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/commands/BrokerIdTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/commands/BrokerIdTest.cpp Wed Jul 25 22:57:11 2012
@@ -72,9 +72,11 @@ void BrokerIdTest::test() {
testMap.put( &myCommand3, 0 );
testMap.put( &myCommand2, 0 );
- CPPUNIT_ASSERT( testMap.keySet().at( 0 )->getValue() == "A" );
- CPPUNIT_ASSERT( testMap.keySet().at( 1 )->getValue() == "B" );
- CPPUNIT_ASSERT( testMap.keySet().at( 2 )->getValue() == "C" );
+ std::vector<BrokerId*> keys = testMap.keySet().toArray();
+
+ CPPUNIT_ASSERT( keys.at( 0 )->getValue() == "A" );
+ CPPUNIT_ASSERT( keys.at( 1 )->getValue() == "B" );
+ CPPUNIT_ASSERT( keys.at( 2 )->getValue() == "C" );
}
////////////////////////////////////////////////////////////////////////////////
@@ -102,6 +104,8 @@ void BrokerIdTest::test2() {
testMap.put( myCommand2, 0 );
CPPUNIT_ASSERT( testMap.size() == 2 );
- CPPUNIT_ASSERT( testMap.keySet().at( 0 )->getValue() == "A" );
- CPPUNIT_ASSERT( testMap.keySet().at( 1 )->getValue() == "C" );
+ std::vector< Pointer<BrokerId> > keys = testMap.keySet().toArray();
+
+ CPPUNIT_ASSERT( keys.at( 0 )->getValue() == "A" );
+ CPPUNIT_ASSERT( keys.at( 1 )->getValue() == "C" );
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/util/PrimitiveMapTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/util/PrimitiveMapTest.cpp?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/util/PrimitiveMapTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/util/PrimitiveMapTest.cpp Wed Jul 25 22:57:11 2012
@@ -280,7 +280,7 @@ void PrimitiveMapTest::testGetKeys(){
pmap.setInt("int", 5 );
pmap.setFloat( "float", 5.5f );
pmap.setInt("int2", 6 );
- std::vector<std::string> keys = pmap.keySet();
+ std::vector<std::string> keys = pmap.keySet().toArray();
CPPUNIT_ASSERT( keys.size() == 3 );
CPPUNIT_ASSERT( keys[0] == "int" || keys[0] == "float" || keys[0] == "int2" );
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashMapTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashMapTest.cpp?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashMapTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashMapTest.cpp Wed Jul 25 22:57:11 2012
@@ -122,20 +122,6 @@ void HashMapTest::testConstructorMap() {
CPPUNIT_ASSERT_MESSAGE("Failed to construct correct HashMap",
myMap.get(counter) == hashMap.get(counter));
}
-
-// try {
-// Map mockMap = new MockMap();
-// hm = new HashMap(mockMap);
-// fail("Should throw NullPointerException");
-// } catch (NullPointerException e) {
-// //empty
-// }
-//
-// HashMap map = new HashMap();
-// map.put("a", "a");
-// SubMap map2 = new SubMap(map);
-// assertTrue(map2.containsKey("a"));
-// assertTrue(map2.containsValue("a"));
}
////////////////////////////////////////////////////////////////////////////////
@@ -206,19 +192,19 @@ void HashMapTest::testEntrySet() {
hashMap.put(i, Integer::toString(i));
}
- Pointer< Set<MapEntry<int, std::string> > > set(hashMap.entrySet());
- Pointer< Iterator<MapEntry<int, std::string> > > iterator(set->iterator());
+ Set<MapEntry<int, std::string> >& set = hashMap.entrySet();
+ Pointer< Iterator<MapEntry<int, std::string> > > iterator(set.iterator());
- CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size", hashMap.size() == set->size());
+ CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size", hashMap.size() == set.size());
while (iterator->hasNext()) {
MapEntry<int, std::string> entry = iterator->next();
CPPUNIT_ASSERT_MESSAGE("Returned incorrect entry set",
hashMap.containsKey(entry.getKey()) && hashMap.containsValue(entry.getValue()));
}
- iterator.reset(set->iterator());
- set->remove(iterator->next());
- CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove on set didn't take", 49, set->size());
+ iterator.reset(set.iterator());
+ set.remove(iterator->next());
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove on set didn't take", 49, set.size());
}
////////////////////////////////////////////////////////////////////////////////
@@ -252,17 +238,17 @@ void HashMapTest::testKeySet() {
HashMap<int, std::string> hashMap;
populateMap(hashMap);
- Pointer< Set<int> > set(hashMap.keySet());
- CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size()", set->size() == hashMap.size());
+ Set<int>& set = hashMap.keySet();
+ CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size()", set.size() == hashMap.size());
for (int i = 0; i < MAP_SIZE; i++) {
- CPPUNIT_ASSERT_MESSAGE("Returned set does not contain all keys", set->contains(i));
+ CPPUNIT_ASSERT_MESSAGE("Returned set does not contain all keys", set.contains(i));
}
{
HashMap<int, std::string> localMap;
localMap.put(0, "test");
- Pointer< Set<int> > intSet(localMap.keySet());
- CPPUNIT_ASSERT_MESSAGE("Failed with zero key", intSet->contains(0));
+ Set<int>& intSet = localMap.keySet();
+ CPPUNIT_ASSERT_MESSAGE("Failed with zero key", intSet.contains(0));
}
{
HashMap<int, std::string> localMap;
@@ -270,8 +256,8 @@ void HashMapTest::testKeySet() {
localMap.put(102, "102");
localMap.put(203, "203");
- Pointer< Set<int> > intSet(localMap.keySet());
- Pointer< Iterator<int> > it(intSet->iterator());
+ Set<int>& intSet = localMap.keySet();
+ Pointer< Iterator<int> > it(intSet.iterator());
int remove1 = it->next();
it->hasNext();
it->remove();
@@ -288,7 +274,7 @@ void HashMapTest::testKeySet() {
CPPUNIT_ASSERT_MESSAGE("Wrong result", it->next() == list.get(0));
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong size", 1, localMap.size());
- it.reset(intSet->iterator());
+ it.reset(intSet.iterator());
CPPUNIT_ASSERT_MESSAGE("Wrong contents", it->next() == list.get(0));
}
{
@@ -296,8 +282,8 @@ void HashMapTest::testKeySet() {
map2.put(1, "1");
map2.put(4, "4");
- Pointer< Set<int> > intSet(map2.keySet());
- Pointer< Iterator<int> > it2(intSet->iterator());
+ Set<int>& intSet = map2.keySet();
+ Pointer< Iterator<int> > it2(intSet.iterator());
int remove3 = it2->next();
int next;
@@ -311,7 +297,7 @@ void HashMapTest::testKeySet() {
it2->remove();
CPPUNIT_ASSERT_MESSAGE("Wrong result 2", it2->next() == next);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong size 2", 1, map2.size());
- it2.reset(intSet->iterator());
+ it2.reset(intSet.iterator());
CPPUNIT_ASSERT_MESSAGE("Wrong contents 2", it2->next() == next);
}
}
@@ -384,8 +370,8 @@ void HashMapTest::testPut() {
CPPUNIT_ASSERT(map.containsKey(myKey));
CPPUNIT_ASSERT_EQUAL(std::string("myValue"), map.get(myKey));
bool found = false;
- Pointer< Set<int> > intSet(map.keySet());
- Pointer< Iterator<int> > itr(intSet->iterator());
+ Set<int>& intSet = map.keySet();
+ Pointer< Iterator<int> > itr(intSet.iterator());
while (itr->hasNext()) {
int key = itr->next();
if ((found = key) == myKey) {
@@ -399,7 +385,7 @@ void HashMapTest::testPut() {
map.put(myKey, "myValue");
CPPUNIT_ASSERT(map.containsKey(myKey));
CPPUNIT_ASSERT_EQUAL(std::string("myValue"), map.get(myKey));
- itr.reset(intSet->iterator());
+ itr.reset(intSet.iterator());
while (itr->hasNext()) {
int key = itr->next();
if ((found = (key == myKey))) {
@@ -518,8 +504,8 @@ void HashMapTest::testRehash() {
}
// Check expected ordering (inverse of adding order)
- Pointer< Set<MyKey> > keySet(hashMap.keySet());
- std::vector<MyKey> returnedKeys = keySet->toArray();
+ Set<MyKey>& keySet = hashMap.keySet();
+ std::vector<MyKey> returnedKeys = keySet.toArray();
for (int i = 0; i < 8; i++) {
CPPUNIT_ASSERT_EQUAL(keyOrder[i], returnedKeys[7 - i]);
}
@@ -527,7 +513,7 @@ void HashMapTest::testRehash() {
// The next put causes a rehash
hashMap.put(keyOrder[8], 8);
// Check expected new ordering (adding order)
- returnedKeys = keySet->toArray();
+ returnedKeys = keySet.toArray();
for (int i = 0; i < 9; i++) {
CPPUNIT_ASSERT_EQUAL(keyOrder[i], returnedKeys[i]);
}
@@ -547,14 +533,14 @@ void HashMapTest::testValues() {
HashMap<int, std::string> hashMap;
populateMap(hashMap);
- Pointer< Collection<std::string> > c(hashMap.values());
- CPPUNIT_ASSERT_MESSAGE("Returned collection of incorrect size()", c->size() == hashMap.size());
+ Collection<std::string>& c = hashMap.values();
+ CPPUNIT_ASSERT_MESSAGE("Returned collection of incorrect size()", c.size() == hashMap.size());
for (int i = 0; i < MAP_SIZE; i++) {
CPPUNIT_ASSERT_MESSAGE("Returned collection does not contain all keys",
- c->contains(Integer::toString(i)));
+ c.contains(Integer::toString(i)));
}
- c->remove("10");
+ c.remove("10");
CPPUNIT_ASSERT_MESSAGE("Removing from collection should alter Map",
!hashMap.containsKey(10));
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.cpp?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.cpp Wed Jul 25 22:57:11 2012
@@ -18,278 +18,28 @@
#include "StlMapTest.h"
#include <string>
+#include <decaf/util/HashMap.h>
#include <decaf/util/StlMap.h>
+#include <decaf/util/ArrayList.h>
+#include <decaf/lang/Integer.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
using namespace std;
using namespace decaf;
using namespace decaf::util;
+using namespace decaf::lang;
-template <typename K, typename V, typename COMPARATOR = std::less<K> >
-class StlTestMap : public Map<K, V> {
-private:
-
- std::map<K,V,COMPARATOR> valueMap;
- concurrent::Mutex mutex;
-
-public:
-
- /**
- * Default constructor - does nothing.
- */
- StlTestMap() : Map<K,V>() {}
-
- /**
- * Copy constructor - copies the content of the given map into this
- * one.
- * @param source The source map.
- */
- StlTestMap( const StlTestMap& source ) : Map<K,V>() {
- copy( source );
- }
-
- /**
- * Copy constructor - copies the content of the given map into this
- * one.
- * @param source The source map.
- */
- StlTestMap( const Map<K,V>& source ) : Map<K,V>() {
- copy( source );
- }
-
- virtual ~StlTestMap() {}
-
- /**
- * {@inheritDoc}
- */
- virtual bool equals( const StlTestMap& source ) const {
- return this->valueMap == source.valueMap;
- }
-
- virtual bool equals( const Map<K,V>& source ) const {
- std::vector<K> keys = source.keySet();
-
- typename std::vector<K>::const_iterator iter = keys.begin();
- for( ; iter != keys.end(); ++iter ) {
- if( !this->containsKey( *iter ) ) {
- return false;
- }
-
- if( !( this->get( *iter ) == source.get( *iter ) ) ) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * {@inheritDoc}
- */
- virtual void copy( const StlTestMap& source ) {
- this->valueMap.clear();
- this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
- }
-
- virtual void copy( const Map<K,V>& source ) {
- this->clear();
- this->putAll( source );
- }
-
- /**
- * {@inheritDoc}
- */
- virtual void clear() {
- valueMap.clear();
- }
-
- /**
- * {@inheritDoc}
- */
- virtual bool containsKey( const K& key ) const {
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter = valueMap.find(key);
- return iter != valueMap.end();
- }
-
- /**
- * {@inheritDoc}
- */
- virtual bool containsValue( const V& value ) const {
-
- if( valueMap.empty() ){
- return false;
- }
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter = valueMap.begin();
- for( ; iter != valueMap.end(); ++iter ){
- if( (*iter).second == value ) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- virtual bool isEmpty() const {
- return valueMap.empty();
- }
-
- /**
- * {@inheritDoc}
- */
- virtual int size() const {
- return (int)valueMap.size();
- }
-
- /**
- * {@inheritDoc}
- */
- virtual V& get( const K& key ) {
-
- typename std::map<K,V,COMPARATOR>::iterator iter;
- iter = valueMap.find( key );
- if( iter == valueMap.end() ){
- throw util::NoSuchElementException(
- __FILE__, __LINE__, "Key does not exist in map" );
- }
-
- return iter->second;
- }
- virtual const V& get( const K& key ) const {
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter = valueMap.find( key );
- if( iter == valueMap.end() ){
- throw util::NoSuchElementException(
- __FILE__, __LINE__, "Key does not exist in map" );
- }
-
- return iter->second;
- }
-
- /**
- * {@inheritDoc}
- */
- virtual void put( const K& key, const V& value ) {
-
- valueMap[key] = value;
- }
-
- /**
- * {@inheritDoc}
- */
- virtual void putAll( const StlTestMap<K,V,COMPARATOR>& other ) {
-
- this->valueMap.insert( other.valueMap.begin(), other.valueMap.end() );
- }
- virtual void putAll( const Map<K,V>& other ) {
-
- std::vector<K> keys = other.keySet();
-
- typename std::vector<K>::const_iterator iter = keys.begin();
- for( ; iter != keys.end(); ++iter ) {
-
- this->put( *iter, other.get( *iter ) );
- }
- }
-
- /**
- * {@inheritDoc}
- */
- virtual V remove( const K& key ) {
-
- typename std::map<K,V,COMPARATOR>::iterator iter = valueMap.find( key );
- if( iter == valueMap.end() ) {
- throw decaf::util::NoSuchElementException(
- __FILE__, __LINE__, "Key is not present in this Map." );
- }
-
- V result = iter->second;
- valueMap.erase( iter );
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- virtual std::vector<K> keySet() const{
- std::vector<K> keys( valueMap.size() );
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- keys[ix] = iter->first;
- }
+////////////////////////////////////////////////////////////////////////////////
+namespace {
- return keys;
- }
+ const int MAP_SIZE = 1000;
- /**
- * {@inheritDoc}
- */
- virtual std::vector<V> values() const {
- std::vector<V> values( valueMap.size() );
-
- typename std::map<K,V,COMPARATOR>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- values[ix] = iter->second;
+ void populateMap(StlMap<int, std::string>& map) {
+ for (int i = 0; i < MAP_SIZE; ++i) {
+ map.put(i, Integer::toString(i));
}
-
- return values;
- }
-
- virtual Set< MapEntry<K, V> >* entrySet() {
- throw decaf::lang::exceptions::UnsupportedOperationException();
}
-
- virtual Set< MapEntry<K, V> >* entrySet() const {
- throw decaf::lang::exceptions::UnsupportedOperationException();
- }
-
-public:
-
- virtual void lock() {
- mutex.lock();
- }
-
- virtual bool tryLock() {
- return mutex.tryLock();
- }
-
- virtual void unlock() {
- mutex.unlock();
- }
-
- virtual void wait() {
-
- mutex.wait();
- }
-
- virtual void wait( long long millisecs ) {
-
- mutex.wait( millisecs );
- }
-
- virtual void wait( long long millisecs, int nanos ) {
-
- mutex.wait( millisecs, nanos );
- }
-
- virtual void notify() {
-
- mutex.notify();
- }
-
- virtual void notifyAll() {
-
- mutex.notifyAll();
- }
-
-};
+}
////////////////////////////////////////////////////////////////////////////////
void StlMapTest::testConstructor() {
@@ -303,7 +53,7 @@ void StlMapTest::testConstructor() {
map1.get( "TEST" ),
decaf::util::NoSuchElementException );
- StlTestMap<string, int> srcMap;
+ HashMap<string, int> srcMap;
srcMap.put( "A", 1 );
srcMap.put( "B", 1 );
srcMap.put( "C", 1 );
@@ -339,7 +89,6 @@ void StlMapTest::testContiansValue() {
CPPUNIT_ASSERT( boolMap.containsValue(true) == false );
}
-
////////////////////////////////////////////////////////////////////////////////
void StlMapTest::testClear() {
@@ -356,7 +105,7 @@ void StlMapTest::testClear() {
void StlMapTest::testCopy() {
StlMap<string, int> destMap;
- StlTestMap<string, int> srcMap;
+ HashMap<string, int> srcMap;
StlMap<string, int> srcMap2;
CPPUNIT_ASSERT( destMap.size() == 0 );
@@ -454,8 +203,8 @@ void StlMapTest::testPut() {
void StlMapTest::testPutAll() {
StlMap<string, int> destMap;
- StlTestMap<string, int> srcMap;
- StlTestMap<string, int> srcMap2;
+ HashMap<string, int> srcMap;
+ HashMap<string, int> srcMap2;
srcMap.put( "A", 1 );
srcMap.put( "B", 1 );
@@ -489,3 +238,113 @@ void StlMapTest::testRemove() {
decaf::util::NoSuchElementException );
}
+////////////////////////////////////////////////////////////////////////////////
+void StlMapTest::testEntrySet() {
+
+ StlMap<int, std::string> map;
+
+ for (int i = 0; i < 50; i++) {
+ map.put(i, Integer::toString(i));
+ }
+
+ Set<MapEntry<int, std::string> >& set = map.entrySet();
+ Pointer< Iterator<MapEntry<int, std::string> > > iterator(set.iterator());
+
+ CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size", map.size() == set.size());
+ while (iterator->hasNext()) {
+ MapEntry<int, std::string> entry = iterator->next();
+ CPPUNIT_ASSERT_MESSAGE("Returned incorrect entry set",
+ map.containsKey(entry.getKey()) && map.containsValue(entry.getValue()));
+ }
+
+ iterator.reset(set.iterator());
+ set.remove(iterator->next());
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove on set didn't take", 49, set.size());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void StlMapTest::testKeySet() {
+
+ StlMap<int, std::string> map;
+ populateMap(map);
+ Set<int>& set = map.keySet();
+ CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size()", set.size() == map.size());
+ for (int i = 0; i < MAP_SIZE; i++) {
+ CPPUNIT_ASSERT_MESSAGE("Returned set does not contain all keys", set.contains(i));
+ }
+
+ {
+ StlMap<int, std::string> localMap;
+ localMap.put(0, "test");
+ Set<int>& intSet = localMap.keySet();
+ CPPUNIT_ASSERT_MESSAGE("Failed with zero key", intSet.contains(0));
+ }
+ {
+ StlMap<int, std::string> localMap;
+ localMap.put(1, "1");
+ localMap.put(102, "102");
+ localMap.put(203, "203");
+
+ Set<int>& intSet = localMap.keySet();
+ Pointer< Iterator<int> > it(intSet.iterator());
+ int remove1 = it->next();
+ it->hasNext();
+ it->remove();
+ int remove2 = it->next();
+ it->remove();
+
+ ArrayList<int> list;
+ list.add(1);
+ list.add(102);
+ list.add(203);
+
+ list.remove(remove1);
+ list.remove(remove2);
+
+ CPPUNIT_ASSERT_MESSAGE("Wrong result", it->next() == list.get(0));
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong size", 1, localMap.size());
+ it.reset(intSet.iterator());
+ CPPUNIT_ASSERT_MESSAGE("Wrong contents", it->next() == list.get(0));
+ }
+ {
+ StlMap<int, std::string> map2;
+ map2.put(1, "1");
+ map2.put(4, "4");
+
+ Set<int>& intSet = map2.keySet();
+ Pointer< Iterator<int> > it2(intSet.iterator());
+
+ int remove3 = it2->next();
+ int next;
+
+ if (remove3 == 1) {
+ next = 4;
+ } else {
+ next = 1;
+ }
+ it2->hasNext();
+ it2->remove();
+ CPPUNIT_ASSERT_MESSAGE("Wrong result 2", it2->next() == next);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong size 2", 1, map2.size());
+ it2.reset(intSet.iterator());
+ CPPUNIT_ASSERT_MESSAGE("Wrong contents 2", it2->next() == next);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void StlMapTest::testValues() {
+
+ StlMap<int, std::string> map;
+ populateMap(map);
+
+ Collection<std::string>& c = map.values();
+ CPPUNIT_ASSERT_MESSAGE("Returned collection of incorrect size()", c.size() == map.size());
+ for (int i = 0; i < MAP_SIZE; i++) {
+ CPPUNIT_ASSERT_MESSAGE("Returned collection does not contain all keys",
+ c.contains(Integer::toString(i)));
+ }
+
+ c.remove("10");
+ CPPUNIT_ASSERT_MESSAGE("Removing from collection should alter Map",
+ !map.containsKey(10));
+}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.h?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/StlMapTest.h Wed Jul 25 22:57:11 2012
@@ -38,6 +38,9 @@ namespace util {
CPPUNIT_TEST( testRemove );
CPPUNIT_TEST( testContiansValue );
CPPUNIT_TEST( testIsEmpty );
+ CPPUNIT_TEST( testEntrySet );
+ CPPUNIT_TEST( testKeySet );
+ CPPUNIT_TEST( testValues );
CPPUNIT_TEST_SUITE_END();
public:
@@ -56,6 +59,9 @@ namespace util {
void testRemove();
void testContiansValue();
void testIsEmpty();
+ void testEntrySet();
+ void testKeySet();
+ void testValues();
};
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp?rev=1365834&r1=1365833&r2=1365834&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp Wed Jul 25 22:57:11 2012
@@ -336,8 +336,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( decaf::
//CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::ArrayListTest );
//#include <decaf/util/ArraysTest.h>
//CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::ArraysTest );
-//#include <decaf/util/StlMapTest.h>
-//CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::StlMapTest );
+#include <decaf/util/StlMapTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::StlMapTest );
//#include <decaf/util/PropertiesTest.h>
//CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::PropertiesTest );
//#include <decaf/util/QueueTest.h>