001/* =========================================================== 002 * Orson Charts : a 3D chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C)opyright 2013-2016, by Object Refinery Limited. All rights reserved. 006 * 007 * http://www.object-refinery.com/orsoncharts/index.html 008 * 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation, either version 3 of the License, or 012 * (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 021 * 022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 023 * Other names may be trademarks of their respective owners.] 024 * 025 * If you do not wish to be bound by the terms of the GPL, an alternative 026 * commercial license can be purchased. For details, please see visit the 027 * Orson Charts home page: 028 * 029 * http://www.object-refinery.com/orsoncharts/index.html 030 * 031 */ 032 033package com.orsoncharts.data.category; 034 035import java.util.List; 036import java.io.Serializable; 037 038import com.orsoncharts.data.AbstractDataset3D; 039import com.orsoncharts.data.DefaultKeyedValues3D; 040import com.orsoncharts.data.JSONUtils; 041import com.orsoncharts.data.KeyedValues; 042import com.orsoncharts.util.ArgChecks; 043 044/** 045 * A standard implementation of the {@link CategoryDataset3D} interface. 046 * This dataset is typically used to create bar charts and stacked bar charts. 047 * <br><br> 048 * NOTE: This class is serializable, but the serialization format is subject 049 * to change in future releases and should not be relied upon for persisting 050 * instances of this class. 051 */ 052@SuppressWarnings("serial") 053public final class StandardCategoryDataset3D extends AbstractDataset3D 054 implements CategoryDataset3D, Serializable { 055 056 /** 057 * Storage for the data. 058 */ 059 private DefaultKeyedValues3D<Number> data; 060 061 /** 062 * Creates a new (empty) dataset. 063 */ 064 public StandardCategoryDataset3D() { 065 this.data = new DefaultKeyedValues3D<Number>(); 066 } 067 068 /** 069 * Returns the number of data series in the dataset. 070 * 071 * @return The number of data series. 072 */ 073 @Override 074 public int getSeriesCount() { 075 return this.data.getSeriesCount(); 076 } 077 078 /** 079 * Returns the number of rows in the dataset. 080 * 081 * @return The number of rows. 082 */ 083 @Override 084 public int getRowCount() { 085 return this.data.getRowCount(); 086 } 087 088 /** 089 * Returns the number of columns in the dataset. 090 * 091 * @return The number of columns. 092 */ 093 @Override 094 public int getColumnCount() { 095 return this.data.getColumnCount(); 096 } 097 098 /** 099 * Returns the key for the specified series. 100 * 101 * @param seriesIndex the series index. 102 * 103 * @return The series key. 104 */ 105 @Override 106 public Comparable<?> getSeriesKey(int seriesIndex) { 107 return this.data.getSeriesKey(seriesIndex); 108 } 109 110 /** 111 * Returns the key for the specified row. 112 * 113 * @param rowIndex The row index. 114 * 115 * @return The row key. 116 */ 117 @Override 118 public Comparable<?> getRowKey(int rowIndex) { 119 return this.data.getRowKey(rowIndex); 120 } 121 122 /** 123 * Returns the key for the specified column. 124 * 125 * @param columnIndex the column index. 126 * 127 * @return The column key. 128 */ 129 @Override 130 public Comparable<?> getColumnKey(int columnIndex) { 131 return this.data.getColumnKey(columnIndex); 132 } 133 134 /** 135 * Returns the index for the specified series key, or {@code -1} if the 136 * key is not defined in the dataset. 137 * 138 * @param serieskey the series key ({@code null} not permitted). 139 * 140 * @return The series index or {@code -1}. 141 */ 142 @Override 143 public int getSeriesIndex(Comparable<?> serieskey) { 144 return this.data.getSeriesIndex(serieskey); 145 } 146 147 /** 148 * Returns the index of the specified row key, or {@code -1} if there 149 * is no matching key. 150 * 151 * @param rowkey the row key ({@code null} not permitted). 152 * 153 * @return The row index or {@code -1}. 154 */ 155 @Override 156 public int getRowIndex(Comparable<?> rowkey) { 157 // arg checking is covered 158 return this.data.getRowIndex(rowkey); 159 } 160 161 /** 162 * Returns the index of the specified column key, or {@code -1} if 163 * there is no matching key. 164 * 165 * @param columnkey the column key ({@code null} not permitted). 166 * 167 * @return The column index or {@code -1}. 168 */ 169 @Override 170 public int getColumnIndex(Comparable<?> columnkey) { 171 // arg checking is covered 172 return this.data.getColumnIndex(columnkey); 173 } 174 175 /** 176 * Returns a list of the series keys for the dataset. Modifying this 177 * list will have no impact on the underlying dataset. 178 * 179 * @return A list of the series keys (possibly empty, but never 180 * {@code null}). 181 */ 182 @Override 183 public List<Comparable<?>> getSeriesKeys() { 184 return this.data.getSeriesKeys(); 185 } 186 187 /** 188 * Returns a list of the row keys for the dataset. Modifying this 189 * list will have no impact on the underlying dataset. 190 * 191 * @return A list of the row keys (possibly empty, but never 192 * {@code null}). 193 */ 194 @Override 195 public List<Comparable<?>> getRowKeys() { 196 return this.data.getRowKeys(); 197 } 198 199 /** 200 * Returns a list of the column keys for the dataset. Modifying this 201 * list will have no impact on the underlying dataset. 202 * 203 * @return A list of the column keys (possibly empty, but never 204 * {@code null}). 205 */ 206 @Override 207 public List<Comparable<?>> getColumnKeys() { 208 return this.data.getColumnKeys(); 209 } 210 211 /** 212 * Returns the value for a series at the specified cell (referenced by 213 * row key and column key). 214 * 215 * @param seriesKey the series key ({@code null} not permitted). 216 * @param rowKey the row key ({@code null} not permitted). 217 * @param columnKey the column key ({@code null} not permitted). 218 * 219 * @return The value (possibly {@code null}). 220 */ 221 @Override 222 public Number getValue(Comparable<?> seriesKey, Comparable<?> rowKey, 223 Comparable<?> columnKey) { 224 return this.data.getValue(seriesKey, rowKey, columnKey); 225 } 226 227 /** 228 * Returns the value for a series at the specified cell (referenced by 229 * row index and column index). 230 * 231 * @param seriesIndex the series index. 232 * @param rowIndex the row index. 233 * @param columnIndex the column index. 234 * 235 * @return The value (possibly {@code null}). 236 */ 237 @Override 238 public Number getValue(int seriesIndex, int rowIndex, int columnIndex) { 239 return this.data.getValue(seriesIndex, rowIndex, columnIndex); 240 } 241 242 /** 243 * Sets the value for a series at the specified cell (referenced by row 244 * key and column key). 245 * 246 * @param n the value ({@code null} permitted). 247 * @param seriesKey the series key ({@code null} not permitted). 248 * @param rowKey the row key ({@code null} not permitted). 249 * @param columnKey the column key ({@code null} not permitted). 250 */ 251 public void setValue(Number n, Comparable<?> seriesKey, 252 Comparable<?> rowKey, Comparable<?> columnKey) { 253 this.data.setValue(n, seriesKey, rowKey, columnKey); 254 fireDatasetChanged(); 255 } 256 257 /** 258 * Adds a value for a series at the specified cell (referenced by row key 259 * and column key). This method simply calls {@link #setValue( 260 * java.lang.Number, java.lang.Comparable, java.lang.Comparable, 261 * java.lang.Comparable) }. 262 * 263 * @param n the value ({@code null} permitted). 264 * @param seriesKey the series key ({@code null} not permitted). 265 * @param rowKey the row key ({@code null} not permitted). 266 * @param columnKey the column key ({@code null} not permitted). 267 */ 268 public void addValue(Number n, Comparable<?> seriesKey, 269 Comparable<?> rowKey, Comparable<?> columnKey) { 270 setValue(n, seriesKey, rowKey, columnKey); 271 } 272 273 /** 274 * Returns the value for a series at the specified cell (referenced by row 275 * index and column index) as a double primitive. If the stored data 276 * value is {@code null}, this method returns {@code Double.NaN}. 277 * 278 * @param seriesIndex the series index. 279 * @param rowIndex the row index. 280 * @param columnIndex the column index. 281 * 282 * @return The value (possibly {@code Double.NaN}). 283 */ 284 @Override 285 public double getDoubleValue(int seriesIndex, int rowIndex, 286 int columnIndex) { 287 return this.data.getDoubleValue(seriesIndex, rowIndex, columnIndex); 288 } 289 290 /** 291 * Adds a data series as a single row in the dataset. 292 * 293 * @param seriesKey the series key ({@code null} not permitted). 294 * @param data the data ({@code null} not permitted). 295 */ 296 public void addSeriesAsRow(Comparable<?> seriesKey, 297 KeyedValues<? extends Number> data) { 298 addSeriesAsRow(seriesKey, seriesKey, data); 299 } 300 301 /** 302 * Adds a data series as a single row in the dataset. 303 * 304 * @param seriesKey the series key ({@code null} not permitted). 305 * @param rowKey the row key ({@code null} not permitted). 306 * @param data the data ({@code null} not permitted). 307 */ 308 public void addSeriesAsRow(Comparable<?> seriesKey, Comparable<?> rowKey, 309 KeyedValues<? extends Number> data) { 310 ArgChecks.nullNotPermitted(seriesKey, "seriesKey"); 311 ArgChecks.nullNotPermitted(data, "data"); 312 for (Comparable<?> key : data.getKeys()) { 313 setValue(data.getValue(key), seriesKey, rowKey, key); 314 } 315 } 316 317 /** 318 * Tests this instance for equality with an arbitrary object. 319 * 320 * @param obj the object to test against ({@code null} permitted). 321 * 322 * @return A boolean. 323 */ 324 @Override 325 public boolean equals(Object obj) { 326 if (obj == this) { 327 return true; 328 } 329 if (!(obj instanceof StandardCategoryDataset3D)) { 330 return false; 331 } 332 StandardCategoryDataset3D that = (StandardCategoryDataset3D) obj; 333 if (!this.data.equals(that.data)) { 334 return false; 335 } 336 return true; 337 } 338 339 /** 340 * Returns a string representation of this instance, primarily for 341 * debugging purposes. 342 * <br><br> 343 * Implementation note: the current implementation (which is subject to 344 * change) writes the dataset in JSON format using 345 * {@link JSONUtils#writeKeyedValues3D(com.orsoncharts.data.KeyedValues3D)}. 346 * 347 * @return A string. 348 */ 349 @Override 350 public String toString() { 351 return JSONUtils.writeKeyedValues3D(this); 352 } 353}