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 "TimeSeries.h"
00026
00027 namespace cmlabs {
00028
00029
00030
00031
00032
00033 TimeSeries::TimeSeries()
00034 {
00035 }
00036
00037 TimeSeries::~TimeSeries()
00038 {
00039 removeAll();
00040 }
00041
00042 TimeSeries::TimeSeries(const TimeSeries &ts) {
00043 times.addAll(ts.times);
00044 dict.copyAll(ts.dict);
00045 }
00046
00047 const TimeSeries& TimeSeries::operator = (const TimeSeries& ts) {
00048 times.addAll(ts.times);
00049 dict.copyAll(ts.dict);
00050 return *this;
00051 }
00052
00053 TimeSeries::TimeSeries(const JString& xml) {
00054 dict.fromXML(xml);
00055 }
00056
00057 TimeSeries::TimeSeries(XMLNode* node) {
00058 dict.fromXML(node);
00059 }
00060
00061 Object* TimeSeries::clone() const {
00062 TimeSeries* ts = new TimeSeries();
00063 ts->times.addAll(times);
00064 ts->dict.copyAll(dict);
00065
00066
00067
00068
00069
00070
00071 return ts;
00072 }
00073
00074
00075 unsigned long TimeSeries::getPayloadSize() const {
00076 return times.getPayloadSize() + dict.getPayloadSize();
00077 }
00078
00079
00080 int TimeSeries::getCount() {
00081 return dict.getCount();
00082 }
00083
00084
00085 bool TimeSeries::contains(const JTime& time) {
00086 JString key = time.asText();
00087 return(dict.get(key) != NULL);
00088 }
00089
00090
00091 JTime TimeSeries::getTime(int pos) {
00092 JTime t;
00093 JString str = times.get(pos);
00094 str.replace("x", "");
00095 if (str.length() > 0)
00096 t = JTime(str);
00097 else
00098 t.setInvalid();
00099 return t;
00100 }
00101
00102 Object* TimeSeries::get(int pos) {
00103 return dict.get(pos);
00104 }
00105
00106 Object* TimeSeries::get(const JTime& time) {
00107 JString key = time.asText();
00108 return dict.get(key);
00109 }
00110
00111 Object* TimeSeries::getOldest() {
00112 return dict.get(0);
00113 }
00114
00115 Object* TimeSeries::getNewest() {
00116 return dict.get(dict.getCount()-1);
00117 }
00118
00119 Object* TimeSeries::getFirstAfter(const JTime& time) {
00120 return dict.get(times.getFirstAfter(time.asText()));
00121 }
00122
00123 Object* TimeSeries::getLastBefore(const JTime& time) {
00124 return dict.get(times.getLastBefore(time.asText()));
00125 }
00126
00127
00128 bool TimeSeries::put(const JTime& time, Object* obj) {
00129 JString key = time.asText();
00130 while (dict.contains(key)) {
00131 key += "x";
00132 }
00133
00134 int pos = times.addAndReturnPosition(key);
00135
00136 if (pos >= 0) {
00137 if (!dict.putAfter(pos, key, obj)) {
00138 times.remove(pos);
00139 return false;
00140 }
00141
00142 return true;
00143 }
00144 else
00145 return false;
00146 }
00147
00148 bool TimeSeries::remove(const JTime& time) {
00149 JString key = time.asText();
00150 while (dict.contains(key)) {
00151 dict.remove(key);
00152 times.remove(key);
00153 key += "x";
00154 }
00155 return true;
00156 }
00157
00158 bool TimeSeries::removeOldest() {
00159 JString key = times.getFirst();
00160 if (key.length() > 0) {
00161 dict.remove(key);
00162 times.remove(key);
00163 return true;
00164 }
00165 else
00166 return false;
00167 }
00168
00169 bool TimeSeries::removeNewest() {
00170 JString key = times.getLast();
00171 if (key.length() > 0) {
00172 dict.remove(key);
00173 times.remove(key);
00174 return true;
00175 }
00176 else
00177 return false;
00178 }
00179
00180 bool TimeSeries::removeAllBetween(const JTime& t1, const JTime& t2) {
00181 Collection col = times.getAllBetween(t1.asText(), t2.asText());
00182 for (int n=0; n<col.getCount(); n++) {
00183 times.remove(col.get(n));
00184 dict.remove(col.get(n));
00185 }
00186 return true;
00187 }
00188
00189 bool TimeSeries::removeAllBefore(const JTime& time) {
00190 Collection col = times.getAllBetween("", time.asText());
00191 for (int n=0; n<col.getCount(); n++) {
00192 times.remove(col.get(n));
00193 dict.remove(col.get(n));
00194 }
00195 return true;
00196 }
00197
00198 bool TimeSeries::removeAllAfter(const JTime& time) {
00199 Collection col = times.getAllBetween(time.asText(), "99999999999999999999999999999");
00200 for (int n=0; n<col.getCount(); n++) {
00201 times.remove(col.get(n));
00202 dict.remove(col.get(n));
00203 }
00204 return true;
00205 }
00206
00207 ObjectCollection* TimeSeries::getAllBetween(const JTime& t1, const JTime& t2) {
00208 ObjectCollection* retCol = new ObjectCollection();
00209
00210
00211 Collection col = times.getAllBetween(t1.asText(), t2.asText()+"z");
00212 for (int n=0; n<col.getCount(); n++) {
00213
00214 retCol->add(dict.get(col.get(n)));
00215 }
00216 if (retCol->getCount() == 0) {
00217 delete(retCol);
00218 return NULL;
00219 }
00220 return retCol;
00221 }
00222
00223 ObjectCollection* TimeSeries::getAllBefore(const JTime& time) {
00224 ObjectCollection* retCol = new ObjectCollection();
00225 Collection col = times.getAllBetween("", time.asText());
00226 for (int n=0; n<col.getCount(); n++) {
00227 retCol->add(dict.get(col.get(0)));
00228 }
00229 if (retCol->getCount() == 0) {
00230 delete(retCol);
00231 return NULL;
00232 }
00233 return retCol;
00234 }
00235
00236 ObjectCollection* TimeSeries::getAllAfter(const JTime& time) {
00237 ObjectCollection* retCol = new ObjectCollection();
00238 Collection col = times.getAllBetween(time.asText(), "");
00239 for (int n=0; n<col.getCount(); n++) {
00240 retCol->add(dict.get(col.get(0)));
00241 }
00242 if (retCol->getCount() == 0) {
00243 delete(retCol);
00244 return NULL;
00245 }
00246 return retCol;
00247 }
00248
00249 bool TimeSeries::removeAll() {
00250
00251
00252
00253 dict.removeAll();
00254 times.removeAll();
00255 return true;
00256 }
00257
00258
00259 JTime TimeSeries::getFirstTime() {
00260 JTime t;
00261 JString str = times.getFirst();
00262 str.replace("x", "");
00263 if (str.length() > 0)
00264 t = JTime(str);
00265 else
00266 t.setInvalid();
00267 return t;
00268 }
00269
00270 JTime TimeSeries::getLastTime() {
00271 JTime t;
00272 JString str = times.getLast();
00273 str.replace("x", "");
00274 if (str.length() > 0)
00275 t = JTime(str);
00276 else
00277 t.setInvalid();
00278 return t;
00279 }
00280
00281 JTime TimeSeries::getNextTime() {
00282 JTime t;
00283 JString str = times.getNext();
00284 str.replace("x", "");
00285 if (str.length() > 0)
00286 t = JTime(str);
00287 else
00288 t.setInvalid();
00289 return t;
00290 }
00291
00292 JTime TimeSeries::getPreviousTime() {
00293 JTime t;
00294 JString str = times.getPrevious();
00295 str.replace("x", "");
00296 if (str.length() > 0)
00297 t = JTime(str);
00298 else
00299 t.setInvalid();
00300 return t;
00301 }
00302
00303
00304
00305
00306
00307 JString TimeSeries::print() {
00308
00309 JString res, str;
00310 JTime t;
00311
00312 if (times.getCount() != dict.getCount())
00313 res += JString::format("TimeSeries broken with %d and %d entries...\n", times.getCount(), dict.getCount());
00314
00315 Object* obj;
00316 for (int n=0; n<this->times.getCount(); n++) {
00317 str = times.get(n);
00318 str.replace("x", "");
00319 t = JTime(str);
00320 obj = dict.get(n);
00321 if (obj != NULL) {
00322 res += JString::format("%s: %s = %s\n", (char*) JString(JString(n), 5, 1), (char*) t.print(), (char*) obj->print());
00323 }
00324 else {
00325 res += JString::format("%s: %s = null\n", (char*) JString(JString(n), 5, 1), (char*) t.print());
00326 }
00327 }
00328
00329 return res;
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339 long TimeSeries::getBinarySize(int chunk) {
00340 return dict.getBinarySize(chunk);
00341 }
00342
00343
00344 int TimeSeries::getBinaryChunkCount() {
00345 return dict.getBinaryChunkCount();
00346 }
00347
00348
00349 long TimeSeries::toBinaryBuffer(int chunk, char* buffer, int maxlen) {
00350 return dict.toBinaryBuffer(chunk, buffer, maxlen);
00351 }
00352
00353
00354 bool TimeSeries::fromBinaryBuffer(int chunk, char* buffer, long len) {
00355 return dict.fromBinaryBuffer(chunk, buffer, len);
00356 }
00357
00358
00359
00360
00361
00362 bool TimeSeries::unitTest() {
00363
00364
00365
00366
00367
00368
00369 return true;
00370 }
00371
00372
00373
00374 }