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 * XYItemEntity.java
029 * -----------------
030 * (C) Copyright 2002-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Richard Atkinson;
034 *                   Christian W. Zuckschwerdt;
035 *                   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
036 *
037 */
038
039package org.jfree.chart.entity;
040
041import java.awt.Shape;
042
043import org.jfree.data.xy.XYDataset;
044
045/**
046 * A chart entity that represents one item within an
047 * {@link org.jfree.chart.plot.XYPlot}.
048 */
049public class XYItemEntity extends ChartEntity {
050
051    /** For serialization. */
052    private static final long serialVersionUID = -3870862224880283771L;
053
054    /** The dataset. */
055    private transient XYDataset dataset;
056
057    /** The series. */
058    private int series;
059
060    /** The item. */
061    private int item;
062
063    /**
064     * Creates a new entity.
065     *
066     * @param area  the area.
067     * @param dataset  the dataset.
068     * @param series  the series (zero-based index).
069     * @param item  the item (zero-based index).
070     * @param toolTipText  the tool tip text.
071     * @param urlText  the URL text for HTML image maps.
072     */
073    public XYItemEntity(Shape area,
074                        XYDataset dataset, int series, int item,
075                        String toolTipText, String urlText) {
076        super(area, toolTipText, urlText);
077        this.dataset = dataset;
078        this.series = series;
079        this.item = item;
080    }
081
082    /**
083     * Returns the dataset this entity refers to.
084     *
085     * @return The dataset.
086     */
087    public XYDataset getDataset() {
088        return this.dataset;
089    }
090
091    /**
092     * Sets the dataset this entity refers to.
093     *
094     * @param dataset  the dataset.
095     */
096    public void setDataset(XYDataset dataset) {
097        this.dataset = dataset;
098    }
099
100    /**
101     * Returns the series index.
102     *
103     * @return The series index.
104     */
105    public int getSeriesIndex() {
106        return this.series;
107    }
108
109    /**
110     * Sets the series index.
111     *
112     * @param series the series index (zero-based).
113     */
114    public void setSeriesIndex(int series) {
115        this.series = series;
116    }
117
118    /**
119     * Returns the item index.
120     *
121     * @return The item index.
122     */
123    public int getItem() {
124        return this.item;
125    }
126
127    /**
128     * Sets the item index.
129     *
130     * @param item the item index (zero-based).
131     */
132    public void setItem(int item) {
133        this.item = item;
134    }
135
136    /**
137     * Tests the entity for equality with an arbitrary object.
138     *
139     * @param obj  the object ({@code null} permitted).
140     *
141     * @return A boolean.
142     */
143    @Override
144    public boolean equals(Object obj) {
145        if (this == obj) {
146            return true;
147        }
148        if (!(obj instanceof XYItemEntity)) {
149            return false;
150        }
151        XYItemEntity that = (XYItemEntity) obj;
152
153        // fix the "equals not symmetric" problem
154        if (!that.canEqual(this)) {
155            return false;
156        }
157        // compare fields in this class
158        if (this.series != that.series) {
159            return false;
160        }
161        if (this.item != that.item) {
162            return false;
163        }
164        return super.equals(obj);
165    }
166
167    /**
168     * Ensures symmetry between super/subclass implementations of equals. For
169     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
170     *
171     * @param other Object
172     * 
173     * @return true ONLY if the parameter is THIS class type
174     */
175    @Override
176    public boolean canEqual(Object other) {
177        // Solves Problem: equals not symmetric
178        return (other instanceof XYItemEntity);
179    }
180
181    @Override
182    public int hashCode() {
183        int hash = super.hashCode(); // equals calls superclass function, so hashCode must also
184        hash = 37 * hash + this.series;
185        hash = 37 * hash + this.item;
186        return hash;
187    }
188
189    /**
190     * Returns a string representation of this instance, useful for debugging
191     * purposes.
192     *
193     * @return A string.
194     */
195    @Override
196    public String toString() {
197        return "XYItemEntity: series = " + getSeriesIndex() + ", item = "
198            + getItem() + ", dataset = " + getDataset();
199    }
200
201}