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