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