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