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