Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Dictionary.cpp

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

Generated on Mon Oct 22 20:03:18 2007 for CoreLibrary by  doxygen 1.4.4