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.xyz;
034
035import java.io.Serializable;
036
037import com.orsoncharts.data.ItemKey;
038import com.orsoncharts.util.ArgChecks;
039import com.orsoncharts.util.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        ArgChecks.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        StringBuilder sb = new StringBuilder();
126        sb.append("{\"seriesKey\": \"").append(this.seriesKey.toString());
127        sb.append("\", ");
128        sb.append("\"itemIndex\": ").append(this.itemIndex).append("}");
129        return sb.toString();
130    }
131
132    @Override
133    public String toString() {
134        StringBuilder sb = new StringBuilder();
135        sb.append("XYZItemKey[seriesKey=");
136        sb.append(this.seriesKey.toString()).append(",item=");
137        sb.append(itemIndex);
138        sb.append("]");
139        return sb.toString();
140    }
141
142    @Override
143    public int compareTo(XYZItemKey<S> key) {
144        int result = this.seriesKey.compareTo(key.seriesKey);
145        if (result == 0) {
146            result = this.itemIndex - key.itemIndex;
147        }
148        return result;
149    }
150
151}