    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.xyz;
    034
    035import java.io.Serializable;
    036
    037import org.jfree.chart3d.data.ItemKey;
    038import org.jfree.chart3d.internal.Args;
    039import org.jfree.chart3d.internal.ObjectUtils;
    040
    041/**
    042 * An object that references one data item in an {@link XYZDataset}.  This is
    043 * used internally to track the data item that a 3D object is related to, if
    044 * any (and later that link is used for chart interaction).  Instances of
    045 * this class are immutable.
    046 *
    047 * @param <S> The series key type.
    048 *
    049 * @since 1.3
    050 */
    051public class XYZItemKey<S extends Comparable<S>> implements ItemKey,
    052        Comparable<XYZItemKey<S>>, Serializable {
    053
    054    /** A key identifying a series in the dataset. */
    055    private final S seriesKey;
    056
    057    /** The index of an item within a series. */
    058    private final int itemIndex;
    059
    060    /**
    061     * Creates a new instance.
    062     *
    063     * @param seriesKey  the series key ({@code null} not permitted).
    064     * @param itemIndex  the item index.
    065     */
    066    public XYZItemKey(S seriesKey, int itemIndex) {
    067        Args.nullNotPermitted(seriesKey, "seriesKey");
    068        this.seriesKey = seriesKey;
    069        this.itemIndex = itemIndex;
    070    }
    071
    072    /**
    073     * Returns the series key.
    074     *
    075     * @return The series key (never {@code null}).
    076     */
    077    public S getSeriesKey() {
    078        return this.seriesKey;
    079    }
    080
    081    /**
    082     * Returns the item index.
    083     *
    084     * @return The item index.
    085     */
    086    public int getItemIndex() {
    087        return this.itemIndex;
    088    }
    089
    090    /**
    091     * Tests this instance for equality with an arbitrary object.
    092     *
    093     * @param obj  the object to test ({@code null} permitted).
    094     *
    095     * @return A boolean.
    096     */
    097    @Override
    098    public boolean equals(Object obj) {
    099        if (obj == this) {
    100            return true;
    101        }
    102        if (!(obj instanceof XYZItemKey)) {
    103            return false;
    104        }
    105        XYZItemKey that = (XYZItemKey) obj;
    106        if (!this.seriesKey.equals(that.seriesKey)) {
    107            return false;
    108        }
    109        if (this.itemIndex != that.itemIndex) {
    110            return false;
    111        }
    112        return true;
    113    }
    114
    115    @Override
    116    public int hashCode() {
    117        int hash = 7;
    118        hash = 41 * hash + ObjectUtils.hashCode(this.seriesKey);
    119        hash = 41 * hash + this.itemIndex;
    120        return hash;
    121    }
    122
    123    @Override
    124    public String toJSONString() {
    125        return "{\"seriesKey\": \"" + this.seriesKey.toString()
    126                + "\", " + "\"itemIndex\": " + this.itemIndex + "}";
    127    }
    128
    129    @Override
    130    public String toString() {
    131        return "XYZItemKey[seriesKey=" + this.seriesKey.toString()
    132                + ",item=" + itemIndex + "]";
    133    }
    134
    135    @Override
    136    public int compareTo(XYZItemKey<S> key) {
    137        int result = this.seriesKey.compareTo(key.seriesKey);
    138        if (result == 0) {
    139            result = this.itemIndex - key.itemIndex;
    140        }
    141        return result;
    142    }
    143
    144}