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 * CategoryItemEntity.java
029 * -----------------------
030 * (C) Copyright 2002-2022, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Richard Atkinson;
034 *                   Christian W. Zuckschwerdt;
035 *
036 */
037
038package org.jfree.chart.entity;
039
040import java.awt.Shape;
041import java.io.Serializable;
042import java.util.Objects;
043
044import org.jfree.chart.internal.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<R extends Comparable<R>, C extends Comparable<C>> 
052        extends ChartEntity implements Cloneable, Serializable {
053
054    /** For serialization. */
055    private static final long serialVersionUID = -8657249457902337349L;
056
057    /** The dataset. */
058    private CategoryDataset<R, C> dataset;
059
060    /** The row key. */
061    private R rowKey;
062
063    /** The column key. */
064    private C columnKey;
065
066    /**
067     * Creates a new entity instance for an item in the specified dataset.
068     *
069     * @param area  the 'hotspot' area ({@code null} not permitted).
070     * @param toolTipText  the tool tip text.
071     * @param urlText  the URL text.
072     * @param dataset  the dataset ({@code null} not permitted).
073     * @param rowKey  the row key ({@code null} not permitted).
074     * @param columnKey  the column key ({@code null} not permitted).
075     */
076    public CategoryItemEntity(Shape area, String toolTipText, String urlText,
077            CategoryDataset dataset, R rowKey, C columnKey) {
078        super(area, toolTipText, urlText);
079        Args.nullNotPermitted(dataset, "dataset");
080        this.dataset = dataset;
081        this.rowKey = rowKey;
082        this.columnKey = columnKey;
083    }
084
085    /**
086     * Returns the dataset this entity refers to.  This can be used to
087     * differentiate between items in a chart that displays more than one
088     * dataset.
089     *
090     * @return The dataset (never {@code null}).
091     *
092     * @see #setDataset(CategoryDataset)
093     */
094    public CategoryDataset<R, C> getDataset() {
095        return this.dataset;
096    }
097
098    /**
099     * Sets the dataset this entity refers to.
100     *
101     * @param dataset  the dataset ({@code null} not permitted).
102     *
103     * @see #getDataset()
104     */
105    public void setDataset(CategoryDataset<R, C> dataset) {
106        Args.nullNotPermitted(dataset, "dataset");
107        this.dataset = dataset;
108    }
109
110    /**
111     * Returns the row key.
112     *
113     * @return The row key (never {@code null}).
114     *
115     * @see #setRowKey(Comparable)
116     */
117    public R getRowKey() {
118        return this.rowKey;
119    }
120
121    /**
122     * Sets the row key.
123     *
124     * @param rowKey  the row key ({@code null} not permitted).
125     *
126     * @see #getRowKey()
127     */
128    public void setRowKey(R rowKey) {
129        this.rowKey = rowKey;
130    }
131
132    /**
133     * Returns the column key.
134     *
135     * @return The column key (never {@code null}).
136     *
137     * @see #setColumnKey(Comparable)
138     */
139    public C getColumnKey() {
140        return this.columnKey;
141    }
142
143    /**
144     * Sets the column key.
145     *
146     * @param columnKey  the column key ({@code null} not permitted).
147     *
148     * @see #getColumnKey()
149     */
150    public void setColumnKey(C columnKey) {
151        this.columnKey = columnKey;
152    }
153
154    /**
155     * Returns a string representing this object (useful for debugging
156     * purposes).
157     *
158     * @return A string (never {@code null}).
159     */
160    @Override
161    public String toString() {
162        return "CategoryItemEntity: rowKey=" + this.rowKey
163               + ", columnKey=" + this.columnKey + ", dataset=" + this.dataset;
164    }
165
166    /**
167     * Tests the entity for equality with an arbitrary object.
168     *
169     * @param obj  the object ({@code null} permitted).
170     *
171     * @return A boolean.
172     */
173    @Override
174    public boolean equals(Object obj) {
175        if (obj == this) {
176            return true;
177        }
178        if (!(obj instanceof CategoryItemEntity)) {
179            return false;
180        }
181        CategoryItemEntity that = (CategoryItemEntity) obj;
182        if (!this.rowKey.equals(that.rowKey)) {
183            return false;
184        }
185        if (!this.columnKey.equals(that.columnKey)) {
186            return false;
187        }
188        if (!Objects.equals(this.dataset, that.dataset)) {
189            return false;
190        }
191
192        return super.equals(obj);
193    }
194
195}