00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 #include "Dictionary.h"
00026 
00027 namespace cmlabs {
00028 
00029 
00030 
00031 
00032 
00033 Dictionary::Dictionary()
00034 {
00035         objectTable.type = "dictionary";
00036 }
00037 
00038 Dictionary::Dictionary(const JString& xml) : CollectionBase(xml) {
00039         objectTable.type = "dictionary";
00040 }
00041 
00042 Dictionary::Dictionary(XMLNode* node) : CollectionBase(node) {
00043         objectTable.type = "dictionary";
00044 }
00045 
00046 
00047 Dictionary::~Dictionary()
00048 {
00049 
00050 }
00051 
00052 bool Dictionary::addAll(const CollectionBase* c) {
00053         return CollectionBase::copyAll(c);
00054 }
00055 
00056 bool Dictionary::addAll(const CollectionBase& c) {
00057         return CollectionBase::copyAll(c);
00058 }
00059 
00060 Dictionary::Dictionary(const Dictionary &c) {
00061         objectTable.copyAll(c.objectTable);
00062         objectTable.type = c.objectTable.type;
00063         objectTable.sorting = c.objectTable.sorting;
00064 }
00065 
00066 const Dictionary& Dictionary::operator = (const Dictionary& c) {
00067         objectTable.removeAll();
00068         objectTable.copyAll(c.objectTable);
00069         objectTable.type = c.objectTable.type;
00070         objectTable.sorting = c.objectTable.sorting;
00071         return *this;
00072 }
00073 
00074 
00075 bool Dictionary::contains(const JString& key) const {
00076         return objectTable.containsKey(&key);
00077 }
00078 
00079 
00080 JString Dictionary::get(const JString& key) const {
00081         JString* str = (JString*) objectTable.get(&key);
00082         if (str == NULL)
00083                 return "";
00084         else
00085                 return *str;
00086 }
00087 
00088 JString Dictionary::take(const JString& key) {
00089         JString* str = (JString*) objectTable.take(&key);
00090         if (str == NULL)
00091                 return "";
00092         else
00093                 return *str;
00094 }
00095 
00096 bool Dictionary::containsIgnoreCase(const JString& key) const {
00097         if (contains(key))
00098                 return true;
00099         if (contains(key.toLowerCase()))
00100                 return true;
00101         for (int n=0; n<getCount(); n++) {
00102                 if (getKey(n).equalsIgnoreCase(key))
00103                         return true;
00104         }
00105         return false;
00106 }
00107 
00108 JString Dictionary::getIgnoreCase(const JString& key) const {
00109         if (contains(key))
00110                 return get(key);
00111         if (contains(key.toLowerCase()))
00112                 return get(key.toLowerCase());
00113         for (int n=0; n<getCount(); n++) {
00114                 if (getKey(n).equalsIgnoreCase(key))
00115                         return get(n);
00116         }
00117         return "";
00118 }
00119 
00120 bool Dictionary::put(const JString& key, const JString& value) {
00121         if (key.length() == 0)
00122                 return false;
00123         JString* keycopy = (JString*) key.clone();
00124         JString* valuecopy = (JString*) value.clone();
00125         if (!objectTable.add(keycopy, valuecopy)) {
00126                 delete(keycopy);
00127                 delete(valuecopy);
00128                 return false;
00129         }
00130         else
00131                 return true;
00132 }
00133 
00134 bool Dictionary::remove(const JString& key) {
00135         return objectTable.remove(&key);
00136 }
00137 
00138 bool Dictionary::remove(int pos) {
00139         return objectTable.remove(pos);
00140 }
00141 
00142 bool Dictionary::putFirst(const JString& key, const JString& value) {
00143         if (key.length() == 0)
00144                 return false;
00145         JString* keycopy = (JString*) key.clone();
00146         JString* valuecopy = (JString*) value.clone();
00147         if (!objectTable.addFirst(keycopy, valuecopy)) {
00148                 delete(keycopy);
00149                 delete(valuecopy);
00150                 return false;
00151         }
00152         else
00153                 return true;
00154 }
00155 
00156 bool Dictionary::putLast(const JString& key, const JString& value) {
00157         if (key.length() == 0)
00158                 return false;
00159         JString* keycopy = (JString*) key.clone();
00160         JString* valuecopy = (JString*) value.clone();
00161         if (!objectTable.addLast(keycopy, valuecopy)) {
00162                 delete(keycopy);
00163                 delete(valuecopy);
00164                 return false;
00165         }
00166         else
00167                 return true;
00168 }
00169 
00170 bool Dictionary::putAfter(int pos, const JString& key, const JString& value) {
00171         if (key.length() == 0)
00172                 return false;
00173         JString* keycopy = (JString*) key.clone();
00174         JString* valuecopy = (JString*) value.clone();
00175         if (!objectTable.addAfter(pos, keycopy, valuecopy)) {
00176                 delete(keycopy);
00177                 delete(valuecopy);
00178                 return false;
00179         }
00180         else
00181                 return true;
00182 }
00183 
00184 
00185 
00186 Collection Dictionary::getAllKeys() {
00187         Collection col;
00188         if (objectTable.first == NULL)
00189                 return col;
00190 
00191         CollectionEntry* item = objectTable.first;
00192 
00193         while (item != NULL) {
00194                 if (item->key != NULL)
00195                         col.add(*(JString*)item->key);
00196                 item = (CollectionEntry*) item->next;
00197         }               
00198         return col;
00199 }
00200 
00201 JString Dictionary::get(int pos) {
00202         JString* str = (JString*) objectTable.get(pos);
00203         if (str == NULL)
00204                 return "";
00205         else
00206                 return *str;
00207 }
00208 
00209 JString Dictionary::operator [](int pos) const {
00210         return get(pos);
00211 }
00212 
00213 JString Dictionary::operator [](const JString& key) const {
00214         return get(key);
00215 }
00216 
00217 JString Dictionary::get(int pos) const {
00218         JString* str = (JString*) objectTable.get(pos);
00219         if (str == NULL)
00220                 return "";
00221         else
00222                 return *str;
00223 }
00224 
00225 JString Dictionary::take(int pos) {
00226         JString* str = (JString*) objectTable.take(pos);
00227         if (str == NULL)
00228                 return "";
00229         else
00230                 return *str;
00231 }
00232 
00233 
00234 JString Dictionary::getFirst() {
00235         JString* str = (JString*) objectTable.getFirst();
00236         if (str == NULL)
00237                 return "";
00238         else
00239                 return *str;
00240 }
00241 
00242 JString Dictionary::getLast() {
00243         JString* str = (JString*) objectTable.getLast();
00244         if (str == NULL)
00245                 return "";
00246         else
00247                 return *str;
00248 }
00249 
00250 JString Dictionary::getNext() {
00251         JString* str = (JString*) objectTable.getNext();
00252         if (str == NULL)
00253                 return "";
00254         else
00255                 return *str;
00256 }
00257 
00258 JString Dictionary::getPrevious() {
00259         JString* str = (JString*) objectTable.getPrevious();
00260         if (str == NULL)
00261                 return "";
00262         else
00263                 return *str;
00264 }
00265 
00266 int Dictionary::getPos(const JString& value) const {
00267         return(objectTable.getPos(&value));
00268 }
00269 
00270 int Dictionary::getPosKey(const JString& key) const {
00271         return(objectTable.getPosKey(&key));
00272 }
00273 
00274 JString Dictionary::getKey(int pos) {
00275         Object* obj = objectTable.getKey(pos);
00276         if (obj == NULL)
00277                 return "";
00278         return *(JString*) obj;
00279 }
00280 
00281 JString Dictionary::getKey(int pos) const {
00282         Object* obj = objectTable.getKey(pos);
00283         if (obj == NULL)
00284                 return "";
00285         return *(JString*) obj;
00286 }
00287 
00288 JString Dictionary::getFirstKey() {
00289         Object* obj = objectTable.getFirstKey();
00290         if (obj == NULL)
00291                 return "";
00292         return *(JString*) obj;
00293 }
00294 
00295 JString Dictionary::getLastKey() {
00296         Object* obj = objectTable.getLastKey();
00297         if (obj == NULL)
00298                 return "";
00299         return *(JString*) obj;
00300 }
00301 
00302 JString Dictionary::getNextKey() {
00303         Object* obj = objectTable.getNextKey();
00304         if (obj == NULL)
00305                 return "";
00306         return *(JString*) obj;
00307 }
00308 
00309 JString Dictionary::getPreviousKey() {
00310         Object* obj = objectTable.getPreviousKey();
00311         if (obj == NULL)
00312                 return "";
00313         return *(JString*) obj;
00314 }
00315 
00316 bool Dictionary::removeNoDelete(int pos) {
00317         return objectTable.remove(pos);
00318 }
00319 
00320 bool Dictionary::removeAllNoDelete() {
00321         return objectTable.removeAll();
00322 }
00323 
00324 void Dictionary::noDelete() {
00325         objectTable.doDelete();
00326 }
00327 
00328 void Dictionary::doDelete() {
00329         objectTable.doDelete();
00330 }
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 bool Dictionary::unitTest() {
00340 
00341         
00342         JString test1 = "test";
00343         JString test2 = "test";
00344         if (!test1.isSameClass(&test2)) {
00345                 this->addUnitTestLog("System typeid is not working/enabled!");
00346                 return false;
00347         }
00348 
00349         Dictionary* t = new Dictionary();
00350         t->put("test1", "bla1");
00351         t->put("test2", "bla2");
00352         t->put("test3", "bla3");
00353         t->put("test4", "bla4");
00354 
00355         if (!t->get("test1").equals("bla1"))
00356                 return false;
00357         if (!t->get("test2").equals("bla2"))
00358                 return false;
00359         if (!t->get("test3").equals("bla3"))
00360                 return false;
00361         if (!t->get("test4").equals("bla4"))
00362                 return false;
00363 
00364         t->put("test4", "bla5");
00365 
00366         if (!t->get("test4").equals("bla5"))
00367                 return false;
00368 
00369         delete(t);
00370         return true;
00371 }
00372 
00373 
00374 
00375 
00376 }