001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ---------------------- 028 * DefaultPieDataset.java 029 * ---------------------- 030 * (C) Copyright 2001-2020, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Sam (oldman); 034 * Tracy Hiltbrand (generics for bug fix to PiePlot); 035 */ 036 037package org.jfree.data.general; 038 039import java.io.Serializable; 040import java.util.Collections; 041import java.util.List; 042import org.jfree.chart.util.Args; 043import org.jfree.chart.util.PublicCloneable; 044import org.jfree.chart.util.SortOrder; 045 046import org.jfree.data.DefaultKeyedValues; 047import org.jfree.data.KeyedValues; 048import org.jfree.data.UnknownKeyException; 049 050/** 051 * A default implementation of the {@link PieDataset} interface. 052 * 053 * @param <K> Key type for PieDataset 054 */ 055public class DefaultPieDataset<K extends Comparable<K>> extends AbstractDataset 056 implements PieDataset<K>, Cloneable, PublicCloneable, Serializable { 057 058 /** For serialization. */ 059 private static final long serialVersionUID = 2904745139106540618L; 060 061 /** Storage for the data. */ 062 private DefaultKeyedValues<K> data; 063 064 /** 065 * Constructs a new dataset, initially empty. 066 */ 067 public DefaultPieDataset() { 068 this.data = new DefaultKeyedValues<>(); 069 } 070 071 /** 072 * Creates a new dataset by copying data from a {@link KeyedValues} 073 * instance. 074 * 075 * @param source the data ({@code null} not permitted). 076 */ 077 public DefaultPieDataset(KeyedValues<K> source) { 078 Args.nullNotPermitted(data, "data"); 079 this.data = new DefaultKeyedValues<>(); 080 for (int i = 0; i < source.getItemCount(); i++) { 081 this.data.addValue(source.getKey(i), source.getValue(i)); 082 } 083 } 084 085 /** 086 * Returns the number of items in the dataset. 087 * 088 * @return The item count. 089 */ 090 @Override 091 public int getItemCount() { 092 return this.data.getItemCount(); 093 } 094 095 /** 096 * Returns the categories in the dataset. The returned list is 097 * unmodifiable. 098 * 099 * @return The categories in the dataset. 100 */ 101 @Override 102 public List<K> getKeys() { 103 return Collections.unmodifiableList(this.data.getKeys()); 104 } 105 106 /** 107 * Returns the key for the specified item, or {@code null}. 108 * 109 * @param item the item index (in the range {@code 0} to 110 * {@code getItemCount() - 1}). 111 * 112 * @return The key, or {@code null}. 113 * 114 * @throws IndexOutOfBoundsException if {@code item} is not in the 115 * specified range. 116 */ 117 @Override 118 public K getKey(int item) { 119 return this.data.getKey(item); 120 } 121 122 /** 123 * Returns the index for a key, or -1 if the key is not recognised. 124 * 125 * @param key the key ({@code null} not permitted). 126 * 127 * @return The index, or {@code -1} if the key is unrecognised. 128 * 129 * @throws IllegalArgumentException if {@code key} is 130 * {@code null}. 131 */ 132 @Override 133 public int getIndex(K key) { 134 return this.data.getIndex(key); 135 } 136 137 /** 138 * Returns a value. 139 * 140 * @param item the value index. 141 * 142 * @return The value (possibly {@code null}). 143 */ 144 @Override 145 public Number getValue(int item) { 146 Number result = null; 147 if (getItemCount() > item) { 148 result = this.data.getValue(item); 149 } 150 return result; 151 } 152 153 /** 154 * Returns the data value associated with a key. 155 * 156 * @param key the key ({@code null} not permitted). 157 * 158 * @return The value (possibly {@code null}). 159 * 160 * @throws UnknownKeyException if the key is not recognised. 161 */ 162 @Override 163 public Number getValue(K key) { 164 Args.nullNotPermitted(key, "key"); 165 return this.data.getValue(key); 166 } 167 168 /** 169 * Sets the data value for a key and sends a {@link DatasetChangeEvent} to 170 * all registered listeners. 171 * 172 * @param key the key ({@code null} not permitted). 173 * @param value the value. 174 * 175 * @throws IllegalArgumentException if {@code key} is 176 * {@code null}. 177 */ 178 public void setValue(K key, Number value) { 179 this.data.setValue(key, value); 180 fireDatasetChanged(); 181 } 182 183 /** 184 * Sets the data value for a key and sends a {@link DatasetChangeEvent} to 185 * all registered listeners. 186 * 187 * @param key the key ({@code null} not permitted). 188 * @param value the value. 189 * 190 * @throws IllegalArgumentException if {@code key} is 191 * {@code null}. 192 */ 193 public void setValue(K key, double value) { 194 setValue(key, Double.valueOf(value)); 195 } 196 197 /** 198 * Inserts a new value at the specified position in the dataset or, if 199 * there is an existing item with the specified key, updates the value 200 * for that item and moves it to the specified position. After the change 201 * is made, this methods sends a {@link DatasetChangeEvent} to all 202 * registered listeners. 203 * 204 * @param position the position (in the range 0 to getItemCount()). 205 * @param key the key ({@code null} not permitted). 206 * @param value the value ({@code null} permitted). 207 * 208 * @since 1.0.6 209 */ 210 public void insertValue(int position, K key, double value) { 211 insertValue(position, key, Double.valueOf(value)); 212 } 213 214 /** 215 * Inserts a new value at the specified position in the dataset or, if 216 * there is an existing item with the specified key, updates the value 217 * for that item and moves it to the specified position. After the change 218 * is made, this methods sends a {@link DatasetChangeEvent} to all 219 * registered listeners. 220 * 221 * @param position the position (in the range 0 to getItemCount()). 222 * @param key the key ({@code null} not permitted). 223 * @param value the value ({@code null} permitted). 224 * 225 * @since 1.0.6 226 */ 227 public void insertValue(int position, K key, Number value) { 228 this.data.insertValue(position, key, value); 229 fireDatasetChanged(); 230 } 231 232 /** 233 * Removes an item from the dataset and sends a {@link DatasetChangeEvent} 234 * to all registered listeners. 235 * 236 * @param key the key ({@code null} not permitted). 237 * 238 * @throws IllegalArgumentException if {@code key} is 239 * {@code null}. 240 */ 241 public void remove(K key) { 242 this.data.removeValue(key); 243 fireDatasetChanged(); 244 } 245 246 /** 247 * Clears all data from this dataset and sends a {@link DatasetChangeEvent} 248 * to all registered listeners (unless the dataset was already empty). 249 * 250 * @since 1.0.2 251 */ 252 public void clear() { 253 if (getItemCount() > 0) { 254 this.data.clear(); 255 fireDatasetChanged(); 256 } 257 } 258 259 /** 260 * Sorts the dataset's items by key and sends a {@link DatasetChangeEvent} 261 * to all registered listeners. 262 * 263 * @param order the sort order ({@code null} not permitted). 264 * 265 * @since 1.0.3 266 */ 267 public void sortByKeys(SortOrder order) { 268 this.data.sortByKeys(order); 269 fireDatasetChanged(); 270 } 271 272 /** 273 * Sorts the dataset's items by value and sends a {@link DatasetChangeEvent} 274 * to all registered listeners. 275 * 276 * @param order the sort order ({@code null} not permitted). 277 * 278 * @since 1.0.3 279 */ 280 public void sortByValues(SortOrder order) { 281 this.data.sortByValues(order); 282 fireDatasetChanged(); 283 } 284 285 /** 286 * Tests if this object is equal to another. 287 * 288 * @param obj the other object. 289 * 290 * @return A boolean. 291 */ 292 @Override 293 public boolean equals(Object obj) { 294 if (obj == this) { 295 return true; 296 } 297 298 if (!(obj instanceof PieDataset)) { 299 return false; 300 } 301 @SuppressWarnings("unchecked") 302 PieDataset<K> that = (PieDataset<K>) obj; 303 int count = getItemCount(); 304 if (that.getItemCount() != count) { 305 return false; 306 } 307 308 for (int i = 0; i < count; i++) { 309 K k1 = getKey(i); 310 K 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 /** 333 * Returns a hash code. 334 * 335 * @return A hash code. 336 */ 337 @Override 338 public int hashCode() { 339 return this.data.hashCode(); 340 } 341 342 /** 343 * Returns a clone of the dataset. 344 * 345 * @return A clone. 346 * 347 * @throws CloneNotSupportedException This class will not throw this 348 * exception, but subclasses (if any) might. 349 */ 350 @Override 351 @SuppressWarnings("unchecked") 352 public Object clone() throws CloneNotSupportedException { 353 DefaultPieDataset<K> clone = (DefaultPieDataset<K>) super.clone(); 354 clone.data = (DefaultKeyedValues) this.data.clone(); 355 return clone; 356 } 357 358}