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 * CategoryItemEntity.java
029 * -----------------------
030 * (C) Copyright 2002-present, by David Gilbert and Contributors.
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;
042import java.io.Serializable;
043import java.util.Objects;
044import org.jfree.chart.util.Args;
045
046import org.jfree.data.category.CategoryDataset;
047
048/**
049 * A chart entity that represents one item within a category plot.
050 */
051public class CategoryItemEntity extends ChartEntity
052        implements Cloneable, Serializable {
053
054    /** For serialization. */
055    private static final long serialVersionUID = -8657249457902337349L;
056
057    /** The dataset. */
058    private CategoryDataset dataset;
059
060    /**
061     * The row key.
062     */
063    private Comparable rowKey;
064
065    /**
066     * The column key.
067     */
068    private Comparable columnKey;
069
070    /**
071     * Creates a new entity instance for an item in the specified dataset.
072     *
073     * @param area  the 'hotspot' area ({@code null} not permitted).
074     * @param toolTipText  the tool tip text.
075     * @param urlText  the URL text.
076     * @param dataset  the dataset ({@code null} not permitted).
077     * @param rowKey  the row key ({@code null} not permitted).
078     * @param columnKey  the column key ({@code null} not permitted).
079     */
080    public CategoryItemEntity(Shape area, String toolTipText, String urlText,
081            CategoryDataset dataset, Comparable rowKey, Comparable columnKey) {
082        super(area, toolTipText, urlText);
083        Args.nullNotPermitted(dataset, "dataset");
084        this.dataset = dataset;
085        this.rowKey = rowKey;
086        this.columnKey = columnKey;
087    }
088
089    /**
090     * Returns the dataset this entity refers to.  This can be used to
091     * differentiate between items in a chart that displays more than one
092     * dataset.
093     *
094     * @return The dataset (never {@code null}).
095     *
096     * @see #setDataset(CategoryDataset)
097     */
098    public CategoryDataset getDataset() {
099        return this.dataset;
100    }
101
102    /**
103     * Sets the dataset this entity refers to.
104     *
105     * @param dataset  the dataset ({@code null} not permitted).
106     *
107     * @see #getDataset()
108     */
109    public void setDataset(CategoryDataset dataset) {
110        Args.nullNotPermitted(dataset, "dataset");
111        this.dataset = dataset;
112    }
113
114    /**
115     * Returns the row key.
116     *
117     * @return The row key (never {@code null}).
118     *
119     * @see #setRowKey(Comparable)
120     */
121    public Comparable getRowKey() {
122        return this.rowKey;
123    }
124
125    /**
126     * Sets the row key.
127     *
128     * @param rowKey  the row key ({@code null} not permitted).
129     *
130     * @see #getRowKey()
131     */
132    public void setRowKey(Comparable rowKey) {
133        this.rowKey = rowKey;
134    }
135
136    /**
137     * Returns the column key.
138     *
139     * @return The column key (never {@code null}).
140     *
141     * @see #setColumnKey(Comparable)
142     */
143    public Comparable getColumnKey() {
144        return this.columnKey;
145    }
146
147    /**
148     * Sets the column key.
149     *
150     * @param columnKey  the column key ({@code null} not permitted).
151     *
152     * @see #getColumnKey()
153     */
154    public void setColumnKey(Comparable columnKey) {
155        this.columnKey = columnKey;
156    }
157
158    /**
159     * Returns a string representing this object (useful for debugging
160     * purposes).
161     *
162     * @return A string (never {@code null}).
163     */
164    @Override
165    public String toString() {
166        return "CategoryItemEntity: rowKey=" + this.rowKey
167               + ", columnKey=" + this.columnKey + ", dataset=" + this.dataset;
168    }
169
170    /**
171     * Tests the entity for equality with an arbitrary object.
172     *
173     * @param obj  the object ({@code null} permitted).
174     *
175     * @return A boolean.
176     */
177    @Override
178    public boolean equals(Object obj) {
179        if (obj == this) {
180            return true;
181        }
182        if (!(obj instanceof CategoryItemEntity)) {
183            return false;
184        }
185        CategoryItemEntity that = (CategoryItemEntity) obj;
186        if (!Objects.equals(this.rowKey, that.rowKey)) {
187            return false;
188        }
189        if (!Objects.equals(this.columnKey, that.columnKey)) {
190            return false;
191        }
192        if (!Objects.equals(this.dataset, that.dataset)) {
193            return false;
194        }
195        
196        // fix the "equals not symmetric" problem
197        if (!that.canEqual(this)) {
198            return false;
199        }
200        
201        return super.equals(obj);
202    }
203
204    /**
205     * Ensures symmetry between super/subclass implementations of equals. For
206     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
207     *
208     * @param other Object
209     * 
210     * @return true ONLY if the parameter is THIS class type
211     */
212    @Override
213    public boolean canEqual(Object other) {
214        // fix the "equals not symmetric" problem
215        return (other instanceof CategoryItemEntity);
216    }
217
218    @Override
219    public int hashCode() {
220        int hash = super.hashCode(); // equals calls superclass, hashCode must also
221        hash = 37 * hash + Objects.hashCode(this.dataset);
222        hash = 37 * hash + Objects.hashCode(this.rowKey);
223        hash = 37 * hash + Objects.hashCode(this.columnKey);
224        return hash;
225    }
226
227}