activemq-cpp-3.9.5
ConcurrentStlMap.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _DECAF_UTIL_CONCURRENTSTLMAP_H_
19#define _DECAF_UTIL_CONCURRENTSTLMAP_H_
20
21#include <map>
22#include <vector>
28#include <decaf/util/Map.h>
31#include <decaf/util/Iterator.h>
32#include <decaf/lang/Pointer.h>
33
34namespace decaf {
35namespace util {
36namespace concurrent {
37
50 template <typename K, typename V, typename COMPARATOR = std::less<K> >
51 class ConcurrentStlMap : public ConcurrentMap<K, V> {
52 private:
53
54 std::map<K,V,COMPARATOR> valueMap;
55 mutable concurrent::Mutex mutex;
56 int modCount;
57
58 private:
59
60 class AbstractMapIterator {
61 protected:
62
63 mutable int position;
64 int expectedModCount;
65 typename std::map<K,V,COMPARATOR>::iterator futureEntry;
66 typename std::map<K,V,COMPARATOR>::iterator currentEntry;
67
68 ConcurrentStlMap* associatedMap;
69
70 private:
71
72 AbstractMapIterator(const AbstractMapIterator&);
73 AbstractMapIterator& operator= (const AbstractMapIterator&);
74
75 public:
76
77 AbstractMapIterator(ConcurrentStlMap* parent) : position(0),
78 expectedModCount(parent->modCount),
79 futureEntry(parent->valueMap.begin()),
80 currentEntry(parent->valueMap.end()),
81 associatedMap(parent) {
82 }
83
84 virtual ~AbstractMapIterator() {}
85
86 virtual bool checkHasNext() const {
87 synchronized(&this->associatedMap->mutex) {
88 if (futureEntry != this->associatedMap->valueMap.end()) {
89 return true;
90 }
91 }
92 return false;
93 }
94
95 void checkConcurrentMod() const {
96 if (expectedModCount != this->associatedMap->modCount) {
98 __FILE__, __LINE__, "StlMap modified outside this iterator");
99 }
100 }
101
102 void makeNext() {
103 synchronized(&this->associatedMap->mutex) {
104 checkConcurrentMod();
105
106 if (!checkHasNext()) {
107 throw NoSuchElementException(__FILE__, __LINE__, "No next element");
108 }
109
110 currentEntry = futureEntry;
111 futureEntry++;
112 }
113 }
114
115 virtual void doRemove() {
116 synchronized(&this->associatedMap->mutex) {
117 checkConcurrentMod();
118
119 if (currentEntry == this->associatedMap->valueMap.end()) {
121 __FILE__, __LINE__, "Remove called before call to next()");
122 }
123
124 this->associatedMap->valueMap.erase(currentEntry);
125 currentEntry = this->associatedMap->valueMap.end();
126
127 expectedModCount++;
128 associatedMap->modCount++;
129 }
130 }
131 };
132
133 class EntryIterator : public Iterator< MapEntry<K,V> >, public AbstractMapIterator {
134 private:
135
136 EntryIterator(const EntryIterator&);
137 EntryIterator& operator= (const EntryIterator&);
138
139 public:
140
141 EntryIterator(ConcurrentStlMap* parent) : AbstractMapIterator(parent) {
142 }
143
144 virtual ~EntryIterator() {}
145
146 virtual bool hasNext() const {
147 return this->checkHasNext();
148 }
149
150 virtual MapEntry<K, V> next() {
151 synchronized(&this->associatedMap->mutex) {
152 this->makeNext();
153 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
154 }
155
156 return MapEntry<K, V>();
157 }
158
159 virtual void remove() {
160 this->doRemove();
161 }
162 };
163
164 class KeyIterator : public Iterator<K>, public AbstractMapIterator {
165 private:
166
167 KeyIterator(const KeyIterator&);
168 KeyIterator& operator= (const KeyIterator&);
169
170 public:
171
172 KeyIterator(ConcurrentStlMap* parent) : AbstractMapIterator(parent) {
173 }
174
175 virtual ~KeyIterator() {}
176
177 virtual bool hasNext() const {
178 return this->checkHasNext();
179 }
180
181 virtual K next() {
182 synchronized(&this->associatedMap->mutex) {
183 this->makeNext();
184 return this->currentEntry->first;
185 }
186
187 return K();
188 }
189
190 virtual void remove() {
191 this->doRemove();
192 }
193 };
194
195 class ValueIterator : public Iterator<V>, public AbstractMapIterator {
196 private:
197
198 ValueIterator(const ValueIterator&);
199 ValueIterator& operator= (const ValueIterator&);
200
201 public:
202
203 ValueIterator(ConcurrentStlMap* parent) : AbstractMapIterator(parent) {
204 }
205
206 virtual ~ValueIterator() {}
207
208 virtual bool hasNext() const {
209 return this->checkHasNext();
210 }
211
212 virtual V next() {
213 synchronized(&this->associatedMap->mutex) {
214 this->makeNext();
215 return this->currentEntry->second;
216 }
217
218 return V();
219 }
220
221 virtual void remove() {
222 this->doRemove();
223 }
224 };
225
226 private:
227
228 class ConstAbstractMapIterator {
229 protected:
230
231 mutable int position;
232 int expectedModCount;
233 typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
234 typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
235
236 const ConcurrentStlMap* associatedMap;
237
238 private:
239
240 ConstAbstractMapIterator(const ConstAbstractMapIterator&);
241 ConstAbstractMapIterator& operator= (const ConstAbstractMapIterator&);
242
243 public:
244
245 ConstAbstractMapIterator(const ConcurrentStlMap* parent) : position(0),
246 expectedModCount(parent->modCount),
247 futureEntry(parent->valueMap.begin()),
248 currentEntry(parent->valueMap.end()),
249 associatedMap(parent) {
250 }
251
252 virtual ~ConstAbstractMapIterator() {}
253
254 virtual bool checkHasNext() const {
255 synchronized(&this->associatedMap->mutex) {
256 if (futureEntry != this->associatedMap->valueMap.end()) {
257 return true;
258 }
259 }
260 return false;
261 }
262
263 void checkConcurrentMod() const {
264 synchronized(&this->associatedMap->mutex) {
265 if (expectedModCount != this->associatedMap->modCount) {
267 __FILE__, __LINE__, "StlMap modified outside this iterator");
268 }
269 }
270 }
271
272 void makeNext() {
273 synchronized(&this->associatedMap->mutex) {
274 checkConcurrentMod();
275
276 if (!checkHasNext()) {
277 throw NoSuchElementException(__FILE__, __LINE__, "No next element");
278 }
279
280 currentEntry = futureEntry;
281 futureEntry++;
282 }
283 }
284 };
285
286 class ConstEntryIterator : public Iterator< MapEntry<K,V> >, public ConstAbstractMapIterator {
287 private:
288
289 ConstEntryIterator(const ConstEntryIterator&);
290 ConstEntryIterator& operator= (const ConstEntryIterator&);
291
292 public:
293
294 ConstEntryIterator(const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
295 }
296
297 virtual ~ConstEntryIterator() {}
298
299 virtual bool hasNext() const {
300 return this->checkHasNext();
301 }
302
303 virtual MapEntry<K, V> next() {
304 synchronized(&this->associatedMap->mutex) {
305 this->makeNext();
306 return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
307 }
308
309 return MapEntry<K, V>();
310 }
311
312 virtual void remove() {
313 throw lang::exceptions::UnsupportedOperationException(
314 __FILE__, __LINE__, "Cannot write to a const Iterator." );
315 }
316 };
317
318 class ConstKeyIterator : public Iterator<K>, public ConstAbstractMapIterator {
319 private:
320
321 ConstKeyIterator(const ConstKeyIterator&);
322 ConstKeyIterator& operator= (const ConstKeyIterator&);
323
324 public:
325
326 ConstKeyIterator(const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
327 }
328
329 virtual ~ConstKeyIterator() {}
330
331 virtual bool hasNext() const {
332 return this->checkHasNext();
333 }
334
335 virtual K next() {
336 synchronized(&this->associatedMap->mutex) {
337 this->makeNext();
338 return this->currentEntry->first;
339 }
340
341 return K();
342 }
343
344 virtual void remove() {
345 throw lang::exceptions::UnsupportedOperationException(
346 __FILE__, __LINE__, "Cannot write to a const Iterator." );
347 }
348 };
349
350 class ConstValueIterator : public Iterator<V>, public ConstAbstractMapIterator {
351 private:
352
353 ConstValueIterator(const ConstValueIterator&);
354 ConstValueIterator& operator= (const ConstValueIterator&);
355
356 public:
357
358 ConstValueIterator(const ConcurrentStlMap* parent) : ConstAbstractMapIterator(parent) {
359 }
360
361 virtual ~ConstValueIterator() {}
362
363 virtual bool hasNext() const {
364 return this->checkHasNext();
365 }
366
367 virtual V next() {
368 synchronized(&this->associatedMap->mutex) {
369 this->makeNext();
370 return this->currentEntry->second;
371 }
372
373 return V();
374 }
375
376 virtual void remove() {
377 throw lang::exceptions::UnsupportedOperationException(
378 __FILE__, __LINE__, "Cannot write to a const Iterator." );
379 }
380 };
381
382 private:
383
384 // Special Set implementation that is backed by this HashMap
385 class StlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
386 private:
387
388 ConcurrentStlMap* associatedMap;
389
390 private:
391
392 StlMapEntrySet(const StlMapEntrySet&);
393 StlMapEntrySet& operator= (const StlMapEntrySet&);
394
395 public:
396
397 StlMapEntrySet(ConcurrentStlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
398 }
399
400 virtual ~StlMapEntrySet() {}
401
402 virtual int size() const {
403 return associatedMap->size();
404 }
405
406 virtual void clear() {
407 associatedMap->clear();
408 }
409
410 virtual bool remove(const MapEntry<K,V>& entry) {
411 synchronized(&this->associatedMap->mutex) {
412 if (this->associatedMap->containsKey(entry.getKey()) &&
413 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
414 associatedMap->remove(entry.getKey());
415 return true;
416 }
417 }
418
419 return false;
420 }
421
422 virtual bool contains(const MapEntry<K,V>& entry) const {
423 synchronized(&this->associatedMap->mutex) {
424 if (this->associatedMap->containsKey(entry.getKey()) &&
425 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
426 return true;
427 }
428 }
429 return false;
430 }
431
432 virtual Iterator< MapEntry<K, V> >* iterator() {
433 return new EntryIterator(associatedMap);
434 }
435
436 virtual Iterator< MapEntry<K, V> >* iterator() const {
437 return new ConstEntryIterator(associatedMap);
438 }
439 };
440
441 // Special Set implementation that is backed by this HashMap
442 class ConstStlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
443 private:
444
445 const ConcurrentStlMap* associatedMap;
446
447 private:
448
449 ConstStlMapEntrySet(const ConstStlMapEntrySet&);
450 ConstStlMapEntrySet& operator= (const ConstStlMapEntrySet&);
451
452 public:
453
454 ConstStlMapEntrySet(const ConcurrentStlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
455 }
456
457 virtual ~ConstStlMapEntrySet() {}
458
459 virtual int size() const {
460 return associatedMap->size();
461 }
462
463 virtual void clear() {
465 __FILE__, __LINE__, "Can't clear a const collection");
466 }
467
468 virtual bool remove(const MapEntry<K,V>& entry DECAF_UNUSED) {
470 __FILE__, __LINE__, "Can't remove from const collection");
471 }
472
473 virtual bool contains(const MapEntry<K,V>& entry) const {
474 synchronized(&this->associatedMap->mutex) {
475 if (this->associatedMap->containsKey(entry.getKey()) &&
476 this->associatedMap->get(entry.getKey()) == entry.getValue()) {
477 return true;
478 }
479 }
480 return false;
481 }
482
483 virtual Iterator< MapEntry<K, V> >* iterator() {
485 __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
486 }
487
488 virtual Iterator< MapEntry<K, V> >* iterator() const {
489 return new ConstEntryIterator(associatedMap);
490 }
491 };
492
493 private:
494
495 class StlMapKeySet : public AbstractSet<K> {
496 private:
497
498 ConcurrentStlMap* associatedMap;
499
500 private:
501
502 StlMapKeySet(const StlMapKeySet&);
503 StlMapKeySet& operator= (const StlMapKeySet&);
504
505 public:
506
507 StlMapKeySet(ConcurrentStlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
508 }
509
510 virtual ~StlMapKeySet() {}
511
512 virtual bool contains(const K& key) const {
513 return this->associatedMap->containsKey(key);
514 }
515
516 virtual int size() const {
517 return this->associatedMap->size();
518 }
519
520 virtual void clear() {
521 this->associatedMap->clear();
522 }
523
524 virtual bool remove(const K& key) {
525 synchronized(&this->associatedMap->mutex) {
526 if (this->associatedMap->containsKey(key)) {
527 associatedMap->remove(key);
528 return true;
529 }
530 }
531 return false;
532 }
533
534 virtual Iterator<K>* iterator() {
535 return new KeyIterator(this->associatedMap);
536 }
537
538 virtual Iterator<K>* iterator() const {
539 return new ConstKeyIterator(this->associatedMap);
540 }
541 };
542
543 class ConstStlMapKeySet : public AbstractSet<K> {
544 private:
545
546 const ConcurrentStlMap* associatedMap;
547
548 private:
549
550 ConstStlMapKeySet(const ConstStlMapKeySet&);
551 ConstStlMapKeySet& operator= (const ConstStlMapKeySet&);
552
553 public:
554
555 ConstStlMapKeySet(const ConcurrentStlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
556 }
557
558 virtual ~ConstStlMapKeySet() {}
559
560 virtual bool contains(const K& key) const {
561 return this->associatedMap->containsKey(key);
562 }
563
564 virtual int size() const {
565 return this->associatedMap->size();
566 }
567
568 virtual void clear() {
570 __FILE__, __LINE__, "Can't modify a const collection");
571 }
572
573 virtual bool remove(const K& key DECAF_UNUSED) {
575 __FILE__, __LINE__, "Can't modify a const collection");
576 }
577
578 virtual Iterator<K>* iterator() {
580 __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
581 }
582
583 virtual Iterator<K>* iterator() const {
584 return new ConstKeyIterator(this->associatedMap);
585 }
586 };
587
588 private:
589
590 class StlMapValueCollection : public AbstractCollection<V> {
591 private:
592
593 ConcurrentStlMap* associatedMap;
594
595 private:
596
597 StlMapValueCollection(const StlMapValueCollection&);
598 StlMapValueCollection& operator= (const StlMapValueCollection&);
599
600 public:
601
602 StlMapValueCollection(ConcurrentStlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
603 }
604
605 virtual ~StlMapValueCollection() {}
606
607 virtual bool contains(const V& value) const {
608 return this->associatedMap->containsValue(value);
609 }
610
611 virtual int size() const {
612 return this->associatedMap->size();
613 }
614
615 virtual void clear() {
616 this->associatedMap->clear();
617 }
618
619 virtual Iterator<V>* iterator() {
620 return new ValueIterator(this->associatedMap);
621 }
622
623 virtual Iterator<V>* iterator() const {
624 return new ConstValueIterator(this->associatedMap);
625 }
626 };
627
628 class ConstStlMapValueCollection : public AbstractCollection<V> {
629 private:
630
631 const ConcurrentStlMap* associatedMap;
632
633 private:
634
635 ConstStlMapValueCollection(const ConstStlMapValueCollection&);
636 ConstStlMapValueCollection& operator= (const ConstStlMapValueCollection&);
637
638 public:
639
640 ConstStlMapValueCollection(const ConcurrentStlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
641 }
642
643 virtual ~ConstStlMapValueCollection() {}
644
645 virtual bool contains(const V& value) const {
646 return this->associatedMap->containsValue(value);
647 }
648
649 virtual int size() const {
650 return this->associatedMap->size();
651 }
652
653 virtual void clear() {
655 __FILE__, __LINE__, "Can't modify a const collection");
656 }
657
658 virtual Iterator<V>* iterator() {
660 __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
661 }
662
663 virtual Iterator<V>* iterator() const {
664 return new ConstValueIterator(this->associatedMap);
665 }
666 };
667
668 private:
669
670 // Cached values that are only initialized once a request for them is made.
674
675 // Cached values that are only initialized once a request for them is made.
676 mutable decaf::lang::Pointer<ConstStlMapEntrySet> cachedConstEntrySet;
677 mutable decaf::lang::Pointer<ConstStlMapKeySet> cachedConstKeySet;
678 mutable decaf::lang::Pointer<ConstStlMapValueCollection> cachedConstValueCollection;
679
680 public:
681
685 ConcurrentStlMap() : ConcurrentMap<K,V>(), valueMap(), mutex(), modCount(0),
686 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
687 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
688
689 }
690
696 ConcurrentStlMap(const ConcurrentStlMap& source) : ConcurrentMap<K, V>(), valueMap(), mutex(), modCount(0),
697 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
698 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
699 copy(source);
700 }
701
707 ConcurrentStlMap(const Map<K, V>& source) : ConcurrentMap<K, V>(), valueMap(), mutex(), modCount(0),
708 cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
709 cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
710 copy(source);
711 }
712
713 virtual ~ConcurrentStlMap() {}
714
718 virtual bool equals(const ConcurrentStlMap& source) const {
719 synchronized(&mutex) {
720 return this->valueMap == source.valueMap;
721 }
722
723 return false;
724 }
725
726 virtual bool equals(const Map<K, V>& source) const {
727 synchronized(&mutex) {
728 typename std::auto_ptr< Iterator<K> > iterator(this->keySet().iterator());
729 while (iterator->hasNext()) {
730 K key = iterator->next();
731 if (!this->containsKey(key)) {
732 return false;
733 }
734
735 if (!(this->get(key) == source.get(key))) {
736 return false;
737 }
738 }
739 }
740
741 return true;
742 }
743
747 virtual void copy(const ConcurrentStlMap& source) {
748 synchronized(&mutex) {
749 this->valueMap.clear();
750 this->valueMap.insert(source.valueMap.begin(), source.valueMap.end());
751 }
752 }
753
754 virtual void copy(const Map<K, V>& source) {
755 synchronized( &mutex ) {
756 this->clear();
757 this->putAll(source);
758 }
759 }
760
764 virtual void clear() {
765 synchronized(&mutex) {
766 valueMap.clear();
767 }
768 }
769
773 virtual bool containsKey(const K& key) const {
774 typename std::map<K, V, COMPARATOR>::const_iterator iter;
775
776 synchronized(&mutex) {
777 if (!valueMap.empty()) {
778 iter = valueMap.find(key);
779 return iter != valueMap.end();
780 }
781 }
782
783 return false;
784 }
785
789 virtual bool containsValue(const V& value) const {
790 synchronized(&mutex) {
791 if (valueMap.empty()) {
792 return false;
793 }
794
795 typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
796 for (; iter != valueMap.end(); ++iter) {
797 if ((*iter).second == value) {
798 return true;
799 }
800 }
801 }
802
803 return false;
804 }
805
809 virtual bool isEmpty() const {
810 synchronized(&mutex) {
811 return valueMap.empty();
812 }
813
814 return true;
815 }
816
820 virtual int size() const {
821 synchronized(&mutex) {
822 return (int)valueMap.size();
823 }
824
825 return 0;
826 }
827
831 virtual V& get(const K& key) {
832 typename std::map<K,V,COMPARATOR>::iterator iter;
833 synchronized(&mutex) {
834 if (!valueMap.empty()) {
835 iter = valueMap.find(key);
836 if (iter != valueMap.end()) {
837 return iter->second;
838 }
839 }
840 }
841
843 __FILE__, __LINE__, "Key does not exist in map");
844 }
845
849 virtual const V& get(const K& key) const {
850 typename std::map<K,V,COMPARATOR>::const_iterator iter;
851 synchronized(&mutex) {
852 if (!valueMap.empty()) {
853 iter = valueMap.find(key);
854 if (iter != valueMap.end()) {
855 return iter->second;
856 }
857 }
858 }
859
861 __FILE__, __LINE__, "Key does not exist in map");
862 }
863
867 virtual bool put(const K& key, const V& value) {
868 bool result = false;
869 synchronized(&mutex) {
870 if (this->containsKey(key)) {
871 result = true;
872 }
873 modCount++;
874 valueMap[key] = value;
875 }
876 return result;
877 }
878
882 virtual bool put(const K& key, const V& value, V& oldValue) {
883 bool result = false;
884 synchronized(&mutex) {
885 if (this->containsKey(key)) {
886 result = true;
887 oldValue = valueMap[key];
888 }
889 modCount++;
890 valueMap[key] = value;
891 }
892 return result;
893 }
894
898 virtual void putAll(const ConcurrentStlMap<K, V, COMPARATOR>& other) {
899 synchronized(&mutex) {
900 this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
901 this->modCount++;
902 }
903 }
904
908 virtual void putAll(const Map<K, V>& other) {
909 synchronized(&mutex) {
910 typename std::auto_ptr< Iterator<K> > iterator(other.keySet().iterator());
911 while (iterator->hasNext()) {
912 K key = iterator->next();
913 this->put(key, other.get(key));
914 }
915 modCount++;
916 }
917 }
918
922 virtual V remove(const K& key) {
923 V result = V();
924 synchronized(&mutex) {
925 if (!valueMap.empty()) {
926 typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
927 if (iter == valueMap.end()) {
928 return result;
929 }
930 result = iter->second;
931 valueMap.erase(iter);
932 modCount++;
933 }
934 }
935
936 return result;
937 }
938
962 bool putIfAbsent(const K& key, const V& value) {
963 synchronized(&mutex) {
964 if (!this->containsKey(key)) {
965 this->put(key, value);
966 return true;
967 }
968 }
969
970 return false;
971 }
972
991 bool remove(const K& key, const V& value) {
992 synchronized(&mutex) {
993 if( this->containsKey( key ) && ( this->get( key ) == value ) ) {
994 this->remove(key);
995 return true;
996 }
997 }
998
999 return false;
1000 }
1001
1021 bool replace(const K& key, const V& oldValue, const V& newValue) {
1022 synchronized(&mutex) {
1023 if (this->containsKey(key) && (this->get(key) == oldValue)) {
1024 this->put(key, newValue);
1025 return true;
1026 }
1027 }
1028
1029 return false;
1030 }
1031
1052 V replace(const K& key, const V& value) {
1053 synchronized(&mutex) {
1054 if (this->containsKey(key)) {
1055 V result = this->get(key);
1056 this->put(key, value);
1057 return result;
1058 }
1059 }
1060
1062 __FILE__, __LINE__, "Value to Replace was not in the Map." );
1063 }
1064
1066 synchronized(&mutex) {
1067 if (this->cachedEntrySet == NULL) {
1068 this->cachedEntrySet.reset(new StlMapEntrySet(this));
1069 }
1070 }
1071 return *(this->cachedEntrySet);
1072 }
1073 virtual const Set< MapEntry<K, V> >& entrySet() const {
1074 synchronized(&mutex) {
1075 if (this->cachedConstEntrySet == NULL) {
1076 this->cachedConstEntrySet.reset(new ConstStlMapEntrySet(this));
1077 }
1078 }
1079 return *(this->cachedConstEntrySet);
1080 }
1081
1082 virtual Set<K>& keySet() {
1083 synchronized(&mutex) {
1084 if (this->cachedKeySet == NULL) {
1085 this->cachedKeySet.reset(new StlMapKeySet(this));
1086 }
1087 }
1088 return *(this->cachedKeySet);
1089 }
1090
1091 virtual const Set<K>& keySet() const {
1092 synchronized(&mutex) {
1093 if (this->cachedConstKeySet == NULL) {
1094 this->cachedConstKeySet.reset(new ConstStlMapKeySet(this));
1095 }
1096 }
1097 return *(this->cachedConstKeySet);
1098 }
1099
1101 synchronized(&mutex) {
1102 if (this->cachedValueCollection == NULL) {
1103 this->cachedValueCollection.reset(new StlMapValueCollection(this));
1104 }
1105 }
1106 return *(this->cachedValueCollection);
1107 }
1108
1109 virtual const Collection<V>& values() const {
1110 synchronized(&mutex) {
1111 if (this->cachedConstValueCollection == NULL) {
1112 this->cachedConstValueCollection.reset(new ConstStlMapValueCollection(this));
1113 }
1114 }
1115 return *(this->cachedConstValueCollection);
1116 }
1117
1118 public:
1119
1120 virtual void lock() {
1121 mutex.lock();
1122 }
1123
1124 virtual bool tryLock() {
1125 return mutex.tryLock();
1126 }
1127
1128 virtual void unlock() {
1129 mutex.unlock();
1130 }
1131
1132 virtual void wait() {
1133 mutex.wait();
1134 }
1135
1136 virtual void wait( long long millisecs ) {
1137 mutex.wait( millisecs );
1138 }
1139
1140 virtual void wait( long long millisecs, int nanos ) {
1141 mutex.wait( millisecs, nanos );
1142 }
1143
1144 virtual void notify() {
1145 mutex.notify();
1146 }
1147
1148 virtual void notifyAll() {
1149 mutex.notifyAll();
1150 }
1151
1152 };
1153
1154}}}
1155
1156#endif /*_DECAF_UTIL_CONCURRENTSTLMAP_H_*/
virtual V remove(const K &key)
Removes the value (key/value pair) for the specified key from the map, returns a copy of the value th...
Definition ConcurrentStlMap.h:922
ConcurrentStlMap()
Default constructor - does nothing.
Definition ConcurrentStlMap.h:685
virtual int size() const
The number of elements (key/value pairs) in this map.
Definition ConcurrentStlMap.h:820
virtual void clear()
Removes all of the mappings from this map (optional operation).The map will be empty after this call ...
Definition ConcurrentStlMap.h:764
virtual decaf::util::Iterator< E > * iterator()=0
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition Pointer.h:53
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition Pointer.h:161
Definition IllegalStateException.h:32
Definition UnsupportedOperationException.h:32
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition AbstractCollection.h:58
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition AbstractSet.h:47
The root interface in the collection hierarchy.
Definition Collection.h:69
Definition ConcurrentModificationException.h:28
Defines an object that can be used to iterate over the elements of a collection.
Definition Iterator.h:34
Definition MapEntry.h:27
virtual K & getKey()
Definition MapEntry.h:57
virtual V & getValue()
Definition MapEntry.h:69
Map()
Default constructor - does nothing.
Definition Map.h:94
virtual V & get(const K &key)=0
Gets the value mapped to the specified key in the Map.
virtual Set< K > & keySet()=0
Returns a Set view of the keys contained in this map.
Definition NoSuchElementException.h:31
A collection that contains no duplicate elements.
Definition Set.h:45
Interface for a Map type that provides additional atomic putIfAbsent, remove, and replace methods alo...
Definition ConcurrentMap.h:39
virtual void copy(const ConcurrentStlMap &source)
Definition ConcurrentStlMap.h:747
virtual void lock()
Locks the object.
Definition ConcurrentStlMap.h:1120
virtual void putAll(const ConcurrentStlMap< K, V, COMPARATOR > &other)
Definition ConcurrentStlMap.h:898
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition ConcurrentStlMap.h:1082
virtual const Collection< V > & values() const
Definition ConcurrentStlMap.h:1109
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition ConcurrentStlMap.h:1144
virtual bool put(const K &key, const V &value, V &oldValue)
Associates the specified value with the specified key in this map (optional operation)....
Definition ConcurrentStlMap.h:882
virtual bool containsValue(const V &value) const
Returns true if this map maps one or more keys to the specified value.More formally,...
Definition ConcurrentStlMap.h:789
virtual Set< MapEntry< K, V > > & entrySet()
Returns a Set view of the mappings contained in this map.
Definition ConcurrentStlMap.h:1065
virtual ~ConcurrentStlMap()
Definition ConcurrentStlMap.h:713
virtual bool equals(const ConcurrentStlMap &source) const
Definition ConcurrentStlMap.h:718
virtual V remove(const K &key)
Removes the value (key/value pair) for the specified key from the map, returns a copy of the value th...
Definition ConcurrentStlMap.h:922
virtual bool isEmpty() const
if the Map contains any element or not, TRUE or FALSE
Definition ConcurrentStlMap.h:809
ConcurrentStlMap(const ConcurrentStlMap &source)
Copy constructor - copies the content of the given map into this one.
Definition ConcurrentStlMap.h:696
bool remove(const K &key, const V &value)
Remove entry for key only if currently mapped to given value.
Definition ConcurrentStlMap.h:991
ConcurrentStlMap()
Default constructor - does nothing.
Definition ConcurrentStlMap.h:685
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition ConcurrentStlMap.h:1132
virtual const Set< K > & keySet() const
Definition ConcurrentStlMap.h:1091
virtual const V & get(const K &key) const
Gets the value mapped to the specified key in the Map.If there is no element in the map whose key is ...
Definition ConcurrentStlMap.h:849
bool putIfAbsent(const K &key, const V &value)
If the specified key is not already associated with a value, associate it with the given value.
Definition ConcurrentStlMap.h:962
ConcurrentStlMap(const Map< K, V > &source)
Copy constructor - copies the content of the given map into this one.
Definition ConcurrentStlMap.h:707
virtual void putAll(const Map< K, V > &other)
Copies all of the mappings from the specified map to this map (optional operation)....
Definition ConcurrentStlMap.h:908
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition ConcurrentStlMap.h:1136
virtual void unlock()
Unlocks the object.
Definition ConcurrentStlMap.h:1128
virtual V & get(const K &key)
Gets the value mapped to the specified key in the Map.If there is no element in the map whose key is ...
Definition ConcurrentStlMap.h:831
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition ConcurrentStlMap.h:1148
virtual const Set< MapEntry< K, V > > & entrySet() const
Definition ConcurrentStlMap.h:1073
virtual bool containsKey(const K &key) const
Returns true if this map contains a mapping for the specified key.More formally, returns true if and ...
Definition ConcurrentStlMap.h:773
virtual bool put(const K &key, const V &value)
Associates the specified value with the specified key in this map (optional operation)....
Definition ConcurrentStlMap.h:867
V replace(const K &key, const V &value)
Replace entry for key only if currently mapped to some value.
Definition ConcurrentStlMap.h:1052
virtual int size() const
The number of elements (key/value pairs) in this map.
Definition ConcurrentStlMap.h:820
virtual void clear()
Removes all of the mappings from this map (optional operation).The map will be empty after this call ...
Definition ConcurrentStlMap.h:764
virtual Collection< V > & values()
Returns a Collection view of the values contained in this map.
Definition ConcurrentStlMap.h:1100
virtual void wait(long long millisecs, int nanos)
Waits on a signal from this object, which is generated by a call to Notify.
Definition ConcurrentStlMap.h:1140
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Definition ConcurrentStlMap.h:1124
virtual void copy(const Map< K, V > &source)
Copies the content of the source map into this map.
Definition ConcurrentStlMap.h:754
bool replace(const K &key, const V &oldValue, const V &newValue)
Replace entry for key only if currently mapped to given value.
Definition ConcurrentStlMap.h:1021
virtual bool equals(const Map< K, V > &source) const
Compares the specified object with this map for equality.
Definition ConcurrentStlMap.h:726
Mutex object that offers recursive support on all platforms as well as providing the ability to use t...
Definition Mutex.h:39
#define NULL
Definition Config.h:33
Definition AbstractExecutorService.h:28
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25