1 38 39 package org.jfree.data; 40 41 import java.util.ArrayList ; 42 import java.util.Arrays ; 43 import java.util.Collections ; 44 import java.util.List ; 45 import java.util.ResourceBundle ; 46 47 56 public class DefaultIntervalCategoryDataset extends AbstractSeriesDataset 57 implements IntervalCategoryDataset { 58 59 60 private Comparable [] seriesKeys; 61 62 63 private Comparable [] categoryKeys; 64 65 66 private Number [][] startData; 67 68 69 private Number [][] endData; 70 71 77 public DefaultIntervalCategoryDataset(double[][] starts, double[][] ends) { 78 79 this(DatasetUtilities.createNumberArray2D(starts), 80 DatasetUtilities.createNumberArray2D(ends)); 81 82 } 83 84 94 public DefaultIntervalCategoryDataset(Number [][] starts, Number [][] ends) { 95 96 this(null, null, starts, ends); 97 98 } 99 100 111 public DefaultIntervalCategoryDataset(String [] seriesNames, 112 Number [][] starts, 113 Number [][] ends) { 114 115 this(seriesNames, null, starts, ends); 116 117 } 118 119 129 public DefaultIntervalCategoryDataset(Comparable [] seriesKeys, 130 Comparable [] categoryKeys, 131 Number [][] starts, 132 Number [][] ends) { 133 134 this.startData = starts; 135 this.endData = ends; 136 137 if (starts != null && ends != null) { 138 139 String baseName = "org.jfree.data.resources.DataPackageResources"; 140 ResourceBundle resources = ResourceBundle.getBundle(baseName); 141 142 int seriesCount = starts.length; 143 if (seriesCount != ends.length) { 144 String errMsg = "DefaultIntervalCategoryDataset: the number " 145 + "of series in the start value dataset does " 146 + "not match the number of series in the end " 147 + "value dataset."; 148 throw new IllegalArgumentException (errMsg); 149 } 150 if (seriesCount > 0) { 151 152 if (seriesKeys != null) { 154 155 if (seriesKeys.length != seriesCount) { 156 throw new IllegalArgumentException ( 157 "DefaultIntervalCategoryDataset: the number of series keys does " 158 + "not match the number of series in the data."); 159 } 160 161 this.seriesKeys = seriesKeys; 162 } 163 else { 164 String prefix = resources.getString("series.default-prefix") + " "; 165 this.seriesKeys = generateKeys(seriesCount, prefix); 166 } 167 168 int categoryCount = starts[0].length; 170 if (categoryCount != ends[0].length) { 171 String errMsg = "DefaultIntervalCategoryDataset: the " 172 + "number of categories in the start value " 173 + "dataset does not match the number of " 174 + "categories in the end value dataset."; 175 throw new IllegalArgumentException (errMsg); 176 } 177 if (categoryKeys != null) { 178 if (categoryKeys.length != categoryCount) { 179 throw new IllegalArgumentException ( 180 "DefaultIntervalCategoryDataset: the number of category keys does " 181 + "not match the number of categories in the data."); 182 } 183 this.categoryKeys = categoryKeys; 184 } 185 else { 186 String prefix = resources.getString("categories.default-prefix") + " "; 187 this.categoryKeys = generateKeys(categoryCount, prefix); 188 } 189 190 } 191 else { 192 this.seriesKeys = null; 193 this.categoryKeys = null; 194 } 195 } 196 197 } 198 199 204 public int getSeriesCount() { 205 206 int result = 0; 207 if (startData != null) { 208 result = startData.length; 209 } 210 return result; 211 212 } 213 214 219 public int getItemCount() { 220 return this.categoryKeys.length; 221 } 222 223 230 public Comparable getCategory(int item) { 231 return this.categoryKeys[item]; 232 } 233 234 241 public int getItem(Object category) { 242 List categories = getCategories(); 243 return categories.indexOf(category); 244 } 245 246 253 public int getSeriesIndex(Object series) { 254 List seriesKeys = getSeries(); 255 return seriesKeys.indexOf(series); 256 } 257 258 265 public Comparable getSeries(int series) { 266 267 if ((series >= getSeriesCount()) || (series < 0)) { 269 throw new IllegalArgumentException ( 270 "DefaultCategoryDataset.getSeriesName(int): no such series."); 271 } 272 273 return seriesKeys[series]; 275 276 } 277 278 285 public String getSeriesName(int series) { 286 287 if ((series >= getSeriesCount()) || (series < 0)) { 289 290 throw new IllegalArgumentException ( 291 "DefaultIntervalCategoryDataset.getSeriesName(int): no such series."); 292 } 293 294 return seriesKeys[series].toString(); 296 297 } 298 299 304 public void setSeriesKeys(Comparable [] seriesKeys) { 305 306 if (seriesKeys == null) { 308 throw new IllegalArgumentException ( 309 "DefaultIntervalCategoryDataset.setSeriesKeys(): null not permitted."); 310 } 311 312 if (seriesKeys.length != getSeriesCount()) { 313 throw new IllegalArgumentException ( 314 "DefaultIntervalCategoryDataset.setSeriesKeys(): " 315 + "the number of series keys does not match the data."); 316 } 317 318 this.seriesKeys = seriesKeys; 320 fireDatasetChanged(); 321 322 } 323 324 331 public int getCategoryCount() { 332 333 int result = 0; 334 335 if (startData != null) { 336 if (getSeriesCount() > 0) { 337 result = startData[0].length; 338 } 339 } 340 341 return result; 342 343 } 344 345 352 public List getSeries() { 353 354 if (this.seriesKeys == null) { 357 return new java.util.ArrayList (); 358 } 359 else { 360 return Collections.unmodifiableList(Arrays.asList(this.seriesKeys)); 361 } 362 363 } 364 365 372 public List getCategories() { 373 return getColumnKeys(); 374 } 375 376 383 public List getColumnKeys() { 384 385 if (this.categoryKeys == null) { 388 return new ArrayList (); 389 } 390 else { 391 return Collections.unmodifiableList(Arrays.asList(this.categoryKeys)); 392 } 393 394 } 395 396 401 public void setCategoryKeys(Comparable [] categoryKeys) { 402 403 if (categoryKeys == null) { 405 throw new IllegalArgumentException ( 406 "DefaultIntervalCategoryDataset.setCategories(...): null not permitted."); 407 } 408 409 if (categoryKeys.length != startData[0].length) { 410 throw new IllegalArgumentException ( 411 "DefaultIntervalCategoryDataset.setCategoryKeys(...): " 412 + "the number of categories does not match the data."); 413 } 414 415 for (int i = 0; i < categoryKeys.length; i++) { 416 if (categoryKeys[i] == null) { 417 throw new IllegalArgumentException ( 418 "DefaultIntervalCategoryDataset.setCategoryKeys(...): " 419 + "null category not permitted."); 420 } 421 } 422 423 this.categoryKeys = categoryKeys; 425 fireDatasetChanged(); 426 427 } 428 429 438 public Number getValue(Comparable series, Comparable category) { 439 int seriesIndex = getSeriesIndex(series); 440 int itemIndex = getItem(category); 441 return getValue(seriesIndex, itemIndex); 442 } 443 444 455 public Number getValue(int series, int category) { 456 457 return getEndValue(series, category); 458 459 } 460 461 471 public Number getStartValue(Comparable series, Comparable category) { 472 int seriesIndex = getSeriesIndex(series); 473 int itemIndex = getItem(category); 474 return getStartValue(seriesIndex, itemIndex); 475 } 476 477 487 public Number getStartValue(int series, int category) { 488 489 if ((series < 0) || (series >= getSeriesCount())) { 491 throw new IllegalArgumentException ( 492 "DefaultIntervalCategoryDataset.getValue(...): " 493 + "series index out of range."); 494 } 495 496 if ((category < 0) || (category >= getCategoryCount())) { 497 throw new IllegalArgumentException ( 498 "DefaultIntervalCategoryDataset.getValue(...): " 499 + "category index out of range."); 500 } 501 502 return startData[series][category]; 504 505 } 506 507 517 public Number getEndValue(Comparable series, Comparable category) { 518 int seriesIndex = getSeriesIndex(series); 519 int itemIndex = getItem(category); 520 return getEndValue(seriesIndex, itemIndex); 521 } 522 523 533 public Number getEndValue(int series, int category) { 534 535 if ((series < 0) || (series >= getSeriesCount())) { 537 throw new IllegalArgumentException ( 538 "DefaultIntervalCategoryDataset.getValue(...): " 539 + "series index out of range."); 540 } 541 542 if ((category < 0) || (category >= getCategoryCount())) { 543 throw new IllegalArgumentException ( 544 "DefaultIntervalCategoryDataset.getValue(...): " 545 + "category index out of range."); 546 } 547 548 return endData[series][category]; 550 551 } 552 553 559 public void setStartValue(int series, Object category, Number value) { 560 561 if ((series < 0) || (series > getSeriesCount())) { 563 throw new IllegalArgumentException ( 564 "DefaultIntervalCategoryDataset.setValue: " 565 + "series outside valid range."); 566 } 567 568 int categoryIndex = getCategoryIndex(category); 570 if (categoryIndex < 0) { 571 throw new IllegalArgumentException ( 572 "DefaultIntervalCategoryDataset.setValue: " 573 + "unrecognised category."); 574 } 575 576 startData[series][categoryIndex] = value; 578 fireDatasetChanged(); 579 580 } 581 582 590 public void setEndValue(int series, Object category, Number value) { 591 592 if ((series < 0) || (series > getSeriesCount())) { 594 throw new IllegalArgumentException ( 595 "DefaultIntervalCategoryDataset.setValue: " 596 + "series outside valid range."); 597 } 598 599 int categoryIndex = getCategoryIndex(category); 601 if (categoryIndex < 0) { 602 throw new IllegalArgumentException ( 603 "DefaultIntervalCategoryDataset.setValue: " 604 + "unrecognised category."); 605 } 606 607 endData[series][categoryIndex] = value; 609 fireDatasetChanged(); 610 611 } 612 613 620 private int getCategoryIndex(Object category) { 621 622 int result = -1; 623 for (int i = 0; i < this.categoryKeys.length; i++) { 624 if (category.equals(this.categoryKeys[i])) { 625 result = i; 626 break; 627 } 628 } 629 return result; 630 631 } 632 633 642 private Comparable [] generateKeys(int count, String prefix) { 643 644 Comparable [] result = new Comparable [count]; 645 String name; 646 for (int i = 0; i < count; i++) { 647 name = prefix + (i + 1); 648 result[i] = name; 649 } 650 return result; 651 652 } 653 654 661 public Comparable getColumnKey(int item) { 662 return this.categoryKeys[item]; 663 } 664 665 672 public int getColumnIndex(Comparable columnKey) { 673 List categories = getCategories(); 674 return categories.indexOf(columnKey); 675 } 676 677 684 public int getRowIndex(Comparable rowKey) { 685 List seriesKeys = getSeries(); 686 return seriesKeys.indexOf(rowKey); 687 } 688 689 696 public List getRowKeys() { 697 698 if (this.seriesKeys == null) { 701 return new java.util.ArrayList (); 702 } 703 else { 704 return Collections.unmodifiableList(Arrays.asList(this.seriesKeys)); 705 } 706 707 } 708 709 716 public Comparable getRowKey(int series) { 717 718 if ((series >= getSeriesCount()) || (series < 0)) { 720 throw new IllegalArgumentException ( 721 "DefaultCategoryDataset.getSeriesName(int): no such series."); 722 } 723 724 return seriesKeys[series]; 726 727 } 728 729 736 public int getColumnCount() { 737 738 int result = 0; 739 740 if (startData != null) { 741 if (getSeriesCount() > 0) { 742 result = startData[0].length; 743 } 744 } 745 746 return result; 747 748 } 749 750 755 public int getRowCount() { 756 757 int result = 0; 758 if (startData != null) { 759 result = startData.length; 760 } 761 return result; 762 763 } 764 765 } 766 | Popular Tags |