00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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/ConcurrentModificationException.h>
00028 #include <decaf/util/Map.h>
00029 #include <decaf/util/AbstractCollection.h>
00030 #include <decaf/util/AbstractSet.h>
00031 #include <decaf/util/Iterator.h>
00032 #include <decaf/lang/Pointer.h>
00033
00034 namespace decaf{
00035 namespace util{
00036 namespace concurrent{
00037
00050 template <typename K, typename V, typename COMPARATOR = std::less<K> >
00051 class ConcurrentStlMap : public ConcurrentMap<K, V> {
00052 private:
00053
00054 std::map<K,V,COMPARATOR> valueMap;
00055 mutable concurrent::Mutex mutex;
00056 int modCount;
00057
00058 private:
00059
00060 class AbstractMapIterator {
00061 protected:
00062
00063 mutable int position;
00064 int expectedModCount;
00065 typename std::map<K,V,COMPARATOR>::iterator futureEntry;
00066 typename std::map<K,V,COMPARATOR>::iterator currentEntry;
00067
00068 ConcurrentStlMap* associatedMap;
00069
00070 private:
00071
00072 AbstractMapIterator(const AbstractMapIterator&);
00073 AbstractMapIterator& operator= (const AbstractMapIterator&);
00074
00075 public:
00076
00077 AbstractMapIterator(ConcurrentStlMap* parent) : position(0),
00078 expectedModCount(parent->modCount),
00079 futureEntry(parent->valueMap.begin()),
00080 currentEntry(parent->valueMap.end()),
00081 associatedMap(parent) {
00082 }
00083
00084 virtual ~AbstractMapIterator() {}
00085
00086 virtual bool checkHasNext() const {
00087 synchronized(&this->associatedMap->mutex) {
00088 if (futureEntry != this->associatedMap->valueMap.end()) {
00089 return true;
00090 }
00091 }
00092 return false;
00093 }
00094
00095 void checkConcurrentMod() const {
00096 if (expectedModCount != this->associatedMap->modCount) {
00097 throw ConcurrentModificationException(
00098 __FILE__, __LINE__, "StlMap modified outside this iterator");
00099 }
00100 }
00101
00102 void makeNext() {
00103 synchronized(&this->associatedMap->mutex) {
00104 checkConcurrentMod();
00105
00106 if (!checkHasNext()) {
00107 throw NoSuchElementException(__FILE__, __LINE__, "No next element");
00108 }
00109
00110 currentEntry = futureEntry;
00111 futureEntry++;
00112 }
00113 }
00114
00115 virtual void doRemove() {
00116 synchronized(&this->associatedMap->mutex) {
00117 checkConcurrentMod();
00118
00119 if (currentEntry == this->associatedMap->valueMap.end()) {
00120 throw decaf::lang::exceptions::IllegalStateException(
00121 __FILE__, __LINE__, "Remove called before call to next()");
00122 }
00123
00124 this->associatedMap->valueMap.erase(currentEntry);
00125 currentEntry = this->associatedMap->valueMap.end();
00126
00127 expectedModCount++;
00128 associatedMap->modCount++;
00129 }
00130 }
00131 };
00132
00133 class EntryIterator : public Iterator< MapEntry<K,V> >, public AbstractMapIterator {
00134 private:
00135
00136 EntryIterator(const EntryIterator&);
00137 EntryIterator& operator= (const EntryIterator&);
00138
00139 public:
00140
00141 EntryIterator(ConcurrentStlMap* parent) : AbstractMapIterator(parent) {
00142 }
00143
00144 virtual ~EntryIterator() {}
00145
00146 virtual bool hasNext() const {
00147 return this->checkHasNext();
00148 }
00149
00150 virtual MapEntry<K, V> next() {
00151 synchronized(&this->associatedMap->mutex) {
00152 this->makeNext();
00153 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
00154 }
00155
00156 return MapEntry<K, V>();
00157 }
00158
00159 virtual void remove() {
00160 this->doRemove();
00161 }
00162 };
00163
00164 class KeyIterator : public Iterator<K>, public AbstractMapIterator {
00165 private:
00166
00167 KeyIterator(const KeyIterator&);
00168 KeyIterator& operator= (const KeyIterator&);
00169
00170 public:
00171
00172 KeyIterator(ConcurrentStlMap* parent) : AbstractMapIterator(parent) {
00173 }
00174
00175 virtual ~KeyIterator() {}
00176
00177 virtual bool hasNext() const {
00178 return this->checkHasNext();
00179 }
00180
00181 virtual K next() {
00182 synchronized(&this->associatedMap->mutex) {
00183 this->makeNext();
00184 return this->currentEntry->first;
00185 }
00186
00187 return K();
00188 }
00189
00190 virtual void remove() {
00191 this->doRemove();
00192 }
00193 };
00194
00195 class ValueIterator : public Iterator<V>, public AbstractMapIterator {
00196 private:
00197
00198 ValueIterator(const ValueIterator&);
00199 ValueIterator& operator= (const ValueIterator&);
00200
00201 public:
00202
00203 ValueIterator(ConcurrentStlMap* parent) : AbstractMapIterator(parent) {
00204 }
00205
00206 virtual ~ValueIterator() {}
00207
00208 virtual bool hasNext() const {
00209 return this->checkHasNext();
00210 }
00211
00212 virtual V next() {
00213 synchronized(&this->associatedMap->mutex) {
00214 this->makeNext();
00215 return this->currentEntry->second;
00216 }
00217
00218 return V();
00219 }
00220
00221 virtual void remove() {
00222 this->doRemove();
00223 }
00224 };
00225
00226 private:
00227
00228 class ConstAbstractMapIterator {
00229 protected:
00230
00231 mutable int position;
00232 int expectedModCount;
00233 typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
00234 typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
00235
00236 const ConcurrentStlMap* associatedMap;
00237
00238 private:
00239
00240 ConstAbstractMapIterator(const ConstAbstractMapIterator&);
00241 ConstAbstractMapIterator& operator= (const ConstAbstractMapIterator&);
00242
00243 public:
00244
00245 ConstAbstractMapIterator(const ConcurrentStlMap* parent) : position(0),
00246 expectedModCount(parent->modCount),
00247 futureEntry(parent->valueMap.begin()),
00248 currentEntry(parent->valueMap.end()),
00249 associatedMap(parent) {
00250 }
00251
00252 virtual ~ConstAbstractMapIterator() {}
00253
00254 virtual bool checkHasNext() const {
00255 synchronized(&this->associatedMap->mutex) {
00256 if (futureEntry != this->associatedMap->valueMap.end()) {
00257 return true;
00258 }
00259 }
00260 return false;
00261 }
00262
00263 void checkConcurrentMod() const {
00264 synchronized(&this->associatedMap->mutex) {
00265 if (expectedModCount != this->associatedMap->modCount) {
00266 throw ConcurrentModificationException(
00267 __FILE__, __LINE__, "StlMap modified outside this iterator");
00268 }
00269 }
00270 }
00271
00272 void makeNext() {
00273 synchronized(&this->associatedMap->mutex) {
00274 checkConcurrentMod();
00275
00276 if (!checkHasNext()) {
00277 throw NoSuchElementException(__FILE__, __LINE__, "No next element");
00278 }
00279
00280 currentEntry = futureEntry;
00281 futureEntry++;
00282 }
00283 }
00284 };
00285
00286 class ConstEntryIterator : public Iterator< MapEntry<K,V> >, public ConstAbstractMapIterator {
00287 private:
00288
00289 ConstEntryIterator(const ConstEntryIterator&);
00290 ConstEntryIterator& operator= (const ConstEntryIterator&);
00291
00292 public:
00293
00294 ConstEntryIterator(const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
00295 }
00296
00297 virtual ~ConstEntryIterator() {}
00298
00299 virtual bool hasNext() const {
00300 return this->checkHasNext();
00301 }
00302
00303 virtual MapEntry<K, V> next() {
00304 synchronized(&this->associatedMap->mutex) {
00305 this->makeNext();
00306 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
00307 }
00308
00309 return MapEntry<K, V>();
00310 }
00311
00312 virtual void remove() {
00313 throw lang::exceptions::UnsupportedOperationException(
00314 __FILE__, __LINE__, "Cannot write to a const Iterator." );
00315 }
00316 };
00317
00318 class ConstKeyIterator : public Iterator<K>, public ConstAbstractMapIterator {
00319 private:
00320
00321 ConstKeyIterator(const ConstKeyIterator&);
00322 ConstKeyIterator& operator= (const ConstKeyIterator&);
00323
00324 public:
00325
00326 ConstKeyIterator(const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
00327 }
00328
00329 virtual ~ConstKeyIterator() {}
00330
00331 virtual bool hasNext() const {
00332 return this->checkHasNext();
00333 }
00334
00335 virtual K next() {
00336 synchronized(&this->associatedMap->mutex) {
00337 this->makeNext();
00338 return this->currentEntry->first;
00339 }
00340
00341 return K();
00342 }
00343
00344 virtual void remove() {
00345 throw lang::exceptions::UnsupportedOperationException(
00346 __FILE__, __LINE__, "Cannot write to a const Iterator." );
00347 }
00348 };
00349
00350 class ConstValueIterator : public Iterator<V>, public ConstAbstractMapIterator {
00351 private:
00352
00353 ConstValueIterator(const ConstValueIterator&);
00354 ConstValueIterator& operator= (const ConstValueIterator&);
00355
00356 public:
00357
00358 ConstValueIterator(const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
00359 }
00360
00361 virtual ~ConstValueIterator() {}
00362
00363 virtual bool hasNext() const {
00364 return this->checkHasNext();
00365 }
00366
00367 virtual V next() {
00368 synchronized(&this->associatedMap->mutex) {
00369 this->makeNext();
00370 return this->currentEntry->second;
00371 }
00372
00373 return V();
00374 }
00375
00376 virtual void remove() {
00377 throw lang::exceptions::UnsupportedOperationException(
00378 __FILE__, __LINE__, "Cannot write to a const Iterator." );
00379 }
00380 };
00381
00382 private:
00383
00384
00385 class StlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
00386 private:
00387
00388 ConcurrentStlMap* associatedMap;
00389
00390 private:
00391
00392 StlMapEntrySet(const StlMapEntrySet&);
00393 StlMapEntrySet& operator= (const StlMapEntrySet&);
00394
00395 public:
00396
00397 StlMapEntrySet(ConcurrentStlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
00398 }
00399
00400 virtual ~StlMapEntrySet() {}
00401
00402 virtual int size() const {
00403 return associatedMap->size();
00404 }
00405
00406 virtual void clear() {
00407 associatedMap->clear();
00408 }
00409
00410 virtual bool remove(const MapEntry<K,V>& entry) {
00411 synchronized(&this->associatedMap->mutex) {
00412 if (this->associatedMap->containsKey(entry.getKey()) &&
00413 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
00414 associatedMap->remove(entry.getKey());
00415 return true;
00416 }
00417 }
00418
00419 return false;
00420 }
00421
00422 virtual bool contains(const MapEntry<K,V>& entry) {
00423 synchronized(&this->associatedMap->mutex) {
00424 if (this->associatedMap->containsKey(entry.getKey()) &&
00425 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
00426 return true;
00427 }
00428 }
00429 return false;
00430 }
00431
00432 virtual Iterator< MapEntry<K, V> >* iterator() {
00433 return new EntryIterator(associatedMap);
00434 }
00435
00436 virtual Iterator< MapEntry<K, V> >* iterator() const {
00437 return new ConstEntryIterator(associatedMap);
00438 }
00439 };
00440
00441
00442 class ConstStlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
00443 private:
00444
00445 const ConcurrentStlMap* associatedMap;
00446
00447 private:
00448
00449 ConstStlMapEntrySet(const ConstStlMapEntrySet&);
00450 ConstStlMapEntrySet& operator= (const ConstStlMapEntrySet&);
00451
00452 public:
00453
00454 ConstStlMapEntrySet(const ConcurrentStlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
00455 }
00456
00457 virtual ~ConstStlMapEntrySet() {}
00458
00459 virtual int size() const {
00460 return associatedMap->size();
00461 }
00462
00463 virtual void clear() {
00464 throw decaf::lang::exceptions::UnsupportedOperationException(
00465 __FILE__, __LINE__, "Can't clear a const collection");
00466 }
00467
00468 virtual bool remove(const MapEntry<K,V>& entry DECAF_UNUSED) {
00469 throw decaf::lang::exceptions::UnsupportedOperationException(
00470 __FILE__, __LINE__, "Can't remove from const collection");
00471 }
00472
00473 virtual bool contains(const MapEntry<K,V>& entry) {
00474 synchronized(&this->associatedMap->mutex) {
00475 if (this->associatedMap->containsKey(entry.getKey()) &&
00476 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
00477 return true;
00478 }
00479 }
00480 return false;
00481 }
00482
00483 virtual Iterator< MapEntry<K, V> >* iterator() {
00484 throw decaf::lang::exceptions::UnsupportedOperationException(
00485 __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
00486 }
00487
00488 virtual Iterator< MapEntry<K, V> >* iterator() const {
00489 return new ConstEntryIterator(associatedMap);
00490 }
00491 };
00492
00493 private:
00494
00495 class StlMapKeySet : public AbstractSet<K> {
00496 private:
00497
00498 ConcurrentStlMap* associatedMap;
00499
00500 private:
00501
00502 StlMapKeySet(const StlMapKeySet&);
00503 StlMapKeySet& operator= (const StlMapKeySet&);
00504
00505 public:
00506
00507 StlMapKeySet(ConcurrentStlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
00508 }
00509
00510 virtual ~StlMapKeySet() {}
00511
00512 virtual bool contains(const K& key) const {
00513 return this->associatedMap->containsKey(key);
00514 }
00515
00516 virtual int size() const {
00517 return this->associatedMap->size();
00518 }
00519
00520 virtual void clear() {
00521 this->associatedMap->clear();
00522 }
00523
00524 virtual bool remove(const K& key) {
00525 synchronized(&this->associatedMap->mutex) {
00526 if (this->associatedMap->containsKey(key)) {
00527 associatedMap->remove(key);
00528 return true;
00529 }
00530 }
00531 return false;
00532 }
00533
00534 virtual Iterator<K>* iterator() {
00535 return new KeyIterator(this->associatedMap);
00536 }
00537
00538 virtual Iterator<K>* iterator() const {
00539 return new ConstKeyIterator(this->associatedMap);
00540 }
00541 };
00542
00543 class ConstStlMapKeySet : public AbstractSet<K> {
00544 private:
00545
00546 const ConcurrentStlMap* associatedMap;
00547
00548 private:
00549
00550 ConstStlMapKeySet(const ConstStlMapKeySet&);
00551 ConstStlMapKeySet& operator= (const ConstStlMapKeySet&);
00552
00553 public:
00554
00555 ConstStlMapKeySet(const ConcurrentStlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
00556 }
00557
00558 virtual ~ConstStlMapKeySet() {}
00559
00560 virtual bool contains(const K& key) const {
00561 return this->associatedMap->containsKey(key);
00562 }
00563
00564 virtual int size() const {
00565 return this->associatedMap->size();
00566 }
00567
00568 virtual void clear() {
00569 throw decaf::lang::exceptions::UnsupportedOperationException(
00570 __FILE__, __LINE__, "Can't modify a const collection");
00571 }
00572
00573 virtual bool remove(const K& key DECAF_UNUSED) {
00574 throw decaf::lang::exceptions::UnsupportedOperationException(
00575 __FILE__, __LINE__, "Can't modify a const collection");
00576 }
00577
00578 virtual Iterator<K>* iterator() {
00579 throw decaf::lang::exceptions::UnsupportedOperationException(
00580 __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
00581 }
00582
00583 virtual Iterator<K>* iterator() const {
00584 return new ConstKeyIterator(this->associatedMap);
00585 }
00586 };
00587
00588 private:
00589
00590 class StlMapValueCollection : public AbstractCollection<V> {
00591 private:
00592
00593 ConcurrentStlMap* associatedMap;
00594
00595 private:
00596
00597 StlMapValueCollection(const StlMapValueCollection&);
00598 StlMapValueCollection& operator= (const StlMapValueCollection&);
00599
00600 public:
00601
00602 StlMapValueCollection(ConcurrentStlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
00603 }
00604
00605 virtual ~StlMapValueCollection() {}
00606
00607 virtual bool contains(const V& value) const {
00608 return this->associatedMap->containsValue(value);
00609 }
00610
00611 virtual int size() const {
00612 return this->associatedMap->size();
00613 }
00614
00615 virtual void clear() {
00616 this->associatedMap->clear();
00617 }
00618
00619 virtual Iterator<V>* iterator() {
00620 return new ValueIterator(this->associatedMap);
00621 }
00622
00623 virtual Iterator<V>* iterator() const {
00624 return new ConstValueIterator(this->associatedMap);
00625 }
00626 };
00627
00628 class ConstStlMapValueCollection : public AbstractCollection<V> {
00629 private:
00630
00631 const ConcurrentStlMap* associatedMap;
00632
00633 private:
00634
00635 ConstStlMapValueCollection(const ConstStlMapValueCollection&);
00636 ConstStlMapValueCollection& operator= (const ConstStlMapValueCollection&);
00637
00638 public:
00639
00640 ConstStlMapValueCollection(const ConcurrentStlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
00641 }
00642
00643 virtual ~ConstStlMapValueCollection() {}
00644
00645 virtual bool contains(const V& value) const {
00646 return this->associatedMap->containsValue(value);
00647 }
00648
00649 virtual int size() const {
00650 return this->associatedMap->size();
00651 }
00652
00653 virtual void clear() {
00654 throw decaf::lang::exceptions::UnsupportedOperationException(
00655 __FILE__, __LINE__, "Can't modify a const collection");
00656 }
00657
00658 virtual Iterator<V>* iterator() {
00659 throw decaf::lang::exceptions::UnsupportedOperationException(
00660 __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
00661 }
00662
00663 virtual Iterator<V>* iterator() const {
00664 return new ConstValueIterator(this->associatedMap);
00665 }
00666 };
00667
00668 private:
00669
00670
00671 decaf::lang::Pointer<StlMapEntrySet> cachedEntrySet;
00672 decaf::lang::Pointer<StlMapKeySet> cachedKeySet;
00673 decaf::lang::Pointer<StlMapValueCollection> cachedValueCollection;
00674
00675
00676 mutable decaf::lang::Pointer<ConstStlMapEntrySet> cachedConstEntrySet;
00677 mutable decaf::lang::Pointer<ConstStlMapKeySet> cachedConstKeySet;
00678 mutable decaf::lang::Pointer<ConstStlMapValueCollection> cachedConstValueCollection;
00679
00680 public:
00681
00685 ConcurrentStlMap() : ConcurrentMap<K,V>(), valueMap(), mutex(), modCount(0),
00686 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
00687 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
00688
00689 }
00690
00696 ConcurrentStlMap(const ConcurrentStlMap& source) : ConcurrentMap<K, V>(), valueMap(), mutex(), modCount(0),
00697 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
00698 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
00699 copy(source);
00700 }
00701
00707 ConcurrentStlMap(const Map<K, V>& source) : ConcurrentMap<K, V>(), valueMap(), mutex(), modCount(0),
00708 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
00709 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
00710 copy(source);
00711 }
00712
00713 virtual ~ConcurrentStlMap() {}
00714
00718 virtual bool equals(const ConcurrentStlMap& source) const {
00719 synchronized(&mutex) {
00720 return this->valueMap == source.valueMap;
00721 }
00722
00723 return false;
00724 }
00725
00726 virtual bool equals(const Map<K, V>& source) const {
00727 synchronized(&mutex) {
00728 typename std::auto_ptr< Iterator<K> > iterator(this->keySet().iterator());
00729 while (iterator->hasNext()) {
00730 K key = iterator->next();
00731 if (!this->containsKey(key)) {
00732 return false;
00733 }
00734
00735 if (!(this->get(key) == source.get(key))) {
00736 return false;
00737 }
00738 }
00739 }
00740
00741 return true;
00742 }
00743
00747 virtual void copy(const ConcurrentStlMap& source) {
00748 synchronized(&mutex) {
00749 this->valueMap.clear();
00750 this->valueMap.insert(source.valueMap.begin(), source.valueMap.end());
00751 }
00752 }
00753
00754 virtual void copy(const Map<K, V>& source) {
00755 synchronized( &mutex ) {
00756 this->clear();
00757 this->putAll(source);
00758 }
00759 }
00760
00764 virtual void clear() {
00765 synchronized(&mutex) {
00766 valueMap.clear();
00767 }
00768 }
00769
00773 virtual bool containsKey(const K& key) const {
00774 typename std::map<K, V, COMPARATOR>::const_iterator iter;
00775
00776 synchronized(&mutex) {
00777 iter = valueMap.find(key);
00778 return iter != valueMap.end();
00779 }
00780
00781 return false;
00782 }
00783
00787 virtual bool containsValue(const V& value) const {
00788 synchronized(&mutex) {
00789 if (valueMap.empty()) {
00790 return false;
00791 }
00792
00793 typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
00794 for (; iter != valueMap.end(); ++iter) {
00795 if ((*iter).second == value) {
00796 return true;
00797 }
00798 }
00799 }
00800
00801 return false;
00802 }
00803
00807 virtual bool isEmpty() const {
00808 synchronized(&mutex) {
00809 return valueMap.empty();
00810 }
00811
00812 return true;
00813 }
00814
00818 virtual int size() const {
00819 synchronized(&mutex) {
00820 return (int)valueMap.size();
00821 }
00822
00823 return 0;
00824 }
00825
00829 virtual V& get(const K& key) {
00830 typename std::map<K,V,COMPARATOR>::iterator iter;
00831 synchronized(&mutex) {
00832 iter = valueMap.find(key);
00833 if (iter != valueMap.end()) {
00834 return iter->second;
00835 }
00836 }
00837
00838 throw NoSuchElementException(
00839 __FILE__, __LINE__, "Key does not exist in map");
00840 }
00841
00845 virtual const V& get(const K& key) const {
00846 typename std::map<K,V,COMPARATOR>::const_iterator iter;
00847 synchronized(&mutex) {
00848 iter = valueMap.find(key);
00849 if (iter != valueMap.end()) {
00850 return iter->second;
00851 }
00852 }
00853
00854 throw NoSuchElementException(
00855 __FILE__, __LINE__, "Key does not exist in map");
00856 }
00857
00861 virtual bool put(const K& key, const V& value) {
00862 bool result = false;
00863 synchronized(&mutex) {
00864 if (this->containsKey(key)) {
00865 result = true;
00866 }
00867 modCount++;
00868 valueMap[key] = value;
00869 }
00870 return result;
00871 }
00872
00876 virtual bool put(const K& key, const V& value, V& oldValue) {
00877 bool result = false;
00878 synchronized(&mutex) {
00879 if (this->containsKey(key)) {
00880 result = true;
00881 oldValue = valueMap[key];
00882 }
00883 modCount++;
00884 valueMap[key] = value;
00885 }
00886 return result;
00887 }
00888
00892 virtual void putAll(const ConcurrentStlMap<K, V, COMPARATOR>& other) {
00893 synchronized(&mutex) {
00894 this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
00895 this->modCount++;
00896 }
00897 }
00898
00902 virtual void putAll(const Map<K, V>& other) {
00903 synchronized(&mutex) {
00904 typename std::auto_ptr< Iterator<K> > iterator(other.keySet().iterator());
00905 while (iterator->hasNext()) {
00906 K key = iterator->next();
00907 this->put(key, other.get(key));
00908 }
00909 modCount++;
00910 }
00911 }
00912
00916 virtual V remove(const K& key) {
00917 V result = V();
00918 synchronized(&mutex) {
00919 typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
00920 if (iter == valueMap.end()) {
00921 return result;
00922 }
00923 result = iter->second;
00924 valueMap.erase(iter);
00925 modCount++;
00926 }
00927
00928 return result;
00929 }
00930
00954 bool putIfAbsent(const K& key, const V& value) {
00955 synchronized(&mutex) {
00956 if (!this->containsKey(key)) {
00957 this->put(key, value);
00958 return true;
00959 }
00960 }
00961
00962 return false;
00963 }
00964
00983 bool remove(const K& key, const V& value) {
00984 synchronized(&mutex) {
00985 if( this->containsKey( key ) && ( this->get( key ) == value ) ) {
00986 this->remove(key);
00987 return true;
00988 }
00989 }
00990
00991 return false;
00992 }
00993
01013 bool replace(const K& key, const V& oldValue, const V& newValue) {
01014 synchronized(&mutex) {
01015 if (this->containsKey(key) && (this->get(key) == oldValue)) {
01016 this->put(key, newValue);
01017 return true;
01018 }
01019 }
01020
01021 return false;
01022 }
01023
01044 V replace(const K& key, const V& value) {
01045 synchronized(&mutex) {
01046 if (this->containsKey(key)) {
01047 V result = this->get(key);
01048 this->put(key, value);
01049 return result;
01050 }
01051 }
01052
01053 throw NoSuchElementException(
01054 __FILE__, __LINE__, "Value to Replace was not in the Map." );
01055 }
01056
01057 virtual Set< MapEntry<K, V> >& entrySet() {
01058 synchronized(&mutex) {
01059 if (this->cachedEntrySet == NULL) {
01060 this->cachedEntrySet.reset(new StlMapEntrySet(this));
01061 }
01062 }
01063 return *(this->cachedEntrySet);
01064 }
01065 virtual const Set< MapEntry<K, V> >& entrySet() const {
01066 synchronized(&mutex) {
01067 if (this->cachedConstEntrySet == NULL) {
01068 this->cachedConstEntrySet.reset(new ConstStlMapEntrySet(this));
01069 }
01070 }
01071 return *(this->cachedConstEntrySet);
01072 }
01073
01074 virtual Set<K>& keySet() {
01075 synchronized(&mutex) {
01076 if (this->cachedKeySet == NULL) {
01077 this->cachedKeySet.reset(new StlMapKeySet(this));
01078 }
01079 }
01080 return *(this->cachedKeySet);
01081 }
01082
01083 virtual const Set<K>& keySet() const {
01084 synchronized(&mutex) {
01085 if (this->cachedConstKeySet == NULL) {
01086 this->cachedConstKeySet.reset(new ConstStlMapKeySet(this));
01087 }
01088 }
01089 return *(this->cachedConstKeySet);
01090 }
01091
01092 virtual Collection<V>& values() {
01093 synchronized(&mutex) {
01094 if (this->cachedValueCollection == NULL) {
01095 this->cachedValueCollection.reset(new StlMapValueCollection(this));
01096 }
01097 }
01098 return *(this->cachedValueCollection);
01099 }
01100
01101 virtual const Collection<V>& values() const {
01102 synchronized(&mutex) {
01103 if (this->cachedConstValueCollection == NULL) {
01104 this->cachedConstValueCollection.reset(new ConstStlMapValueCollection(this));
01105 }
01106 }
01107 return *(this->cachedConstValueCollection);
01108 }
01109
01110 public:
01111
01112 virtual void lock() {
01113 mutex.lock();
01114 }
01115
01116 virtual bool tryLock() {
01117 return mutex.tryLock();
01118 }
01119
01120 virtual void unlock() {
01121 mutex.unlock();
01122 }
01123
01124 virtual void wait() {
01125 mutex.wait();
01126 }
01127
01128 virtual void wait( long long millisecs ) {
01129 mutex.wait( millisecs );
01130 }
01131
01132 virtual void wait( long long millisecs, int nanos ) {
01133 mutex.wait( millisecs, nanos );
01134 }
01135
01136 virtual void notify() {
01137 mutex.notify();
01138 }
01139
01140 virtual void notifyAll() {
01141 mutex.notifyAll();
01142 }
01143
01144 };
01145
01146 }}}
01147
01148 #endif