1 57 58 package org.jfree.data.general; 59 60 import java.io.Serializable ; 61 import java.util.Collections ; 62 import java.util.List ; 63 64 import org.jfree.data.DefaultKeyedValues; 65 import org.jfree.data.KeyedValues; 66 import org.jfree.data.UnknownKeyException; 67 import org.jfree.util.PublicCloneable; 68 import org.jfree.util.SortOrder; 69 70 73 public class DefaultPieDataset extends AbstractDataset 74 implements PieDataset, 75 Cloneable , PublicCloneable, 76 Serializable { 77 78 79 private static final long serialVersionUID = 2904745139106540618L; 80 81 82 private DefaultKeyedValues data; 83 84 87 public DefaultPieDataset() { 88 this.data = new DefaultKeyedValues(); 89 } 90 91 97 public DefaultPieDataset(KeyedValues data) { 98 if (data == null) { 99 throw new IllegalArgumentException ("Null 'data' argument."); 100 } 101 this.data = new DefaultKeyedValues(); 102 for (int i = 0; i < data.getItemCount(); i++) { 103 this.data.addValue(data.getKey(i), data.getValue(i)); 104 } 105 } 106 107 112 public int getItemCount() { 113 return this.data.getItemCount(); 114 } 115 116 122 public List getKeys() { 123 return Collections.unmodifiableList(this.data.getKeys()); 124 } 125 126 137 public Comparable getKey(int item) { 138 return this.data.getKey(item); 139 } 140 141 151 public int getIndex(Comparable key) { 152 return this.data.getIndex(key); 153 } 154 155 162 public Number getValue(int item) { 163 164 Number result = null; 165 if (getItemCount() > item) { 166 result = this.data.getValue(item); 167 } 168 return result; 169 170 } 171 172 181 public Number getValue(Comparable key) { 182 if (key == null) { 183 throw new IllegalArgumentException ("Null 'key' argument."); 184 } 185 return this.data.getValue(key); 186 } 187 188 198 public void setValue(Comparable key, Number value) { 199 this.data.setValue(key, value); 200 fireDatasetChanged(); 201 } 202 203 213 public void setValue(Comparable key, double value) { 214 setValue(key, new Double (value)); 215 } 216 217 226 public void remove(Comparable key) { 227 this.data.removeValue(key); 228 fireDatasetChanged(); 229 } 230 231 237 public void clear() { 238 if (getItemCount() > 0) { 239 this.data.clear(); 240 fireDatasetChanged(); 241 } 242 } 243 244 252 public void sortByKeys(SortOrder order) { 253 this.data.sortByKeys(order); 254 fireDatasetChanged(); 255 } 256 257 265 public void sortByValues(SortOrder order) { 266 this.data.sortByValues(order); 267 fireDatasetChanged(); 268 } 269 270 277 public boolean equals(Object obj) { 278 if (obj == this) { 279 return true; 280 } 281 282 if (!(obj instanceof PieDataset)) { 283 return false; 284 } 285 PieDataset that = (PieDataset) obj; 286 int count = getItemCount(); 287 if (that.getItemCount() != count) { 288 return false; 289 } 290 291 for (int i = 0; i < count; i++) { 292 Comparable k1 = getKey(i); 293 Comparable k2 = that.getKey(i); 294 if (!k1.equals(k2)) { 295 return false; 296 } 297 298 Number v1 = getValue(i); 299 Number v2 = that.getValue(i); 300 if (v1 == null) { 301 if (v2 != null) { 302 return false; 303 } 304 } 305 else { 306 if (!v1.equals(v2)) { 307 return false; 308 } 309 } 310 } 311 return true; 312 313 } 314 315 320 public int hashCode() { 321 return this.data.hashCode(); 322 } 323 324 332 public Object clone() throws CloneNotSupportedException { 333 DefaultPieDataset clone = (DefaultPieDataset) super.clone(); 334 clone.data = (DefaultKeyedValues) this.data.clone(); 335 return clone; 336 } 337 338 } 339 | Popular Tags |