001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ---------------------- 028 * DefaultPieDataset.java 029 * ---------------------- 030 * (C) Copyright 2001-2005, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Sam (oldman); 034 * 035 * $Id: DefaultPieDataset.java,v 1.6.2.2 2005/10/25 21:32:29 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 17-Nov-2001 : Version 1 (DG); 040 * 22-Jan-2002 : Removed legend methods from dataset implementations (DG); 041 * 07-Apr-2002 : Modified implementation to guarantee data sequence to remain 042 * in the order categories are added (oldman); 043 * 23-Oct-2002 : Added getCategory(int) method and getItemCount() method, in 044 * line with changes to the PieDataset interface (DG); 045 * 04-Feb-2003 : Changed underlying data storage to DefaultKeyedValues (DG); 046 * 04-Mar-2003 : Inserted DefaultKeyedValuesDataset class into hierarchy (DG); 047 * 24-Apr-2003 : Switched places with DefaultKeyedValuesDataset (DG); 048 * 18-Aug-2003 : Implemented Cloneable (DG); 049 * 03-Mar-2005 : Implemented PublicCloneable (DG); 050 * 29-Jun-2005 : Added remove() method (DG); 051 * 052 */ 053 054 package org.jfree.data.general; 055 056 import java.io.Serializable; 057 import java.util.Collections; 058 import java.util.List; 059 060 import org.jfree.data.DefaultKeyedValues; 061 import org.jfree.data.KeyedValues; 062 import org.jfree.data.UnknownKeyException; 063 import org.jfree.util.PublicCloneable; 064 065 /** 066 * A default implementation of the {@link PieDataset} interface. 067 */ 068 public class DefaultPieDataset extends AbstractDataset 069 implements PieDataset, 070 Cloneable, PublicCloneable, 071 Serializable { 072 073 /** For serialization. */ 074 private static final long serialVersionUID = 2904745139106540618L; 075 076 /** Storage for the data. */ 077 private DefaultKeyedValues data; 078 079 /** 080 * Constructs a new dataset, initially empty. 081 */ 082 public DefaultPieDataset() { 083 this.data = new DefaultKeyedValues(); 084 } 085 086 /** 087 * Creates a new dataset by copying data from a {@link KeyedValues} 088 * instance. 089 * 090 * @param data the data (<code>null</code> not permitted). 091 */ 092 public DefaultPieDataset(KeyedValues data) { 093 if (data == null) { 094 throw new IllegalArgumentException("Null 'data' argument."); 095 } 096 this.data = new DefaultKeyedValues(); 097 for (int i = 0; i < data.getItemCount(); i++) { 098 this.data.addValue(data.getKey(i), data.getValue(i)); 099 } 100 } 101 102 /** 103 * Returns the number of items in the dataset. 104 * 105 * @return The item count. 106 */ 107 public int getItemCount() { 108 return this.data.getItemCount(); 109 } 110 111 /** 112 * Returns the categories in the dataset. The returned list is 113 * unmodifiable. 114 * 115 * @return The categories in the dataset. 116 */ 117 public List getKeys() { 118 return Collections.unmodifiableList(this.data.getKeys()); 119 } 120 121 /** 122 * Returns the key for an item. 123 * 124 * @param item the item index (zero-based). 125 * 126 * @return The key. 127 */ 128 public Comparable getKey(int item) { 129 Comparable result = null; 130 if (getItemCount() > item) { 131 result = this.data.getKey(item); 132 } 133 return result; 134 } 135 136 /** 137 * Returns the index for a key. 138 * 139 * @param key the key. 140 * 141 * @return The index, or <code>-1</code> if the key is unrecognised. 142 */ 143 public int getIndex(Comparable key) { 144 return this.data.getIndex(key); 145 } 146 147 /** 148 * Returns a value. 149 * 150 * @param item the value index. 151 * 152 * @return The value (possibly <code>null</code>). 153 */ 154 public Number getValue(int item) { 155 156 Number result = null; 157 if (getItemCount() > item) { 158 result = this.data.getValue(item); 159 } 160 return result; 161 162 } 163 164 /** 165 * Returns the data value associated with a key. 166 * 167 * @param key the key (<code>null</code> not permitted). 168 * 169 * @return The value (possibly <code>null</code>). 170 * 171 * @throws UnknownKeyException if the key is not recognised. 172 */ 173 public Number getValue(Comparable key) { 174 if (key == null) { 175 throw new IllegalArgumentException("Null 'key' argument."); 176 } 177 return this.data.getValue(key); 178 } 179 180 /** 181 * Sets the data value for a key. 182 * 183 * @param key the key. 184 * @param value the value. 185 */ 186 public void setValue(Comparable key, Number value) { 187 this.data.setValue(key, value); 188 fireDatasetChanged(); 189 } 190 191 /** 192 * Sets the data value for a key. 193 * 194 * @param key the key. 195 * @param value the value. 196 */ 197 public void setValue(Comparable key, double value) { 198 setValue(key, new Double(value)); 199 } 200 201 /** 202 * Removes an item from the dataset and sends a {@link DatasetChangeEvent} 203 * to all registered listeners. 204 * 205 * @param key the key. 206 */ 207 public void remove(Comparable key) { 208 this.data.removeValue(key); 209 fireDatasetChanged(); 210 } 211 212 /** 213 * Tests if this object is equal to another. 214 * 215 * @param obj the other object. 216 * 217 * @return A boolean. 218 */ 219 public boolean equals(Object obj) { 220 if (obj == this) { 221 return true; 222 } 223 224 if (!(obj instanceof PieDataset)) { 225 return false; 226 } 227 PieDataset that = (PieDataset) obj; 228 int count = getItemCount(); 229 if (that.getItemCount() != count) { 230 return false; 231 } 232 233 for (int i = 0; i < count; i++) { 234 Comparable k1 = getKey(i); 235 Comparable k2 = that.getKey(i); 236 if (!k1.equals(k2)) { 237 return false; 238 } 239 240 Number v1 = getValue(i); 241 Number v2 = that.getValue(i); 242 if (v1 == null) { 243 if (v2 != null) { 244 return false; 245 } 246 } 247 else { 248 if (!v1.equals(v2)) { 249 return false; 250 } 251 } 252 } 253 return true; 254 255 } 256 257 /** 258 * Returns a hash code. 259 * 260 * @return A hash code. 261 */ 262 public int hashCode() { 263 return this.data.hashCode(); 264 } 265 266 /** 267 * Returns a clone of the dataset. 268 * 269 * @return A clone. 270 * 271 * @throws CloneNotSupportedException This class will not throw this 272 * exception, but subclasses (if any) might. 273 */ 274 public Object clone() throws CloneNotSupportedException { 275 DefaultPieDataset clone = (DefaultPieDataset) super.clone(); 276 clone.data = (DefaultKeyedValues) this.data.clone(); 277 return clone; 278 } 279 280 }