    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
    006 *
    007 * https://github.com/jfree/orson-charts
    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 org.jfree.chart3d.data.category;
    034
    035import java.util.List;
    036import java.io.Serializable;
    037
    038import org.jfree.chart3d.data.AbstractDataset3D;
    039import org.jfree.chart3d.data.DefaultKeyedValues3D;
    040import org.jfree.chart3d.data.JSONUtils;
    041import org.jfree.chart3d.data.KeyedValues;
    042import org.jfree.chart3d.internal.Args;
    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
    054        <S extends Comparable<S>, R extends Comparable<R>, C extends Comparable<C>>
    055        extends AbstractDataset3D
    056        implements CategoryDataset3D<S, R, C>, Serializable {
    057
    058    /**
    059     * Storage for the data.
    060     */
    061    private final DefaultKeyedValues3D<S, R, C, Number> data;
    062
    063    /**
    064     * Creates a new (empty) dataset.
    065     */
    066    public StandardCategoryDataset3D() {
    067        this.data = new DefaultKeyedValues3D<>();
    068    }
    069
    070    /**
    071     * Returns the number of data series in the dataset.
    072     *
    073     * @return The number of data series.
    074     */
    075    @Override
    076    public int getSeriesCount() {
    077        return this.data.getSeriesCount();
    078    }
    079
    080    /**
    081     * Returns the number of rows in the dataset.
    082     *
    083     * @return The number of rows.
    084     */
    085    @Override
    086    public int getRowCount() {
    087        return this.data.getRowCount();
    088    }
    089
    090    /**
    091     * Returns the number of columns in the dataset.
    092     *
    093     * @return The number of columns.
    094     */
    095    @Override
    096    public int getColumnCount() {
    097        return this.data.getColumnCount();
    098    }
    099
    100    /**
    101     * Returns the key for the specified series.
    102     *
    103     * @param seriesIndex  the series index.
    104     *
    105     * @return The series key.
    106     */
    107    @Override
    108    public S getSeriesKey(int seriesIndex) {
    109        return this.data.getSeriesKey(seriesIndex);
    110    }
    111
    112    /**
    113     * Returns the key for the specified row.
    114     *
    115     * @param rowIndex The row index.
    116     *
    117     * @return The row key.
    118     */
    119    @Override
    120    public R getRowKey(int rowIndex) {
    121        return this.data.getRowKey(rowIndex);
    122    }
    123
    124    /**
    125     * Returns the key for the specified column.
    126     *
    127     * @param columnIndex  the column index.
    128     *
    129     * @return The column key.
    130     */
    131    @Override
    132    public C getColumnKey(int columnIndex) {
    133        return this.data.getColumnKey(columnIndex);
    134    }
    135
    136    /**
    137     * Returns the index for the specified series key, or {@code -1} if the
    138     * key is not defined in the dataset.
    139     *
    140     * @param serieskey  the series key ({@code null} not permitted).
    141     *
    142     * @return The series index or {@code -1}.
    143     */
    144    @Override
    145    public int getSeriesIndex(S serieskey) {
    146        return this.data.getSeriesIndex(serieskey);
    147    }
    148
    149    /**
    150     * Returns the index of the specified row key, or {@code -1} if there
    151     * is no matching key.
    152     *
    153     * @param rowkey  the row key ({@code null} not permitted).
    154     *
    155     * @return The row index or {@code -1}.
    156     */
    157    @Override
    158    public int getRowIndex(R rowkey) {
    159        // arg checking is covered
    160        return this.data.getRowIndex(rowkey);
    161    }
    162
    163    /**
    164     * Returns the index of the specified column key, or {@code -1} if
    165     * there is no matching key.
    166     *
    167     * @param columnkey  the column key ({@code null} not permitted).
    168     *
    169     * @return The column index or {@code -1}.
    170     */
    171    @Override
    172    public int getColumnIndex(C columnkey) {
    173        // arg checking is covered
    174        return this.data.getColumnIndex(columnkey);
    175    }
    176
    177    /**
    178     * Returns a list of the series keys for the dataset.  Modifying this
    179     * list will have no impact on the underlying dataset.
    180     *
    181     * @return A list of the series keys (possibly empty, but never
    182     *     {@code null}).
    183     */
    184    @Override
    185    public List<S> getSeriesKeys() {
    186        return this.data.getSeriesKeys();
    187    }
    188
    189    /**
    190     * Returns a list of the row keys for the dataset.  Modifying this
    191     * list will have no impact on the underlying dataset.
    192     *
    193     * @return A list of the row keys (possibly empty, but never
    194     *     {@code null}).
    195     */
    196    @Override
    197    public List<R> getRowKeys() {
    198        return this.data.getRowKeys();
    199    }
    200
    201    /**
    202     * Returns a list of the column keys for the dataset.  Modifying this
    203     * list will have no impact on the underlying dataset.
    204     *
    205     * @return A list of the column keys (possibly empty, but never
    206     *     {@code null}).
    207     */
    208    @Override
    209    public List<C> getColumnKeys() {
    210        return this.data.getColumnKeys();
    211    }
    212
    213    /**
    214     * Returns the value for a series at the specified cell (referenced by
    215     * row key and column key).
    216     *
    217     * @param seriesKey  the series key ({@code null} not permitted).
    218     * @param rowKey  the row key ({@code null} not permitted).
    219     * @param columnKey  the column key ({@code null} not permitted).
    220     *
    221     * @return The value (possibly {@code null}).
    222     */
    223    @Override
    224    public Number getValue(S seriesKey, R rowKey, C columnKey) {
    225        return this.data.getValue(seriesKey, rowKey, columnKey);
    226    }
    227
    228    /**
    229     * Returns the value for a series at the specified cell (referenced by
    230     * row index and column index).
    231     *
    232     * @param seriesIndex  the series index.
    233     * @param rowIndex  the row index.
    234     * @param columnIndex  the column index.
    235     *
    236     * @return The value (possibly {@code null}).
    237     */
    238    @Override
    239    public Number getValue(int seriesIndex, int rowIndex, int columnIndex) {
    240        return this.data.getValue(seriesIndex, rowIndex, columnIndex);
    241    }
    242
    243    /**
    244     * Sets the value for a series at the specified cell (referenced by row
    245     * key and column key).
    246     *
    247     * @param n  the value ({@code null} permitted).
    248     * @param seriesKey  the series key ({@code null} not permitted).
    249     * @param rowKey  the row key ({@code null} not permitted).
    250     * @param columnKey  the column key ({@code null} not permitted).
    251     */
    252    public void setValue(Number n, S seriesKey, R rowKey, C 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, S seriesKey, R rowKey, C columnKey) {
    269        setValue(n, seriesKey, rowKey, columnKey);
    270    }
    271
    272    /**
    273     * Returns the value for a series at the specified cell (referenced by row
    274     * index and column index) as a double primitive.  If the stored data
    275     * value is {@code null}, this method returns {@code Double.NaN}.
    276     *
    277     * @param seriesIndex  the series index.
    278     * @param rowIndex  the row index.
    279     * @param columnIndex  the column index.
    280     *
    281     * @return The value (possibly {@code Double.NaN}).
    282     */
    283    @Override
    284    public double getDoubleValue(int seriesIndex, int rowIndex,
    285            int columnIndex) {
    286        return this.data.getDoubleValue(seriesIndex, rowIndex, columnIndex);
    287    }
    288
    289    /**
    290     * Adds a data series as a single row in the dataset.
    291     *
    292     * @param seriesKey  the series key ({@code null} not permitted).
    293     * @param data  the data ({@code null} not permitted).
    294     */
    295    @SuppressWarnings("unchecked")
    296    public void addSeriesAsRow(S seriesKey,
    297            KeyedValues<C, ? extends Number> data) {
    298        addSeriesAsRow(seriesKey, (R) 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(S seriesKey, R rowKey,
    309            KeyedValues<C, ? extends Number> data) {
    310        Args.nullNotPermitted(seriesKey, "seriesKey");
    311        Args.nullNotPermitted(data, "data");
    312        for (C 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(org.jfree.chart3d.data.KeyedValues3D)}.
    346     *
    347     * @return A string.
    348     */
    349    @Override
    350    public String toString() {
    351        return JSONUtils.writeKeyedValues3D(this);
    352    }
    353}