|
activemq-cpp-3.3.0
|
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_STLMAP_H_ 00019 #define _DECAF_UTIL_STLMAP_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/Mutex.h> 00026 #include <decaf/util/Map.h> 00027 00028 namespace decaf{ 00029 namespace util{ 00030 00038 template <typename K, typename V, typename COMPARATOR = std::less<K> > 00039 class StlMap : public Map<K, V, COMPARATOR> { 00040 private: 00041 00042 std::map<K,V,COMPARATOR> valueMap; 00043 mutable concurrent::Mutex mutex; 00044 00045 public: 00046 00050 StlMap() : Map<K,V,COMPARATOR>(), valueMap(), mutex() {} 00051 00057 StlMap( const StlMap& source ) : Map<K,V,COMPARATOR>(), valueMap(), mutex() { 00058 copy( source ); 00059 } 00060 00066 StlMap( const Map<K,V,COMPARATOR>& source ) : Map<K,V,COMPARATOR>(), valueMap(), mutex() { 00067 copy( source ); 00068 } 00069 00070 virtual ~StlMap() {} 00071 00075 virtual bool equals( const StlMap& source ) const { 00076 return this->valueMap == source.valueMap; 00077 } 00078 00082 virtual bool equals( const Map<K,V,COMPARATOR>& source ) const { 00083 std::vector<K> keys = source.keySet(); 00084 00085 typename std::vector<K>::const_iterator iter = keys.begin(); 00086 for( ; iter != keys.end(); ++iter ) { 00087 if( !this->containsKey( *iter ) ) { 00088 return false; 00089 } 00090 00091 if( !( this->get( *iter ) == source.get( *iter ) ) ) { 00092 return false; 00093 } 00094 } 00095 00096 return true; 00097 } 00098 00102 virtual void copy( const StlMap& source ) { 00103 this->valueMap.clear(); 00104 this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() ); 00105 } 00106 00110 virtual void copy( const Map<K,V,COMPARATOR>& source ) { 00111 this->clear(); 00112 this->putAll( source ); 00113 } 00114 00118 virtual void clear() { 00119 valueMap.clear(); 00120 } 00121 00125 virtual bool containsKey( const K& key ) const { 00126 typename std::map<K,V,COMPARATOR>::const_iterator iter; 00127 iter = valueMap.find(key); 00128 return iter != valueMap.end(); 00129 } 00130 00134 virtual bool containsValue( const V& value ) const { 00135 00136 if( valueMap.empty() ){ 00137 return false; 00138 } 00139 00140 typename std::map<K,V,COMPARATOR>::const_iterator iter = valueMap.begin(); 00141 for( ; iter != valueMap.end(); ++iter ){ 00142 if( (*iter).second == value ) { 00143 return true; 00144 } 00145 } 00146 00147 return false; 00148 } 00149 00153 virtual bool isEmpty() const { 00154 return valueMap.empty(); 00155 } 00156 00160 virtual int size() const { 00161 return (int)valueMap.size(); 00162 } 00163 00167 virtual V& get( const K& key ) { 00168 00169 typename std::map<K,V,COMPARATOR>::iterator iter; 00170 iter = valueMap.find( key ); 00171 if( iter == valueMap.end() ){ 00172 throw NoSuchElementException( 00173 __FILE__, __LINE__, "Key does not exist in map" ); 00174 } 00175 00176 return iter->second; 00177 } 00178 00182 virtual const V& get( const K& key ) const { 00183 00184 typename std::map<K,V,COMPARATOR>::const_iterator iter; 00185 iter = valueMap.find( key ); 00186 if( iter == valueMap.end() ){ 00187 throw NoSuchElementException( 00188 __FILE__, __LINE__, "Key does not exist in map" ); 00189 } 00190 00191 return iter->second; 00192 } 00193 00197 virtual void put( const K& key, const V& value ) { 00198 valueMap[key] = value; 00199 } 00200 00204 virtual void putAll( const StlMap<K,V,COMPARATOR>& other ) { 00205 this->valueMap.insert( other.valueMap.begin(), other.valueMap.end() ); 00206 } 00207 00211 virtual void putAll( const Map<K,V,COMPARATOR>& other ) { 00212 00213 std::vector<K> keys = other.keySet(); 00214 00215 typename std::vector<K>::const_iterator iter = keys.begin(); 00216 for( ; iter != keys.end(); ++iter ) { 00217 00218 this->put( *iter, other.get( *iter ) ); 00219 } 00220 } 00221 00225 virtual V remove( const K& key ) { 00226 00227 typename std::map<K,V,COMPARATOR>::iterator iter = valueMap.find( key ); 00228 if( iter == valueMap.end() ) { 00229 throw NoSuchElementException( 00230 __FILE__, __LINE__, "Key is not present in this Map." ); 00231 } 00232 00233 V result = iter->second; 00234 valueMap.erase( iter ); 00235 return result; 00236 } 00237 00241 virtual std::vector<K> keySet() const{ 00242 std::vector<K> keys( valueMap.size() ); 00243 00244 typename std::map<K,V,COMPARATOR>::const_iterator iter; 00245 iter=valueMap.begin(); 00246 for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){ 00247 keys[ix] = iter->first; 00248 } 00249 00250 return keys; 00251 } 00252 00256 virtual std::vector<V> values() const { 00257 std::vector<V> values( valueMap.size() ); 00258 00259 typename std::map<K,V,COMPARATOR>::const_iterator iter; 00260 iter=valueMap.begin(); 00261 for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){ 00262 values[ix] = iter->second; 00263 } 00264 00265 return values; 00266 } 00267 00268 public: 00269 00270 virtual void lock() { 00271 mutex.lock(); 00272 } 00273 00274 virtual bool tryLock() { 00275 return mutex.tryLock(); 00276 } 00277 00278 virtual void unlock() { 00279 mutex.unlock(); 00280 } 00281 00282 virtual void wait() { 00283 mutex.wait(); 00284 } 00285 00286 virtual void wait( long long millisecs ) { 00287 mutex.wait( millisecs ); 00288 } 00289 00290 virtual void wait( long long millisecs, int nanos ) { 00291 mutex.wait( millisecs, nanos ); 00292 } 00293 00294 virtual void notify() { 00295 mutex.notify(); 00296 } 00297 00298 virtual void notifyAll() { 00299 mutex.notifyAll(); 00300 } 00301 00302 }; 00303 00304 }} 00305 00306 #endif /*_DECAF_UTIL_STLMAP_H_*/