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 * @since 1.3
048 */
049public class XYZItemKey<S extends Comparable<S>> implements ItemKey, 
050        Comparable<XYZItemKey<S>>, Serializable {
051    
052    /** A key identifying a series in the dataset. */
053    private final S seriesKey;
054    
055    /** The index of an item within a series. */
056    private final int itemIndex;
057    
058    /**
059     * Creates a new instance.
060     * 
061     * @param seriesKey  the series key ({@code null} not permitted).
062     * @param itemIndex  the item index.
063     */
064    public XYZItemKey(S seriesKey, int itemIndex) {
065        ArgChecks.nullNotPermitted(seriesKey, "seriesKey");
066        this.seriesKey = seriesKey;
067        this.itemIndex = itemIndex;
068    }
069    
070    /**
071     * Returns the series key.
072     * 
073     * @return The series key (never {@code null}). 
074     */
075    public S getSeriesKey() {
076        return this.seriesKey;
077    }
078    
079    /**
080     * Returns the item index.
081     * 
082     * @return The item index.
083     */
084    public int getItemIndex() {
085        return this.itemIndex;
086    }
087    
088    /**
089     * Tests this instance for equality with an arbitrary object.
090     * 
091     * @param obj  the object to test ({@code null} permitted).
092     * 
093     * @return A boolean. 
094     */
095    @Override
096    public boolean equals(Object obj) {
097        if (obj == this) {
098            return true;
099        }
100        if (!(obj instanceof XYZItemKey)) {
101            return false;
102        }
103        XYZItemKey that = (XYZItemKey) obj;
104        if (!this.seriesKey.equals(that.seriesKey)) {
105            return false;
106        }
107        if (this.itemIndex != that.itemIndex) {
108            return false;
109        }
110        return true;
111    }
112
113    @Override
114    public int hashCode() {
115        int hash = 7;
116        hash = 41 * hash + ObjectUtils.hashCode(this.seriesKey);
117        hash = 41 * hash + this.itemIndex;
118        return hash;
119    }
120
121    @Override
122    public String toJSONString() {
123        StringBuilder sb = new StringBuilder();
124        sb.append("{\"seriesKey\": \"").append(this.seriesKey.toString());
125        sb.append("\", ");
126        sb.append("\"itemIndex\": ").append(this.itemIndex).append("}");
127        return sb.toString();
128    }
129
130    @Override
131    public String toString() {
132        StringBuilder sb = new StringBuilder();
133        sb.append("XYZItemKey[seriesKey=");
134        sb.append(this.seriesKey.toString()).append(",item=");
135        sb.append(itemIndex);
136        sb.append("]");
137        return sb.toString();
138    }
139
140    @Override
141    public int compareTo(XYZItemKey<S> key) {
142        int result = this.seriesKey.compareTo(key.seriesKey);
143        if (result == 0) {
144            result = this.itemIndex - key.itemIndex;
145        }
146        return result;
147    }
148
149}