1 43 44 package org.jfree.data.category; 45 46 import java.util.ArrayList ; 47 import java.util.Arrays ; 48 import java.util.Collections ; 49 import java.util.List ; 50 import java.util.ResourceBundle ; 51 52 import org.jfree.data.DataUtilities; 53 import org.jfree.data.general.AbstractSeriesDataset; 54 55 64 public class DefaultIntervalCategoryDataset extends AbstractSeriesDataset 65 implements IntervalCategoryDataset { 66 67 68 private Comparable [] seriesKeys; 69 70 71 private Comparable [] categoryKeys; 72 73 74 private Number [][] startData; 75 76 77 private Number [][] endData; 78 79 85 public DefaultIntervalCategoryDataset(double[][] starts, double[][] ends) { 86 this( 87 DataUtilities.createNumberArray2D(starts), 88 DataUtilities.createNumberArray2D(ends) 89 ); 90 } 91 92 103 public DefaultIntervalCategoryDataset(Number [][] starts, Number [][] ends) { 104 this(null, null, starts, ends); 105 } 106 107 118 public DefaultIntervalCategoryDataset(String [] seriesNames, 119 Number [][] starts, 120 Number [][] ends) { 121 122 this(seriesNames, null, starts, ends); 123 124 } 125 126 136 public DefaultIntervalCategoryDataset(Comparable [] seriesKeys, 137 Comparable [] categoryKeys, 138 Number [][] starts, 139 Number [][] ends) { 140 141 this.startData = starts; 142 this.endData = ends; 143 144 if (starts != null && ends != null) { 145 146 String baseName = "org.jfree.data.resources.DataPackageResources"; 147 ResourceBundle resources = ResourceBundle.getBundle(baseName); 148 149 int seriesCount = starts.length; 150 if (seriesCount != ends.length) { 151 String errMsg = "DefaultIntervalCategoryDataset: the number " 152 + "of series in the start value dataset does " 153 + "not match the number of series in the end " 154 + "value dataset."; 155 throw new IllegalArgumentException (errMsg); 156 } 157 if (seriesCount > 0) { 158 159 if (seriesKeys != null) { 161 162 if (seriesKeys.length != seriesCount) { 163 throw new IllegalArgumentException ( 164 "The number of series keys does " 165 + "not match the number of series in the data." 166 ); 167 } 168 169 this.seriesKeys = seriesKeys; 170 } 171 else { 172 String prefix 173 = resources.getString("series.default-prefix") + " "; 174 this.seriesKeys = generateKeys(seriesCount, prefix); 175 } 176 177 int categoryCount = starts[0].length; 179 if (categoryCount != ends[0].length) { 180 String errMsg = "DefaultIntervalCategoryDataset: the " 181 + "number of categories in the start value " 182 + "dataset does not match the number of " 183 + "categories in the end value dataset."; 184 throw new IllegalArgumentException (errMsg); 185 } 186 if (categoryKeys != null) { 187 if (categoryKeys.length != categoryCount) { 188 throw new IllegalArgumentException ( 189 "The number of category keys does " 190 + "not match the number of categories in the data." 191 ); 192 } 193 this.categoryKeys = categoryKeys; 194 } 195 else { 196 String prefix = resources.getString( 197 "categories.default-prefix" 198 ) + " "; 199 this.categoryKeys = generateKeys(categoryCount, prefix); 200 } 201 202 } 203 else { 204 this.seriesKeys = null; 205 this.categoryKeys = null; 206 } 207 } 208 209 } 210 211 216 public int getSeriesCount() { 217 int result = 0; 218 if (this.startData != null) { 219 result = this.startData.length; 220 } 221 return result; 222 } 223 224 229 public int getItemCount() { 230 return this.categoryKeys.length; 231 } 232 233 240 public int getSeriesIndex(Comparable series) { 241 List seriesKeys = getSeries(); 242 return seriesKeys.indexOf(series); 243 } 244 245 252 public Comparable getSeriesKey(int series) { 253 if ((series >= getSeriesCount()) || (series < 0)) { 254 throw new IllegalArgumentException ("No such series : " + series); 255 } 256 return this.seriesKeys[series]; 257 } 258 259 264 public void setSeriesKeys(Comparable [] seriesKeys) { 265 266 if (seriesKeys == null) { 268 throw new IllegalArgumentException ("Null 'seriesKeys' argument."); 269 } 270 271 if (seriesKeys.length != getSeriesCount()) { 272 throw new IllegalArgumentException ( 273 "DefaultIntervalCategoryDataset.setSeriesKeys(): " 274 + "the number of series keys does not match the data." 275 ); 276 } 277 278 this.seriesKeys = seriesKeys; 280 fireDatasetChanged(); 281 282 } 283 284 291 public int getCategoryCount() { 292 int result = 0; 293 if (this.startData != null) { 294 if (getSeriesCount() > 0) { 295 result = this.startData[0].length; 296 } 297 } 298 return result; 299 } 300 301 308 public List getSeries() { 309 310 if (this.seriesKeys == null) { 313 return new java.util.ArrayList (); 314 } 315 else { 316 return Collections.unmodifiableList(Arrays.asList(this.seriesKeys)); 317 } 318 319 } 320 321 328 public List getCategories() { 329 return getColumnKeys(); 330 } 331 332 339 public List getColumnKeys() { 340 341 if (this.categoryKeys == null) { 344 return new ArrayList (); 345 } 346 else { 347 return Collections.unmodifiableList( 348 Arrays.asList(this.categoryKeys) 349 ); 350 } 351 352 } 353 354 360 public void setCategoryKeys(Comparable [] categoryKeys) { 361 362 if (categoryKeys == null) { 364 throw new IllegalArgumentException ("Null 'categoryKeys' argument."); 365 } 366 367 if (categoryKeys.length != this.startData[0].length) { 368 throw new IllegalArgumentException ( 369 "The number of categories does not match the data." 370 ); 371 } 372 373 for (int i = 0; i < categoryKeys.length; i++) { 374 if (categoryKeys[i] == null) { 375 throw new IllegalArgumentException ( 376 "DefaultIntervalCategoryDataset.setCategoryKeys(): " 377 + "null category not permitted."); 378 } 379 } 380 381 this.categoryKeys = categoryKeys; 383 fireDatasetChanged(); 384 385 } 386 387 396 public Number getValue(Comparable series, Comparable category) { 397 int seriesIndex = getSeriesIndex(series); 398 int itemIndex = getColumnIndex(category); 399 return getValue(seriesIndex, itemIndex); 400 } 401 402 413 public Number getValue(int series, int category) { 414 return getEndValue(series, category); 415 } 416 417 426 public Number getStartValue(Comparable series, Comparable category) { 427 int seriesIndex = getSeriesIndex(series); 428 int itemIndex = getColumnIndex(category); 429 return getStartValue(seriesIndex, itemIndex); 430 } 431 432 441 public Number getStartValue(int series, int category) { 442 443 if ((series < 0) || (series >= getSeriesCount())) { 445 throw new IllegalArgumentException ( 446 "DefaultIntervalCategoryDataset.getValue(): " 447 + "series index out of range."); 448 } 449 450 if ((category < 0) || (category >= getCategoryCount())) { 451 throw new IllegalArgumentException ( 452 "DefaultIntervalCategoryDataset.getValue(): " 453 + "category index out of range."); 454 } 455 456 return this.startData[series][category]; 458 459 } 460 461 469 public Number getEndValue(Comparable series, Comparable category) { 470 int seriesIndex = getSeriesIndex(series); 471 int itemIndex = getColumnIndex(category); 472 return getEndValue(seriesIndex, itemIndex); 473 } 474 475 483 public Number getEndValue(int series, int category) { 484 485 if ((series < 0) || (series >= getSeriesCount())) { 487 throw new IllegalArgumentException ( 488 "DefaultIntervalCategoryDataset.getValue(): " 489 + "series index out of range."); 490 } 491 492 if ((category < 0) || (category >= getCategoryCount())) { 493 throw new IllegalArgumentException ( 494 "DefaultIntervalCategoryDataset.getValue(): " 495 + "category index out of range."); 496 } 497 498 return this.endData[series][category]; 500 501 } 502 503 511 public void setStartValue(int series, Comparable category, Number value) { 512 513 if ((series < 0) || (series > getSeriesCount())) { 515 throw new IllegalArgumentException ( 516 "DefaultIntervalCategoryDataset.setValue: " 517 + "series outside valid range."); 518 } 519 520 int categoryIndex = getCategoryIndex(category); 522 if (categoryIndex < 0) { 523 throw new IllegalArgumentException ( 524 "DefaultIntervalCategoryDataset.setValue: " 525 + "unrecognised category."); 526 } 527 528 this.startData[series][categoryIndex] = value; 530 fireDatasetChanged(); 531 532 } 533 534 542 public void setEndValue(int series, Comparable category, Number value) { 543 544 if ((series < 0) || (series > getSeriesCount())) { 546 throw new IllegalArgumentException ( 547 "DefaultIntervalCategoryDataset.setValue: " 548 + "series outside valid range."); 549 } 550 551 int categoryIndex = getCategoryIndex(category); 553 if (categoryIndex < 0) { 554 throw new IllegalArgumentException ( 555 "DefaultIntervalCategoryDataset.setValue: " 556 + "unrecognised category."); 557 } 558 559 this.endData[series][categoryIndex] = value; 561 fireDatasetChanged(); 562 563 } 564 565 572 private int getCategoryIndex(Comparable category) { 573 int result = -1; 574 for (int i = 0; i < this.categoryKeys.length; i++) { 575 if (category.equals(this.categoryKeys[i])) { 576 result = i; 577 break; 578 } 579 } 580 return result; 581 } 582 583 592 private Comparable [] generateKeys(int count, String prefix) { 593 Comparable [] result = new Comparable [count]; 594 String name; 595 for (int i = 0; i < count; i++) { 596 name = prefix + (i + 1); 597 result[i] = name; 598 } 599 return result; 600 } 601 602 609 public Comparable getColumnKey(int column) { 610 return this.categoryKeys[column]; 611 } 612 613 620 public int getColumnIndex(Comparable columnKey) { 621 List categories = getCategories(); 622 return categories.indexOf(columnKey); 623 } 624 625 632 public int getRowIndex(Comparable rowKey) { 633 List seriesKeys = getSeries(); 634 return seriesKeys.indexOf(rowKey); 635 } 636 637 644 public List getRowKeys() { 645 if (this.seriesKeys == null) { 648 return new java.util.ArrayList (); 649 } 650 else { 651 return Collections.unmodifiableList(Arrays.asList(this.seriesKeys)); 652 } 653 } 654 655 662 public Comparable getRowKey(int row) { 663 if ((row >= getRowCount()) || (row < 0)) { 664 throw new IllegalArgumentException ( 665 "The 'row' argument is out of bounds."); 666 } 667 return this.seriesKeys[row]; 668 } 669 670 676 public int getColumnCount() { 677 int result = 0; 678 if (this.startData != null) { 679 if (getSeriesCount() > 0) { 680 result = this.startData[0].length; 681 } 682 } 683 return result; 684 } 685 686 691 public int getRowCount() { 692 int result = 0; 693 if (this.startData != null) { 694 result = this.startData.length; 695 } 696 return result; 697 } 698 699 } 700 | Popular Tags |