activemq-cpp-3.4.0

src/main/decaf/util/concurrent/ConcurrentStlMap.h

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef _DECAF_UTIL_CONCURRENTSTLMAP_H_
00019 #define _DECAF_UTIL_CONCURRENTSTLMAP_H_
00020 
00021 #include <map>
00022 #include <vector>
00023 #include <decaf/util/NoSuchElementException.h>
00024 #include <decaf/util/concurrent/Synchronizable.h>
00025 #include <decaf/util/concurrent/ConcurrentMap.h>
00026 #include <decaf/util/concurrent/Mutex.h>
00027 #include <decaf/util/Map.h>
00028 
00029 namespace decaf{
00030 namespace util{
00031 namespace concurrent{
00032 
00045     template <typename K, typename V, typename COMPARATOR = std::less<K> >
00046     class ConcurrentStlMap : public ConcurrentMap<K, V, COMPARATOR> {
00047     private:
00048 
00049         std::map<K,V,COMPARATOR> valueMap;
00050         mutable concurrent::Mutex mutex;
00051 
00052     public:
00053 
00057         ConcurrentStlMap() : ConcurrentMap<K,V,COMPARATOR>(), valueMap(), mutex() {}
00058 
00064         ConcurrentStlMap( const ConcurrentStlMap& source ) :
00065             ConcurrentMap<K,V,COMPARATOR>(), valueMap(), mutex() {
00066 
00067             copy( source );
00068         }
00069 
00075         ConcurrentStlMap( const Map<K,V,COMPARATOR>& source ) :
00076             ConcurrentMap<K,V,COMPARATOR>(), valueMap(), mutex() {
00077 
00078             copy( source );
00079         }
00080 
00081         virtual ~ConcurrentStlMap() {}
00082 
00086         virtual bool equals( const ConcurrentStlMap& source ) const {
00087             synchronized( &mutex ) {
00088                 return this->valueMap == source.valueMap;
00089             }
00090 
00091             return false;
00092         }
00093 
00094         virtual bool equals( const Map<K,V,COMPARATOR>& source ) const {
00095 
00096             synchronized( &mutex ) {
00097                 std::vector<K> keys = source.keySet();
00098 
00099                 typename std::vector<K>::const_iterator iter = keys.begin();
00100                 for( ; iter != keys.end(); ++iter ) {
00101                     if( !this->containsKey( *iter ) ) {
00102                         return false;
00103                     }
00104 
00105                     if( !( this->get( *iter ) == source.get( *iter ) ) ) {
00106                         return false;
00107                     }
00108                 }
00109             }
00110 
00111             return true;
00112         }
00113 
00117         virtual void copy( const ConcurrentStlMap& source ) {
00118             synchronized( &mutex ) {
00119                 this->valueMap.clear();
00120                 this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
00121             }
00122         }
00123 
00124         virtual void copy( const Map<K,V,COMPARATOR>& source ) {
00125             synchronized( &mutex ) {
00126                 this->clear();
00127                 this->putAll( source );
00128             }
00129         }
00130 
00134         virtual void clear() throw( decaf::lang::exceptions::UnsupportedOperationException ) {
00135             synchronized( &mutex ) {
00136                 valueMap.clear();
00137             }
00138         }
00139 
00143         virtual bool containsKey( const K& key ) const {
00144             typename std::map<K,V,COMPARATOR>::const_iterator iter;
00145 
00146             synchronized( &mutex ) {
00147                 iter = valueMap.find(key);
00148                 return iter != valueMap.end();
00149             }
00150 
00151             return false;
00152         }
00153 
00157         virtual bool containsValue( const V& value ) const {
00158 
00159             synchronized( &mutex ) {
00160 
00161                 if( valueMap.empty() ){
00162                     return false;
00163                 }
00164 
00165                 typename std::map<K,V,COMPARATOR>::const_iterator iter = valueMap.begin();
00166                 for( ; iter != valueMap.end(); ++iter ){
00167                     if( (*iter).second == value ) {
00168                         return true;
00169                     }
00170                 }
00171             }
00172 
00173             return false;
00174         }
00175 
00179         virtual bool isEmpty() const {
00180             synchronized( &mutex ) {
00181                 return valueMap.empty();
00182             }
00183 
00184             return true;
00185         }
00186 
00190         virtual int size() const {
00191             synchronized( &mutex ) {
00192                 return (int)valueMap.size();
00193             }
00194 
00195             return 0;
00196         }
00197 
00201         virtual V& get( const K& key ) {
00202 
00203             typename std::map<K,V,COMPARATOR>::iterator iter;
00204 
00205             synchronized( &mutex ) {
00206                 iter = valueMap.find( key );
00207                 if( iter != valueMap.end() ){
00208                     return iter->second;
00209                 }
00210             }
00211 
00212             throw NoSuchElementException(
00213                 __FILE__, __LINE__, "Key does not exist in map" );
00214         }
00215 
00219         virtual const V& get( const K& key ) const {
00220 
00221             typename std::map<K,V,COMPARATOR>::const_iterator iter;
00222 
00223             synchronized( &mutex ) {
00224                 iter = valueMap.find( key );
00225                 if( iter != valueMap.end() ){
00226                     return iter->second;
00227                 }
00228             }
00229 
00230             throw NoSuchElementException(
00231                 __FILE__, __LINE__, "Key does not exist in map" );
00232         }
00233 
00237         virtual void put( const K& key, const V& value ) {
00238 
00239             synchronized( &mutex ) {
00240                 valueMap[key] = value;
00241             }
00242         }
00243 
00247         virtual void putAll( const ConcurrentStlMap<K,V,COMPARATOR>& other ) {
00248 
00249             synchronized( &mutex ) {
00250                 this->valueMap.insert( other.valueMap.begin(), other.valueMap.end() );
00251             }
00252         }
00253 
00257         virtual void putAll( const Map<K,V,COMPARATOR>& other ) {
00258 
00259             synchronized( &mutex ) {
00260                 std::vector<K> keys = other.keySet();
00261 
00262                 typename std::vector<K>::const_iterator iter = keys.begin();
00263                 for( ; iter != keys.end(); ++iter ) {
00264                     this->put( *iter, other.get( *iter ) );
00265                 }
00266             }
00267         }
00268 
00272         virtual V remove( const K& key ) {
00273 
00274             V result;
00275 
00276             synchronized( &mutex ) {
00277                 typename std::map<K,V,COMPARATOR>::iterator iter = valueMap.find( key );
00278                 if( iter == valueMap.end() ) {
00279                     throw NoSuchElementException(
00280                         __FILE__, __LINE__, "Key is not present in this Map." );
00281                 }
00282 
00283                 result = iter->second;
00284                 valueMap.erase( iter );
00285             }
00286 
00287             return result;
00288         }
00289 
00293         virtual std::vector<K> keySet() const {
00294 
00295             std::vector<K> keys( valueMap.size() );
00296             synchronized( &mutex ) {
00297 
00298                 typename std::map<K,V,COMPARATOR>::const_iterator iter;
00299                 iter=valueMap.begin();
00300                 for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
00301                     keys[ix] = iter->first;
00302                 }
00303             }
00304 
00305             return keys;
00306         }
00307 
00311         virtual std::vector<V> values() const {
00312 
00313             std::vector<V> values( valueMap.size() );
00314             synchronized( &mutex ) {
00315 
00316                 typename std::map<K,V,COMPARATOR>::const_iterator iter;
00317                 iter=valueMap.begin();
00318                 for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
00319                     values[ix] = iter->second;
00320                 }
00321 
00322             }
00323             return values;
00324         }
00325 
00349         bool putIfAbsent( const K& key, const V& value ) {
00350 
00351             synchronized( &mutex ) {
00352                 if( !this->containsKey( key ) ) {
00353                     this->put( key, value );
00354                     return true;
00355                 }
00356             }
00357 
00358             return false;
00359         }
00360 
00379         bool remove( const K& key, const V& value ) {
00380             synchronized( &mutex ) {
00381                 if( this->containsKey( key ) && ( this->get( key ) == value ) ) {
00382                     this->remove( key );
00383                     return true;
00384                 }
00385             }
00386 
00387             return false;
00388         }
00389 
00409         bool replace( const K& key, const V& oldValue, const V& newValue ) {
00410             synchronized( &mutex ) {
00411                 if( this->containsKey( key ) && ( this->get( key ) == oldValue ) ) {
00412                     this->put( key, newValue );
00413                     return true;
00414                 }
00415             }
00416 
00417             return false;
00418         }
00419 
00440         V replace( const K& key, const V& value ) {
00441 
00442             synchronized( &mutex ) {
00443                 if( this->containsKey( key ) ) {
00444                     V result = this->get( key );
00445                     this->put( key, value );
00446                     return result;
00447                 }
00448             }
00449 
00450             throw NoSuchElementException(
00451                 __FILE__, __LINE__, "Value to Replace was not in the Map." );
00452         }
00453 
00454     public:
00455 
00456         virtual void lock() {
00457             mutex.lock();
00458         }
00459 
00460         virtual bool tryLock() {
00461             return mutex.tryLock();
00462         }
00463 
00464         virtual void unlock() {
00465             mutex.unlock();
00466         }
00467 
00468         virtual void wait() {
00469             mutex.wait();
00470         }
00471 
00472         virtual void wait( long long millisecs ) {
00473             mutex.wait( millisecs );
00474         }
00475 
00476         virtual void wait( long long millisecs, int nanos ) {
00477             mutex.wait( millisecs, nanos );
00478         }
00479 
00480         virtual void notify() {
00481             mutex.notify();
00482         }
00483 
00484         virtual void notifyAll() {
00485             mutex.notifyAll();
00486         }
00487 
00488     };
00489 
00490 }}}
00491 
00492 #endif /*_DECAF_UTIL_CONCURRENTSTLMAP_H_*/