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