Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors. 6: * 7: * Project Info: http://www.jfree.org/jfreechart/index.html 8: * 9: * This library is free software; you can redistribute it and/or modify it 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ---------------------- 28: * DefaultPieDataset.java 29: * ---------------------- 30: * (C) Copyright 2001-2006, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): Sam (oldman); 34: * 35: * $Id: DefaultPieDataset.java,v 1.6.2.4 2006/08/01 14:35:38 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 17-Nov-2001 : Version 1 (DG); 40: * 22-Jan-2002 : Removed legend methods from dataset implementations (DG); 41: * 07-Apr-2002 : Modified implementation to guarantee data sequence to remain 42: * in the order categories are added (oldman); 43: * 23-Oct-2002 : Added getCategory(int) method and getItemCount() method, in 44: * line with changes to the PieDataset interface (DG); 45: * 04-Feb-2003 : Changed underlying data storage to DefaultKeyedValues (DG); 46: * 04-Mar-2003 : Inserted DefaultKeyedValuesDataset class into hierarchy (DG); 47: * 24-Apr-2003 : Switched places with DefaultKeyedValuesDataset (DG); 48: * 18-Aug-2003 : Implemented Cloneable (DG); 49: * 03-Mar-2005 : Implemented PublicCloneable (DG); 50: * 29-Jun-2005 : Added remove() method (DG); 51: * ------------- JFREECHART 1.0.0 --------------------------------------------- 52: * 31-Jul-2006 : Added a clear() method to clear all values from the 53: * dataset (DG); 54: * 55: */ 56: 57: package org.jfree.data.general; 58: 59: import java.io.Serializable; 60: import java.util.Collections; 61: import java.util.List; 62: 63: import org.jfree.data.DefaultKeyedValues; 64: import org.jfree.data.KeyedValues; 65: import org.jfree.data.UnknownKeyException; 66: import org.jfree.util.PublicCloneable; 67: 68: /** 69: * A default implementation of the {@link PieDataset} interface. 70: */ 71: public class DefaultPieDataset extends AbstractDataset 72: implements PieDataset, 73: Cloneable, PublicCloneable, 74: Serializable { 75: 76: /** For serialization. */ 77: private static final long serialVersionUID = 2904745139106540618L; 78: 79: /** Storage for the data. */ 80: private DefaultKeyedValues data; 81: 82: /** 83: * Constructs a new dataset, initially empty. 84: */ 85: public DefaultPieDataset() { 86: this.data = new DefaultKeyedValues(); 87: } 88: 89: /** 90: * Creates a new dataset by copying data from a {@link KeyedValues} 91: * instance. 92: * 93: * @param data the data (<code>null</code> not permitted). 94: */ 95: public DefaultPieDataset(KeyedValues data) { 96: if (data == null) { 97: throw new IllegalArgumentException("Null 'data' argument."); 98: } 99: this.data = new DefaultKeyedValues(); 100: for (int i = 0; i < data.getItemCount(); i++) { 101: this.data.addValue(data.getKey(i), data.getValue(i)); 102: } 103: } 104: 105: /** 106: * Returns the number of items in the dataset. 107: * 108: * @return The item count. 109: */ 110: public int getItemCount() { 111: return this.data.getItemCount(); 112: } 113: 114: /** 115: * Returns the categories in the dataset. The returned list is 116: * unmodifiable. 117: * 118: * @return The categories in the dataset. 119: */ 120: public List getKeys() { 121: return Collections.unmodifiableList(this.data.getKeys()); 122: } 123: 124: /** 125: * Returns the key for the specified item, or <code>null</code>. 126: * 127: * @param item the item index (in the range <code>0</code> to 128: * <code>getItemCount() - 1</code>). 129: * 130: * @return The key, or <code>null</code>. 131: * 132: * @throws IndexOutOfBoundsException if <code>item</code> is not in the 133: * specified range. 134: */ 135: public Comparable getKey(int item) { 136: return this.data.getKey(item); 137: } 138: 139: /** 140: * Returns the index for a key, or -1 if the key is not recognised. 141: * 142: * @param key the key (<code>null</code> not permitted). 143: * 144: * @return The index, or <code>-1</code> if the key is unrecognised. 145: * 146: * @throws IllegalArgumentException if <code>key</code> is 147: * <code>null</code>. 148: */ 149: public int getIndex(Comparable key) { 150: return this.data.getIndex(key); 151: } 152: 153: /** 154: * Returns a value. 155: * 156: * @param item the value index. 157: * 158: * @return The value (possibly <code>null</code>). 159: */ 160: public Number getValue(int item) { 161: 162: Number result = null; 163: if (getItemCount() > item) { 164: result = this.data.getValue(item); 165: } 166: return result; 167: 168: } 169: 170: /** 171: * Returns the data value associated with a key. 172: * 173: * @param key the key (<code>null</code> not permitted). 174: * 175: * @return The value (possibly <code>null</code>). 176: * 177: * @throws UnknownKeyException if the key is not recognised. 178: */ 179: public Number getValue(Comparable key) { 180: if (key == null) { 181: throw new IllegalArgumentException("Null 'key' argument."); 182: } 183: return this.data.getValue(key); 184: } 185: 186: /** 187: * Sets the data value for a key and sends a {@link DatasetChangeEvent} to 188: * all registered listeners. 189: * 190: * @param key the key (<code>null</code> not permitted). 191: * @param value the value. 192: * 193: * @throws IllegalArgumentException if <code>key</code> is 194: * <code>null</code>. 195: */ 196: public void setValue(Comparable key, Number value) { 197: this.data.setValue(key, value); 198: fireDatasetChanged(); 199: } 200: 201: /** 202: * Sets the data value for a key and sends a {@link DatasetChangeEvent} to 203: * all registered listeners. 204: * 205: * @param key the key (<code>null</code> not permitted). 206: * @param value the value. 207: * 208: * @throws IllegalArgumentException if <code>key</code> is 209: * <code>null</code>. 210: */ 211: public void setValue(Comparable key, double value) { 212: setValue(key, new Double(value)); 213: } 214: 215: /** 216: * Removes an item from the dataset and sends a {@link DatasetChangeEvent} 217: * to all registered listeners. 218: * 219: * @param key the key (<code>null</code> not permitted). 220: * 221: * @throws IllegalArgumentException if <code>key</code> is 222: * <code>null</code>. 223: */ 224: public void remove(Comparable key) { 225: this.data.removeValue(key); 226: fireDatasetChanged(); 227: } 228: 229: /** 230: * Clears all data from this dataset and sends a {@link DatasetChangeEvent} 231: * to all registered listeners (unless the dataset was already empty). 232: * 233: * @since 1.0.2 234: */ 235: public void clear() { 236: if (getItemCount() > 0) { 237: this.data.clear(); 238: fireDatasetChanged(); 239: } 240: } 241: 242: /** 243: * Tests if this object is equal to another. 244: * 245: * @param obj the other object. 246: * 247: * @return A boolean. 248: */ 249: public boolean equals(Object obj) { 250: if (obj == this) { 251: return true; 252: } 253: 254: if (!(obj instanceof PieDataset)) { 255: return false; 256: } 257: PieDataset that = (PieDataset) obj; 258: int count = getItemCount(); 259: if (that.getItemCount() != count) { 260: return false; 261: } 262: 263: for (int i = 0; i < count; i++) { 264: Comparable k1 = getKey(i); 265: Comparable k2 = that.getKey(i); 266: if (!k1.equals(k2)) { 267: return false; 268: } 269: 270: Number v1 = getValue(i); 271: Number v2 = that.getValue(i); 272: if (v1 == null) { 273: if (v2 != null) { 274: return false; 275: } 276: } 277: else { 278: if (!v1.equals(v2)) { 279: return false; 280: } 281: } 282: } 283: return true; 284: 285: } 286: 287: /** 288: * Returns a hash code. 289: * 290: * @return A hash code. 291: */ 292: public int hashCode() { 293: return this.data.hashCode(); 294: } 295: 296: /** 297: * Returns a clone of the dataset. 298: * 299: * @return A clone. 300: * 301: * @throws CloneNotSupportedException This class will not throw this 302: * exception, but subclasses (if any) might. 303: */ 304: public Object clone() throws CloneNotSupportedException { 305: DefaultPieDataset clone = (DefaultPieDataset) super.clone(); 306: clone.data = (DefaultKeyedValues) this.data.clone(); 307: return clone; 308: } 309: 310: }