1 44 45 package org.jfree.data.time; 46 47 import java.io.Serializable ; 48 import java.util.ArrayList ; 49 import java.util.List ; 50 51 import org.jfree.data.general.Series; 52 import org.jfree.data.general.SeriesException; 53 54 62 public class TimePeriodValues extends Series implements Serializable { 63 64 65 static final long serialVersionUID = -2210593619794989709L; 66 67 68 protected static final String DEFAULT_DOMAIN_DESCRIPTION = "Time"; 69 70 71 protected static final String DEFAULT_RANGE_DESCRIPTION = "Value"; 72 73 74 private String domain; 75 76 77 private String range; 78 79 80 private List data; 81 82 83 private int minStartIndex = -1; 84 85 86 private int maxStartIndex = -1; 87 88 89 private int minMiddleIndex = -1; 90 91 92 private int maxMiddleIndex = -1; 93 94 95 private int minEndIndex = -1; 96 97 98 private int maxEndIndex = -1; 99 100 105 public TimePeriodValues(String name) { 106 this(name, DEFAULT_DOMAIN_DESCRIPTION, DEFAULT_RANGE_DESCRIPTION); 107 } 108 109 120 public TimePeriodValues(String name, String domain, String range) { 121 super(name); 122 this.domain = domain; 123 this.range = range; 124 this.data = new ArrayList (); 125 } 126 127 132 public String getDomainDescription() { 133 return this.domain; 134 } 135 136 141 public void setDomainDescription(String description) { 142 String old = this.domain; 143 this.domain = description; 144 firePropertyChange("Domain", old, description); 145 } 146 147 152 public String getRangeDescription() { 153 return this.range; 154 } 155 156 161 public void setRangeDescription(String description) { 162 String old = this.range; 163 this.range = description; 164 firePropertyChange("Range", old, description); 165 } 166 167 172 public int getItemCount() { 173 return this.data.size(); 174 } 175 176 183 public TimePeriodValue getDataItem(int index) { 184 return (TimePeriodValue) this.data.get(index); 185 } 186 187 194 public TimePeriod getTimePeriod(int index) { 195 return getDataItem(index).getPeriod(); 196 } 197 198 205 public Number getValue(int index) { 206 return getDataItem(index).getValue(); 207 } 208 209 214 public void add(TimePeriodValue item) { 215 216 if (item == null) { 218 throw new IllegalArgumentException ("Null item not allowed."); 219 } 220 221 this.data.add(item); 223 updateBounds(item.getPeriod(), this.data.size() - 1); 224 225 } 226 227 233 private void updateBounds(TimePeriod period, int index) { 234 235 long start = period.getStart().getTime(); 236 long end = period.getEnd().getTime(); 237 long middle = start + ((end - start) / 2); 238 239 if (this.minStartIndex >= 0) { 240 long minStart = getDataItem(this.minStartIndex).getPeriod() 241 .getStart().getTime(); 242 if (start < minStart) { 243 this.minStartIndex = index; 244 } 245 } 246 else { 247 this.minStartIndex = index; 248 } 249 250 if (this.maxStartIndex >= 0) { 251 long maxStart = getDataItem(this.maxStartIndex).getPeriod() 252 .getStart().getTime(); 253 if (start > maxStart) { 254 this.maxStartIndex = index; 255 } 256 } 257 else { 258 this.maxStartIndex = index; 259 } 260 261 if (this.minMiddleIndex >= 0) { 262 long s = getDataItem(this.minMiddleIndex).getPeriod().getStart() 263 .getTime(); 264 long e = getDataItem(this.minMiddleIndex).getPeriod().getEnd() 265 .getTime(); 266 long minMiddle = s + (e - s) / 2; 267 if (middle < minMiddle) { 268 this.minMiddleIndex = index; 269 } 270 } 271 else { 272 this.minMiddleIndex = index; 273 } 274 275 if (this.maxMiddleIndex >= 0) { 276 long s = getDataItem(this.minMiddleIndex).getPeriod().getStart() 277 .getTime(); 278 long e = getDataItem(this.minMiddleIndex).getPeriod().getEnd() 279 .getTime(); 280 long maxMiddle = s + (e - s) / 2; 281 if (middle > maxMiddle) { 282 this.maxMiddleIndex = index; 283 } 284 } 285 else { 286 this.maxMiddleIndex = index; 287 } 288 289 if (this.minEndIndex >= 0) { 290 long minEnd = getDataItem(this.minEndIndex).getPeriod().getEnd() 291 .getTime(); 292 if (end < minEnd) { 293 this.minEndIndex = index; 294 } 295 } 296 else { 297 this.minEndIndex = index; 298 } 299 300 if (this.maxEndIndex >= 0) { 301 long maxEnd = getDataItem(this.maxEndIndex).getPeriod().getEnd() 302 .getTime(); 303 if (end > maxEnd) { 304 this.maxEndIndex = index; 305 } 306 } 307 else { 308 this.maxEndIndex = index; 309 } 310 311 } 312 313 316 private void recalculateBounds() { 317 this.minStartIndex = -1; 318 this.minMiddleIndex = -1; 319 this.minEndIndex = -1; 320 this.maxStartIndex = -1; 321 this.maxMiddleIndex = -1; 322 this.maxEndIndex = -1; 323 for (int i = 0; i < this.data.size(); i++) { 324 TimePeriodValue tpv = (TimePeriodValue) this.data.get(i); 325 updateBounds(tpv.getPeriod(), i); 326 } 327 } 328 329 335 public void add(TimePeriod period, double value) { 336 TimePeriodValue item = new TimePeriodValue(period, value); 337 add(item); 338 } 339 340 346 public void add(TimePeriod period, Number value) { 347 TimePeriodValue item = new TimePeriodValue(period, value); 348 add(item); 349 } 350 351 357 public void update(int index, Number value) { 358 TimePeriodValue item = getDataItem(index); 359 item.setValue(value); 360 fireSeriesChanged(); 361 } 362 363 369 public void delete(int start, int end) { 370 for (int i = 0; i <= (end - start); i++) { 371 this.data.remove(start); 372 } 373 recalculateBounds(); 374 fireSeriesChanged(); 375 } 376 377 384 public boolean equals(Object obj) { 385 386 if (obj == this) { 387 return true; 388 } 389 390 if (!(obj instanceof TimePeriodValues)) { 391 return false; 392 } 393 394 if (!super.equals(obj)) { 395 return false; 396 } 397 398 TimePeriodValues that = (TimePeriodValues) obj; 399 if (!getDomainDescription().equals(that.getDomainDescription())) { 400 return false; 401 } 402 if (!getRangeDescription().equals(that.getRangeDescription())) { 403 return false; 404 } 405 406 int count = getItemCount(); 407 if (count != that.getItemCount()) { 408 return false; 409 } 410 for (int i = 0; i < count; i++) { 411 if (!getDataItem(i).equals(that.getDataItem(i))) { 412 return false; 413 } 414 } 415 return true; 416 417 } 418 419 424 public int hashCode() { 425 int result; 426 result = (this.domain != null ? this.domain.hashCode() : 0); 427 result = 29 * result + (this.range != null ? this.range.hashCode() : 0); 428 result = 29 * result + this.data.hashCode(); 429 result = 29 * result + this.minStartIndex; 430 result = 29 * result + this.maxStartIndex; 431 result = 29 * result + this.minMiddleIndex; 432 result = 29 * result + this.maxMiddleIndex; 433 result = 29 * result + this.minEndIndex; 434 result = 29 * result + this.maxEndIndex; 435 return result; 436 } 437 438 453 public Object clone() throws CloneNotSupportedException { 454 Object clone = createCopy(0, getItemCount() - 1); 455 return clone; 456 } 457 458 469 public TimePeriodValues createCopy(int start, int end) 470 throws CloneNotSupportedException { 471 472 TimePeriodValues copy = (TimePeriodValues) super.clone(); 473 474 copy.data = new ArrayList (); 475 if (this.data.size() > 0) { 476 for (int index = start; index <= end; index++) { 477 TimePeriodValue item = (TimePeriodValue) this.data.get(index); 478 TimePeriodValue clone = (TimePeriodValue) item.clone(); 479 try { 480 copy.add(clone); 481 } 482 catch (SeriesException e) { 483 System.err.println("Failed to add cloned item."); 484 } 485 } 486 } 487 return copy; 488 489 } 490 491 496 public int getMinStartIndex() { 497 return this.minStartIndex; 498 } 499 500 505 public int getMaxStartIndex() { 506 return this.maxStartIndex; 507 } 508 509 515 public int getMinMiddleIndex() { 516 return this.minMiddleIndex; 517 } 518 519 525 public int getMaxMiddleIndex() { 526 return this.maxMiddleIndex; 527 } 528 529 534 public int getMinEndIndex() { 535 return this.minEndIndex; 536 } 537 538 543 public int getMaxEndIndex() { 544 return this.maxEndIndex; 545 } 546 547 } 548 | Popular Tags |