AbstractMap.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00023 #ifndef _ABSTRACT_CLASS_BEE_UTIL_ABSTRACTMAP_H
00024 #define _ABSTRACT_CLASS_BEE_UTIL_ABSTRACTMAP_H
00025
00026 #ifdef __cplusplus
00027
00028 #include "beecrypt/c++/lang/NullPointerException.h"
00029 using beecrypt::lang::NullPointerException;
00030 #include "beecrypt/c++/util/Map.h"
00031 using beecrypt::util::Map;
00032 #include "beecrypt/c++/util/AbstractSet.h"
00033 using beecrypt::util::AbstractSet;
00034
00035 namespace beecrypt {
00036 namespace util {
00039 template <class K, class V> class AbstractMap : public Object, public virtual Map<K,V>
00040 {
00041 private:
00042 class KeySet : public AbstractSet<K>
00043 {
00044 private:
00045 class Iter : public Object, virtual public Iterator<K>
00046 {
00047 private:
00048 Iterator<class Map<K,V>::Entry>* _it;
00049
00050 public:
00051 Iter(AbstractMap* m) : _it(m->entrySet().iterator())
00052 {
00053 }
00054 Iter(const AbstractMap* m) : _it(m->entrySet().iterator())
00055 {
00056 }
00057 virtual ~Iter()
00058 {
00059 delete _it;
00060 }
00061
00062 virtual bool hasNext() throw ()
00063 {
00064 return _it->hasNext();
00065 }
00066 virtual K* next() throw (NoSuchElementException)
00067 {
00068 return _it->next()->getKey();
00069 }
00070 virtual void remove()
00071 {
00072 _it->remove();
00073 }
00074 };
00075
00076 private:
00077 AbstractMap* _m;
00078
00079 public:
00080 KeySet(AbstractMap* m) : _m(m)
00081 {
00082 }
00083 virtual ~KeySet() {}
00084
00085 virtual Iterator<K>* iterator()
00086 {
00087 return new Iter(_m);
00088 }
00089 virtual Iterator<K>* iterator() const
00090 {
00091 return new Iter(_m);
00092 }
00093 virtual jint size() const throw ()
00094 {
00095 return _m->size();
00096 }
00097 };
00098
00099 class Values : public AbstractCollection<V>
00100 {
00101 private:
00102 class Iter : public Object, virtual public Iterator<V>
00103 {
00104 private:
00105 Iterator<class Map<K,V>::Entry>* _it;
00106
00107 public:
00108 Iter(AbstractMap* m) : _it(m->entrySet().iterator())
00109 {
00110 }
00111 Iter(const AbstractMap* m) : _it(m->entrySet().iterator())
00112 {
00113 }
00114 virtual ~Iter()
00115 {
00116 delete _it;
00117 }
00118
00119 virtual bool hasNext() throw ()
00120 {
00121 return _it->hasNext();
00122 }
00123 virtual V* next() throw (NoSuchElementException)
00124 {
00125 return _it->next()->getValue();
00126 }
00127 virtual void remove()
00128 {
00129 _it->remove();
00130 }
00131 };
00132
00133 private:
00134 AbstractMap* _m;
00135
00136 public:
00137 Values(AbstractMap* m) : _m(m)
00138 {
00139 }
00140 virtual ~Values() {}
00141
00142 virtual Iterator<V>* iterator()
00143 {
00144 return new Iter(_m);
00145 }
00146 virtual Iterator<V>* iterator() const
00147 {
00148 return new Iter(_m);
00149 }
00150 virtual jint size() const throw ()
00151 {
00152 return _m->size();
00153 }
00154 };
00155
00156 private:
00157 mutable Set<K>* _keys;
00158 mutable Collection<V>* _values;
00159
00160 protected:
00161 AbstractMap()
00162 {
00163 _keys = 0;
00164 _values = 0;
00165 }
00166
00167 public:
00168 virtual ~AbstractMap()
00169 {
00170 clear();
00171
00172 delete _keys;
00173 delete _values;
00174 }
00175
00176 virtual void clear()
00177 {
00178 entrySet().clear();
00179 }
00180 virtual bool containsKey(const K* key) const
00181 {
00182 bool result = false;
00183 jint pos = size();
00184 Iterator<class Map<K,V>::Entry>* it = entrySet().iterator();
00185 assert(it != 0);
00186 if (key)
00187 {
00188 while (--pos >= 0)
00189 {
00190 class Map<K,V>::Entry* e = it->next();
00191 if (e->getKey->equals(key))
00192 {
00193 result = true;
00194 break;
00195 }
00196 }
00197 }
00198 else
00199 {
00200 while (--pos >= 0)
00201 if (!it->next())
00202 {
00203 result = true;
00204 break;
00205 }
00206 }
00207 delete it;
00208 return result;
00209 }
00210 virtual bool containsValue(const V* value) const
00211 {
00212 bool result = false;
00213 jint pos = size();
00214 Iterator<class Map<K,V>::Entry>* it = entrySet().iterator();
00215 assert(it != 0);
00216 if (value)
00217 {
00218 while (--pos >= 0)
00219 {
00220 V* tmp = it->next()->getValue();
00221 if (tmp && tmp->equals(value))
00222 {
00223 result = true;
00224 break;
00225 }
00226 }
00227 }
00228 else
00229 {
00230 while (--pos >= 0)
00231 if (!it->next())
00232 {
00233 result = true;
00234 break;
00235 }
00236 }
00237 delete it;
00238 return false;
00239 }
00240 virtual Set<class Map<K,V>::Entry>& entrySet() = 0;
00241 virtual const Set<class Map<K,V>::Entry>& entrySet() const = 0;
00242 virtual bool equals(const Object* obj) const throw ()
00243 {
00244 if (this == obj)
00245 return true;
00246
00247 if (obj)
00248 {
00249 const Map<K,V>* map = dynamic_cast<const Map<K,V> >(obj);
00250 if (map)
00251 {
00252 if (map->size() != size())
00253 return false;
00254
00255 bool result = true;
00256 jint pos = size();
00257 Iterator<class Map<K,V>::Entry>* it = entrySet().iterator();
00258 assert(it != 0);
00259 while (--pos >= 0)
00260 {
00261 class Map<K,V>::Entry* e = it.next();
00262 K* k = e->getKey();
00263 V* v = e->getValue();
00264 if (v)
00265 {
00266 if (!v->equals(map->get(k)))
00267 {
00268 result = false;
00269 break;
00270 }
00271 }
00272 else
00273 {
00274 if (map->get(k) || !map->containsKey(k))
00275 {
00276 result = false;
00277 break;
00278 }
00279 }
00280 }
00281 delete it;
00282 return result;
00283 }
00284 }
00285 return false;
00286 }
00287 virtual V* get(const Object* key) const
00288 {
00289 V* result = 0;
00290 jint pos = size();
00291 Iterator<class Map<K,V>::Entry>* it = entrySet().iterator();
00292 assert(it != 0);
00293 if (key)
00294 {
00295 while (--pos >= 0)
00296 {
00297 class Map<K,V>::Entry* e = it->next();
00298 if (key->equals(e->getKey()))
00299 {
00300 result = e->getValue();
00301 break;
00302 }
00303 }
00304 }
00305 else
00306 {
00307 while (--pos >= 0)
00308 {
00309 class Map<K,V>::Entry* e = it->next();
00310 if (!e->getKey())
00311 {
00312 result = e->getValue();
00313 break;
00314 }
00315 }
00316 }
00317 delete it;
00318 return result;
00319 }
00320 virtual jint hashCode() const throw ()
00321 {
00322 jint pos = size(), result = 0;
00323 Iterator<class Map<K,V>::Entry>* it = entrySet().iterator();
00324 assert(it != 0);
00325 while (--pos >= 0)
00326 result += it->next()->hashCode();
00327 delete it;
00328 return result;
00329 }
00330 virtual bool isEmpty()
00331 {
00332 return entrySet().size() == 0;
00333 }
00334 virtual Set<K>& keySet()
00335 {
00336 if (!_keys)
00337 _keys = new KeySet(this);
00338
00339 return *_keys;
00340 }
00341 virtual const Set<K>& keySet() const
00342 {
00343 if (!_keys)
00344 _keys = new KeySet(const_cast<AbstractMap*>(this));
00345
00346 return *_keys;
00347 }
00348 virtual V* put(K* key, V* value)
00349 {
00350 throw UnsupportedOperationException();
00351 }
00352 virtual void putAll(const Map<K,V>& m)
00353 {
00354 jint pos = m.size();
00355 Iterator<class Map<K,V>::Entry>* mit = m.entrySet().iterator();
00356 assert(mit != 0);
00357 while (--pos >= 0)
00358 {
00359 class Map<K,V>::Entry* e = mit->next();
00360 V* tmp = put(e->getKey(), e->getValue());
00361 collection_rcheck(tmp);
00362 }
00363 delete mit;
00364 }
00365 virtual V* remove(const K* key)
00366 {
00367 V* result = 0;
00368 jint pos = size();
00369 Iterator<class Map<K,V>::Entry>* it = entrySet().iterator();
00370 assert(it != 0);
00371 if (key)
00372 {
00373 while (--pos >= 0)
00374 {
00375 class Map<K,V>::Entry* e = it->next();
00376 if (key->equals(e->getKey()))
00377 {
00378 result = e->getValue();
00379 break;
00380 }
00381 }
00382 }
00383 else
00384 {
00385 while (--pos >= 0)
00386 {
00387 class Map<K,V>::Entry* e = it->next();
00388 if (!e->getKey())
00389 {
00390 result = e->getValue();
00391 break;
00392 }
00393 }
00394 }
00395
00396 if (result)
00397 it->remove();
00398
00399 delete it;
00400
00401 return result;
00402 }
00403 virtual jint size()
00404 {
00405 return entrySet().size();
00406 }
00407 virtual String toString() const throw ()
00408 {
00409 Iterator<class Map<K,V>::Entry>& it = entrySet.iterator();
00410
00411 StringBuilder buf("{");
00412
00413 for (jint pos = size(); pos > 0; pos--)
00414 {
00415 class Map<K,V>::Entry* e = it.next();
00416 K* k = e->getKey();
00417 V* v = e->getValue();
00418
00419 if (k == this)
00420 buf.append("(this map)");
00421 else
00422 buf.append(k);
00423
00424 buf.append("=");
00425
00426 if (v == this)
00427 buf.append("(this map)");
00428 else
00429 buf.append(v);
00430
00431 if (pos > 1)
00432 buf.append(", ");
00433 }
00434
00435 buf.append("}");
00436
00437 return buf.toString();
00438 }
00439 virtual Collection<V>& values()
00440 {
00441 if (!_values)
00442 _values = new Values(this);
00443
00444 return *_values;
00445 }
00446 virtual const Collection<V>& values() const
00447 {
00448 if (!_values)
00449 _values = new Values(const_cast<AbstractMap*>(this));
00450
00451 return *_values;
00452 }
00453 };
00454 }
00455 }
00456
00457 #endif
00458
00459 #endif