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 * PieSectionEntity.java
029 * ---------------------
030 * (C) Copyright 2002-2022, by David Gilbert.
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.HashUtils;
045import org.jfree.data.general.PieDataset;
046
047/**
048 * A chart entity that represents one section within a pie plot.
049 */
050public class PieSectionEntity<K extends Comparable<K>> extends ChartEntity 
051        implements Serializable {
052
053    /** For serialization. */
054    private static final long serialVersionUID = 9199892576531984162L;
055
056    /** The dataset. */
057    private PieDataset<K> dataset;
058
059    /** The pie index. */
060    private int pieIndex;
061
062    /** The section index. */
063    private int sectionIndex;
064
065    /** The section key. */
066    private K sectionKey;
067
068    /**
069     * Creates a new pie section entity.
070     *
071     * @param area  the area.
072     * @param dataset  the pie dataset.
073     * @param pieIndex  the pie index (zero-based).
074     * @param sectionIndex  the section index (zero-based).
075     * @param sectionKey  the section key.
076     * @param toolTipText  the tool tip text.
077     * @param urlText  the URL text for HTML image maps.
078     */
079    public PieSectionEntity(Shape area, PieDataset dataset, int pieIndex, 
080            int sectionIndex, K sectionKey, String toolTipText, String urlText) {
081
082        super(area, toolTipText, urlText);
083        this.dataset = dataset;
084        this.pieIndex = pieIndex;
085        this.sectionIndex = sectionIndex;
086        this.sectionKey = sectionKey;
087
088    }
089
090    /**
091     * Returns the dataset this entity refers to.
092     *
093     * @return The dataset.
094     *
095     * @see #setDataset(PieDataset)
096     */
097    public PieDataset<K> getDataset() {
098        return this.dataset;
099    }
100
101    /**
102     * Sets the dataset this entity refers to.
103     *
104     * @param dataset  the dataset.
105     *
106     * @see #getDataset()
107     */
108    public void setDataset(PieDataset<K> dataset) {
109        this.dataset = dataset;
110    }
111
112    /**
113     * Returns the pie index.  For a regular pie chart, the section index is 0.
114     * For a pie chart containing multiple pie plots, the pie index is the row
115     * or column index from which the pie data is extracted.
116     *
117     * @return The pie index.
118     *
119     * @see #setPieIndex(int)
120     */
121    public int getPieIndex() {
122        return this.pieIndex;
123    }
124
125    /**
126     * Sets the pie index.
127     *
128     * @param index  the new index value.
129     *
130     * @see #getPieIndex()
131     */
132    public void setPieIndex(int index) {
133        this.pieIndex = index;
134    }
135
136    /**
137     * Returns the section index.
138     *
139     * @return The section index.
140     *
141     * @see #setSectionIndex(int)
142     */
143    public int getSectionIndex() {
144        return this.sectionIndex;
145    }
146
147    /**
148     * Sets the section index.
149     *
150     * @param index  the section index.
151     *
152     * @see #getSectionIndex()
153     */
154    public void setSectionIndex(int index) {
155        this.sectionIndex = index;
156    }
157
158    /**
159     * Returns the section key.
160     *
161     * @return The section key.
162     *
163     * @see #setSectionKey(Comparable)
164     */
165    public K getSectionKey() {
166        return this.sectionKey;
167    }
168
169    /**
170     * Sets the section key.
171     *
172     * @param key  the section key.
173     *
174     * @see #getSectionKey()
175     */
176    public void setSectionKey(K key) {
177        this.sectionKey = key;
178    }
179
180    /**
181     * Tests this entity for equality with an arbitrary object.
182     *
183     * @param obj  the object ({@code null} permitted).
184     *
185     * @return A boolean.
186     */
187    @Override
188    public boolean equals(Object obj) {
189        if (obj == this) {
190            return true;
191        }
192        if (!(obj instanceof PieSectionEntity)) {
193            return false;
194        }
195        PieSectionEntity that = (PieSectionEntity) obj;
196        if (!Objects.equals(this.dataset, that.dataset)) {
197            return false;
198        }
199        if (this.pieIndex != that.pieIndex) {
200            return false;
201        }
202        if (this.sectionIndex != that.sectionIndex) {
203            return false;
204        }
205        if (!Objects.equals(this.sectionKey, that.sectionKey)) {
206            return false;
207        }
208        return super.equals(obj);
209    }
210
211    /**
212     * Returns a hash code for this instance.
213     *
214     * @return A hash code.
215     */
216    @Override
217    public int hashCode() {
218        int result = super.hashCode();
219        result = HashUtils.hashCode(result, this.pieIndex);
220        result = HashUtils.hashCode(result, this.sectionIndex);
221        return result;
222    }
223
224    /**
225     * Returns a string representing the entity.
226     *
227     * @return A string representing the entity.
228     */
229    @Override
230    public String toString() {
231        return "PieSection: " + this.pieIndex + ", " + this.sectionIndex + "("
232                              + this.sectionKey.toString() + ")";
233    }
234
235}