1 50 51 package org.jfree.data.category; 52 53 import java.util.Collections ; 54 import java.util.List ; 55 56 import org.jfree.data.general.AbstractDataset; 57 import org.jfree.data.general.DatasetChangeEvent; 58 import org.jfree.data.general.DatasetChangeListener; 59 import org.jfree.data.general.PieDataset; 60 import org.jfree.util.TableOrder; 61 62 66 public class CategoryToPieDataset extends AbstractDataset 67 implements PieDataset, DatasetChangeListener { 68 69 static final long serialVersionUID = 5516396319762189617L; 70 71 72 private CategoryDataset source; 73 74 75 private TableOrder extract; 76 77 78 private int index; 79 80 92 public CategoryToPieDataset(CategoryDataset source, 93 TableOrder extract, 94 int index) { 95 if (extract == null) { 96 throw new IllegalArgumentException ("Null 'extract' argument."); 97 } 98 this.source = source; 99 if (this.source != null) { 100 this.source.addChangeListener(this); 101 } 102 this.extract = extract; 103 this.index = index; 104 } 105 106 113 public CategoryDataset getUnderlyingDataset() { 114 return this.source; 115 } 116 117 125 public TableOrder getExtractType() { 126 return this.extract; 127 } 128 129 136 public int getExtractIndex() { 137 return this.index; 138 } 139 140 146 public int getItemCount() { 147 int result = 0; 148 if (this.source != null) { 149 if (this.extract == TableOrder.BY_ROW) { 150 result = this.source.getColumnCount(); 151 } 152 else if (this.extract == TableOrder.BY_COLUMN) { 153 result = this.source.getRowCount(); 154 } 155 } 156 return result; 157 } 158 159 169 public Number getValue(int item) { 170 Number result = null; 171 if (item < 0 || item >= getItemCount()) { 172 throw new IndexOutOfBoundsException ( 174 "The 'item' index is out of bounds."); 175 } 176 if (this.extract == TableOrder.BY_ROW) { 177 result = this.source.getValue(this.index, item); 178 } 179 else if (this.extract == TableOrder.BY_COLUMN) { 180 result = this.source.getValue(item, this.index); 181 } 182 return result; 183 } 184 185 196 public Comparable getKey(int index) { 197 Comparable result = null; 198 if (index < 0 || index >= getItemCount()) { 199 throw new IndexOutOfBoundsException ("Invalid 'index': " + index); 201 } 202 if (this.extract == TableOrder.BY_ROW) { 203 result = this.source.getColumnKey(index); 204 } 205 else if (this.extract == TableOrder.BY_COLUMN) { 206 result = this.source.getRowKey(index); 207 } 208 return result; 209 } 210 211 219 public int getIndex(Comparable key) { 220 int result = -1; 221 if (this.source != null) { 222 if (this.extract == TableOrder.BY_ROW) { 223 result = this.source.getColumnIndex(key); 224 } 225 else if (this.extract == TableOrder.BY_COLUMN) { 226 result = this.source.getRowIndex(key); 227 } 228 } 229 return result; 230 } 231 232 240 public List getKeys() { 241 List result = Collections.EMPTY_LIST; 242 if (this.source != null) { 243 if (this.extract == TableOrder.BY_ROW) { 244 result = this.source.getColumnKeys(); 245 } 246 else if (this.extract == TableOrder.BY_COLUMN) { 247 result = this.source.getRowKeys(); 248 } 249 } 250 return result; 251 } 252 253 262 public Number getValue(Comparable key) { 263 Number result = null; 264 int keyIndex = getIndex(key); 265 if (keyIndex != -1) { 266 if (this.extract == TableOrder.BY_ROW) { 267 result = this.source.getValue(this.index, keyIndex); 268 } 269 else if (this.extract == TableOrder.BY_COLUMN) { 270 result = this.source.getValue(keyIndex, this.index); 271 } 272 } 273 return result; 274 } 275 276 283 public void datasetChanged(DatasetChangeEvent event) { 284 fireDatasetChanged(); 285 } 286 287 296 public boolean equals(Object obj) { 297 if (obj == this) { 298 return true; 299 } 300 if (!(obj instanceof PieDataset)) { 301 return false; 302 } 303 PieDataset that = (PieDataset) obj; 304 int count = getItemCount(); 305 if (that.getItemCount() != count) { 306 return false; 307 } 308 for (int i = 0; i < count; i++) { 309 Comparable k1 = getKey(i); 310 Comparable k2 = that.getKey(i); 311 if (!k1.equals(k2)) { 312 return false; 313 } 314 315 Number v1 = getValue(i); 316 Number v2 = that.getValue(i); 317 if (v1 == null) { 318 if (v2 != null) { 319 return false; 320 } 321 } 322 else { 323 if (!v1.equals(v2)) { 324 return false; 325 } 326 } 327 } 328 return true; 329 } 330 331 } 332 | Popular Tags |