activemq-cpp-3.6.0
StlMap.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_STLMAP_H_
19 #define _DECAF_UTIL_STLMAP_H_
20 
21 #include <map>
22 #include <memory>
23 #include <decaf/lang/Pointer.h>
27 #include <decaf/util/AbstractSet.h>
31 #include <decaf/util/Map.h>
32 #include <decaf/util/Collection.h>
33 #include <decaf/util/Set.h>
34 #include <decaf/util/Iterator.h>
35 
36 namespace decaf{
37 namespace util{
38 
46  template <typename K, typename V, typename COMPARATOR = std::less<K> >
47  class StlMap : public Map<K, V> {
48  private:
49 
50  std::map<K,V,COMPARATOR> valueMap;
51  mutable concurrent::Mutex mutex;
52  int modCount;
53 
54  private:
55 
56  class AbstractMapIterator {
57  protected:
58 
59  mutable int position;
60  int expectedModCount;
61  typename std::map<K,V,COMPARATOR>::iterator futureEntry;
62  typename std::map<K,V,COMPARATOR>::iterator currentEntry;
63 
64  StlMap* associatedMap;
65 
66  private:
67 
68  AbstractMapIterator(const AbstractMapIterator&);
69  AbstractMapIterator& operator= (const AbstractMapIterator&);
70 
71  public:
72 
73  AbstractMapIterator(StlMap* parent) : position(0),
74  expectedModCount(parent->modCount),
75  futureEntry(parent->valueMap.begin()),
76  currentEntry(parent->valueMap.end()),
77  associatedMap(parent) {
78  }
79 
80  virtual ~AbstractMapIterator() {}
81 
82  virtual bool checkHasNext() const {
83  if (futureEntry != this->associatedMap->valueMap.end()) {
84  return true;
85  }
86  return false;
87  }
88 
89  void checkConcurrentMod() const {
90  if (expectedModCount != this->associatedMap->modCount) {
92  __FILE__, __LINE__, "StlMap modified outside this iterator");
93  }
94  }
95 
96  void makeNext() {
97  checkConcurrentMod();
98 
99  if (!checkHasNext()) {
100  throw NoSuchElementException(__FILE__, __LINE__, "No next element");
101  }
102 
103  currentEntry = futureEntry;
104  futureEntry++;
105  }
106 
107  virtual void doRemove() {
108 
109  checkConcurrentMod();
110 
111  if (currentEntry == this->associatedMap->valueMap.end()) {
113  __FILE__, __LINE__, "Remove called before call to next()");
114  }
115 
116  this->associatedMap->valueMap.erase(currentEntry);
117  currentEntry = this->associatedMap->valueMap.end();
118 
119  expectedModCount++;
120  associatedMap->modCount++;
121  }
122  };
123 
124  class EntryIterator : public Iterator< MapEntry<K,V> >, public AbstractMapIterator {
125  private:
126 
127  EntryIterator(const EntryIterator&);
128  EntryIterator& operator= (const EntryIterator&);
129 
130  public:
131 
132  EntryIterator(StlMap* parent) : AbstractMapIterator(parent) {
133  }
134 
135  virtual ~EntryIterator() {}
136 
137  virtual bool hasNext() const {
138  return this->checkHasNext();
139  }
140 
141  virtual MapEntry<K, V> next() {
142  this->makeNext();
143  return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
144  }
145 
146  virtual void remove() {
147  this->doRemove();
148  }
149  };
150 
151  class KeyIterator : public Iterator<K>, public AbstractMapIterator {
152  private:
153 
154  KeyIterator(const KeyIterator&);
155  KeyIterator& operator= (const KeyIterator&);
156 
157  public:
158 
159  KeyIterator(StlMap* parent) : AbstractMapIterator(parent) {
160  }
161 
162  virtual ~KeyIterator() {}
163 
164  virtual bool hasNext() const {
165  return this->checkHasNext();
166  }
167 
168  virtual K next() {
169  this->makeNext();
170  return this->currentEntry->first;
171  }
172 
173  virtual void remove() {
174  this->doRemove();
175  }
176  };
177 
178  class ValueIterator : public Iterator<V>, public AbstractMapIterator {
179  private:
180 
181  ValueIterator(const ValueIterator&);
182  ValueIterator& operator= (const ValueIterator&);
183 
184  public:
185 
186  ValueIterator(StlMap* parent) : AbstractMapIterator(parent) {
187  }
188 
189  virtual ~ValueIterator() {}
190 
191  virtual bool hasNext() const {
192  return this->checkHasNext();
193  }
194 
195  virtual V next() {
196  this->makeNext();
197  return this->currentEntry->second;
198  }
199 
200  virtual void remove() {
201  this->doRemove();
202  }
203  };
204 
205  private:
206 
207  class ConstAbstractMapIterator {
208  protected:
209 
210  mutable int position;
211  int expectedModCount;
212  typename std::map<K,V,COMPARATOR>::const_iterator futureEntry;
213  typename std::map<K,V,COMPARATOR>::const_iterator currentEntry;
214 
215  const StlMap* associatedMap;
216 
217  private:
218 
219  ConstAbstractMapIterator(const ConstAbstractMapIterator&);
220  ConstAbstractMapIterator& operator= (const ConstAbstractMapIterator&);
221 
222  public:
223 
224  ConstAbstractMapIterator(const StlMap* parent) : position(0),
225  expectedModCount(parent->modCount),
226  futureEntry(parent->valueMap.begin()),
227  currentEntry(parent->valueMap.end()),
228  associatedMap(parent) {
229  }
230 
231  virtual ~ConstAbstractMapIterator() {}
232 
233  virtual bool checkHasNext() const {
234  if (futureEntry != this->associatedMap->valueMap.end()) {
235  return true;
236  }
237  return false;
238  }
239 
240  void checkConcurrentMod() const {
241  if (expectedModCount != this->associatedMap->modCount) {
243  __FILE__, __LINE__, "StlMap modified outside this iterator");
244  }
245  }
246 
247  void makeNext() {
248  checkConcurrentMod();
249 
250  if (!checkHasNext()) {
251  throw NoSuchElementException(__FILE__, __LINE__, "No next element");
252  }
253 
254  currentEntry = futureEntry;
255  futureEntry++;
256  }
257  };
258 
259  class ConstEntryIterator : public Iterator< MapEntry<K,V> >, public ConstAbstractMapIterator {
260  private:
261 
262  ConstEntryIterator(const ConstEntryIterator&);
263  ConstEntryIterator& operator= (const ConstEntryIterator&);
264 
265  public:
266 
267  ConstEntryIterator(const StlMap* parent) : ConstAbstractMapIterator(parent) {
268  }
269 
270  virtual ~ConstEntryIterator() {}
271 
272  virtual bool hasNext() const {
273  return this->checkHasNext();
274  }
275 
276  virtual MapEntry<K, V> next() {
277  this->makeNext();
278  return MapEntry<K, V>(this->currentEntry->first, this->currentEntry->second);
279  }
280 
281  virtual void remove() {
283  __FILE__, __LINE__, "Cannot write to a const Iterator." );
284  }
285  };
286 
287  class ConstKeyIterator : public Iterator<K>, public ConstAbstractMapIterator {
288  private:
289 
290  ConstKeyIterator(const ConstKeyIterator&);
291  ConstKeyIterator& operator= (const ConstKeyIterator&);
292 
293  public:
294 
295  ConstKeyIterator(const StlMap* parent) : ConstAbstractMapIterator(parent) {
296  }
297 
298  virtual ~ConstKeyIterator() {}
299 
300  virtual bool hasNext() const {
301  return this->checkHasNext();
302  }
303 
304  virtual K next() {
305  this->makeNext();
306  return this->currentEntry->first;
307  }
308 
309  virtual void remove() {
311  __FILE__, __LINE__, "Cannot write to a const Iterator." );
312  }
313  };
314 
315  class ConstValueIterator : public Iterator<V>, public ConstAbstractMapIterator {
316  private:
317 
318  ConstValueIterator(const ConstValueIterator&);
319  ConstValueIterator& operator= (const ConstValueIterator&);
320 
321  public:
322 
323  ConstValueIterator(const StlMap* parent) : ConstAbstractMapIterator(parent) {
324  }
325 
326  virtual ~ConstValueIterator() {}
327 
328  virtual bool hasNext() const {
329  return this->checkHasNext();
330  }
331 
332  virtual V next() {
333  this->makeNext();
334  return this->currentEntry->second;
335  }
336 
337  virtual void remove() {
339  __FILE__, __LINE__, "Cannot write to a const Iterator." );
340  }
341  };
342 
343  private:
344 
345  // Special Set implementation that is backed by this HashMap
346  class StlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
347  private:
348 
349  StlMap* associatedMap;
350 
351  private:
352 
353  StlMapEntrySet(const StlMapEntrySet&);
354  StlMapEntrySet& operator= (const StlMapEntrySet&);
355 
356  public:
357 
358  StlMapEntrySet(StlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
359  }
360 
361  virtual ~StlMapEntrySet() {}
362 
363  virtual int size() const {
364  return associatedMap->size();
365  }
366 
367  virtual void clear() {
368  associatedMap->clear();
369  }
370 
371  virtual bool remove(const MapEntry<K,V>& entry) {
372  if (this->associatedMap->containsKey(entry.getKey()) &&
373  this->associatedMap->get(entry.getKey()) == entry.getValue()) {
374  associatedMap->remove(entry.getKey());
375  return true;
376  }
377 
378  return false;
379  }
380 
381  virtual bool contains(const MapEntry<K,V>& entry) {
382  if (this->associatedMap->containsKey(entry.getKey()) &&
383  this->associatedMap->get(entry.getKey()) == entry.getValue()) {
384  return true;
385  }
386  return false;
387  }
388 
389  virtual Iterator< MapEntry<K, V> >* iterator() {
390  return new EntryIterator(associatedMap);
391  }
392 
393  virtual Iterator< MapEntry<K, V> >* iterator() const {
394  return new ConstEntryIterator(associatedMap);
395  }
396  };
397 
398  // Special Set implementation that is backed by this HashMap
399  class ConstStlMapEntrySet : public AbstractSet< MapEntry<K, V> > {
400  private:
401 
402  const StlMap* associatedMap;
403 
404  private:
405 
406  ConstStlMapEntrySet(const ConstStlMapEntrySet&);
407  ConstStlMapEntrySet& operator= (const ConstStlMapEntrySet&);
408 
409  public:
410 
411  ConstStlMapEntrySet(const StlMap* parent) : AbstractSet< MapEntry<K,V> >(), associatedMap(parent) {
412  }
413 
414  virtual ~ConstStlMapEntrySet() {}
415 
416  virtual int size() const {
417  return associatedMap->size();
418  }
419 
420  virtual void clear() {
422  __FILE__, __LINE__, "Can't clear a const collection");
423  }
424 
425  virtual bool remove(const MapEntry<K,V>& entry DECAF_UNUSED) {
427  __FILE__, __LINE__, "Can't remove from const collection");
428  }
429 
430  virtual bool contains(const MapEntry<K,V>& entry) {
431  if (this->associatedMap->containsKey(entry.getKey()) &&
432  this->associatedMap->get(entry.getKey()) == entry.getValue()) {
433  return true;
434  }
435  return false;
436  }
437 
438  virtual Iterator< MapEntry<K, V> >* iterator() {
440  __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
441  }
442 
443  virtual Iterator< MapEntry<K, V> >* iterator() const {
444  return new ConstEntryIterator(associatedMap);
445  }
446  };
447 
448  private:
449 
450  class StlMapKeySet : public AbstractSet<K> {
451  private:
452 
453  StlMap* associatedMap;
454 
455  private:
456 
457  StlMapKeySet(const StlMapKeySet&);
458  StlMapKeySet& operator= (const StlMapKeySet&);
459 
460  public:
461 
462  StlMapKeySet(StlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
463  }
464 
465  virtual ~StlMapKeySet() {}
466 
467  virtual bool contains(const K& key) const {
468  return this->associatedMap->containsKey(key);
469  }
470 
471  virtual int size() const {
472  return this->associatedMap->size();
473  }
474 
475  virtual void clear() {
476  this->associatedMap->clear();
477  }
478 
479  virtual bool remove(const K& key) {
480  if (this->associatedMap->containsKey(key)) {
481  associatedMap->remove(key);
482  return true;
483  }
484  return false;
485  }
486 
487  virtual Iterator<K>* iterator() {
488  return new KeyIterator(this->associatedMap);
489  }
490 
491  virtual Iterator<K>* iterator() const {
492  return new ConstKeyIterator(this->associatedMap);
493  }
494  };
495 
496  class ConstStlMapKeySet : public AbstractSet<K> {
497  private:
498 
499  const StlMap* associatedMap;
500 
501  private:
502 
503  ConstStlMapKeySet(const ConstStlMapKeySet&);
504  ConstStlMapKeySet& operator= (const ConstStlMapKeySet&);
505 
506  public:
507 
508  ConstStlMapKeySet(const StlMap* parent) : AbstractSet<K>(), associatedMap(parent) {
509  }
510 
511  virtual ~ConstStlMapKeySet() {}
512 
513  virtual bool contains(const K& key) const {
514  return this->associatedMap->containsKey(key);
515  }
516 
517  virtual int size() const {
518  return this->associatedMap->size();
519  }
520 
521  virtual void clear() {
523  __FILE__, __LINE__, "Can't modify a const collection");
524  }
525 
526  virtual bool remove(const K& key DECAF_UNUSED) {
528  __FILE__, __LINE__, "Can't modify a const collection");
529  }
530 
531  virtual Iterator<K>* iterator() {
533  __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
534  }
535 
536  virtual Iterator<K>* iterator() const {
537  return new ConstKeyIterator(this->associatedMap);
538  }
539  };
540 
541  private:
542 
543  class StlMapValueCollection : public AbstractCollection<V> {
544  private:
545 
546  StlMap* associatedMap;
547 
548  private:
549 
550  StlMapValueCollection(const StlMapValueCollection&);
551  StlMapValueCollection& operator= (const StlMapValueCollection&);
552 
553  public:
554 
555  StlMapValueCollection(StlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
556  }
557 
558  virtual ~StlMapValueCollection() {}
559 
560  virtual bool contains(const V& value) const {
561  return this->associatedMap->containsValue(value);
562  }
563 
564  virtual int size() const {
565  return this->associatedMap->size();
566  }
567 
568  virtual void clear() {
569  this->associatedMap->clear();
570  }
571 
572  virtual Iterator<V>* iterator() {
573  return new ValueIterator(this->associatedMap);
574  }
575 
576  virtual Iterator<V>* iterator() const {
577  return new ConstValueIterator(this->associatedMap);
578  }
579  };
580 
581  class ConstStlMapValueCollection : public AbstractCollection<V> {
582  private:
583 
584  const StlMap* associatedMap;
585 
586  private:
587 
588  ConstStlMapValueCollection(const ConstStlMapValueCollection&);
589  ConstStlMapValueCollection& operator= (const ConstStlMapValueCollection&);
590 
591  public:
592 
593  ConstStlMapValueCollection(const StlMap* parent) : AbstractCollection<V>(), associatedMap(parent) {
594  }
595 
596  virtual ~ConstStlMapValueCollection() {}
597 
598  virtual bool contains(const V& value) const {
599  return this->associatedMap->containsValue(value);
600  }
601 
602  virtual int size() const {
603  return this->associatedMap->size();
604  }
605 
606  virtual void clear() {
608  __FILE__, __LINE__, "Can't modify a const collection");
609  }
610 
611  virtual Iterator<V>* iterator() {
613  __FILE__, __LINE__, "Can't return a non-const iterator for a const collection");
614  }
615 
616  virtual Iterator<V>* iterator() const {
617  return new ConstValueIterator(this->associatedMap);
618  }
619  };
620 
621  private:
622 
623  // Cached values that are only initialized once a request for them is made.
626  decaf::lang::Pointer<StlMapValueCollection> cachedValueCollection;
627 
628  // Cached values that are only initialized once a request for them is made.
629  mutable decaf::lang::Pointer<ConstStlMapEntrySet> cachedConstEntrySet;
630  mutable decaf::lang::Pointer<ConstStlMapKeySet> cachedConstKeySet;
631  mutable decaf::lang::Pointer<ConstStlMapValueCollection> cachedConstValueCollection;
632 
633  public:
634 
638  StlMap() : Map<K,V>(), valueMap(), mutex(), modCount(0),
639  cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
640  cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
641  }
642 
649  StlMap(const StlMap& source ) : Map<K,V>(), valueMap(), mutex(), modCount(0),
650  cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
651  cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
652  copy(source);
653  }
654 
661  StlMap(const Map<K,V>& source) : Map<K,V>(), valueMap(), mutex(), modCount(0),
662  cachedEntrySet(), cachedKeySet(), cachedValueCollection(),
663  cachedConstEntrySet(), cachedConstKeySet(), cachedConstValueCollection() {
664  copy(source);
665  }
666 
667  virtual ~StlMap() {}
668 
672  virtual bool equals(const StlMap& source) const {
673  return this->valueMap == source.valueMap;
674  }
675 
679  virtual bool equals(const Map<K,V>& source) const {
680  typename std::auto_ptr< Iterator<K> > iterator(this->keySet().iterator());
681  while (iterator->hasNext()) {
682  K key = iterator->next();
683  if (!this->containsKey(key)) {
684  return false;
685  }
686 
687  if (!(this->get(key) == source.get(key))) {
688  return false;
689  }
690  }
691 
692  return true;
693  }
694 
698  virtual void copy(const StlMap& source) {
699  this->valueMap.clear();
700  this->valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
701  }
702 
706  virtual void copy(const Map<K, V>& source) {
707  this->clear();
708  this->putAll(source);
709  }
710 
714  virtual void clear() {
715  valueMap.clear();
716  }
717 
721  virtual bool containsKey(const K& key) const {
722  if (valueMap.empty()) {
723  return false;
724  }
725 
726  typename std::map<K, V, COMPARATOR>::const_iterator iter;
727  iter = valueMap.find(key);
728  return iter != valueMap.end();
729  }
730 
734  virtual bool containsValue(const V& value) const {
735  if (valueMap.empty()) {
736  return false;
737  }
738 
739  typename std::map<K, V, COMPARATOR>::const_iterator iter = valueMap.begin();
740  for (; iter != valueMap.end(); ++iter) {
741  if ((*iter).second == value) {
742  return true;
743  }
744  }
745 
746  return false;
747  }
748 
752  virtual bool isEmpty() const {
753  return valueMap.empty();
754  }
755 
759  virtual int size() const {
760  return (int)valueMap.size();
761  }
762 
766  virtual V& get(const K& key) {
767  typename std::map<K, V, COMPARATOR>::iterator iter;
768  iter = valueMap.find(key);
769  if (iter == valueMap.end()) {
770  throw NoSuchElementException(__FILE__, __LINE__, "Key does not exist in map");
771  }
772 
773  return iter->second;
774  }
775 
779  virtual const V& get(const K& key) const {
780  typename std::map<K, V, COMPARATOR>::const_iterator iter;
781  iter = valueMap.find(key);
782  if (iter == valueMap.end()) {
783  throw NoSuchElementException(__FILE__, __LINE__, "Key does not exist in map");
784  }
785 
786  return iter->second;
787  }
788 
792  virtual bool put(const K& key, const V& value) {
793  bool result = false;
794  if (this->containsKey(key)) {
795  result = true;
796  }
797  valueMap[key] = value;
798  modCount++;
799  return result;
800  }
801 
805  virtual bool put(const K& key, const V& value, V& oldValue) {
806  bool result = false;
807  if (this->containsKey(key)) {
808  result = true;
809  oldValue = valueMap[key];
810  }
811  valueMap[key] = value;
812  modCount++;
813  return result;
814  }
815 
819  virtual void putAll(const StlMap<K, V, COMPARATOR>& other) {
820  this->valueMap.insert(other.valueMap.begin(), other.valueMap.end());
821  this->modCount++;
822  }
823 
827  virtual void putAll(const Map<K, V>& other) {
828  typename std::auto_ptr< Iterator<K> > iterator(other.keySet().iterator());
829  while (iterator->hasNext()) {
830  K key = iterator->next();
831  this->put(key, other.get(key));
832  }
833  }
834 
838  virtual V remove(const K& key) {
839 
840  typename std::map<K, V, COMPARATOR>::iterator iter = valueMap.find(key);
841  if (iter == valueMap.end()) {
843  __FILE__, __LINE__, "Key is not present in this Map.");
844  }
845 
846  V result = iter->second;
847  valueMap.erase(iter);
848  modCount++;
849  return result;
850  }
851 
853  if (this->cachedEntrySet == NULL) {
854  this->cachedEntrySet.reset(new StlMapEntrySet(this));
855  }
856  return *(this->cachedEntrySet);
857  }
858  virtual const Set< MapEntry<K, V> >& entrySet() const {
859  if (this->cachedConstEntrySet == NULL) {
860  this->cachedConstEntrySet.reset(new ConstStlMapEntrySet(this));
861  }
862  return *(this->cachedConstEntrySet);
863  }
864 
865  virtual Set<K>& keySet() {
866  if (this->cachedKeySet == NULL) {
867  this->cachedKeySet.reset(new StlMapKeySet(this));
868  }
869  return *(this->cachedKeySet);
870  }
871 
872  virtual const Set<K>& keySet() const {
873  if (this->cachedConstKeySet == NULL) {
874  this->cachedConstKeySet.reset(new ConstStlMapKeySet(this));
875  }
876  return *(this->cachedConstKeySet);
877  }
878 
879  virtual Collection<V>& values() {
880  if (this->cachedValueCollection == NULL) {
881  this->cachedValueCollection.reset(new StlMapValueCollection(this));
882  }
883  return *(this->cachedValueCollection);
884  }
885 
886  virtual const Collection<V>& values() const {
887  if (this->cachedConstValueCollection == NULL) {
888  this->cachedConstValueCollection.reset(new ConstStlMapValueCollection(this));
889  }
890  return *(this->cachedConstValueCollection);
891  }
892 
893  public:
894 
895  virtual void lock() {
896  mutex.lock();
897  }
898 
899  virtual bool tryLock() {
900  return mutex.tryLock();
901  }
902 
903  virtual void unlock() {
904  mutex.unlock();
905  }
906 
907  virtual void wait() {
908  mutex.wait();
909  }
910 
911  virtual void wait( long long millisecs ) {
912  mutex.wait( millisecs );
913  }
914 
915  virtual void wait( long long millisecs, int nanos ) {
916  mutex.wait( millisecs, nanos );
917  }
918 
919  virtual void notify() {
920  mutex.notify();
921  }
922 
923  virtual void notifyAll() {
924  mutex.notifyAll();
925  }
926 
927  };
928 
929 }}
930 
931 #endif /*_DECAF_UTIL_STLMAP_H_*/