001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2021, by Object Refinery Limited 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-2021, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 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.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 extends ChartEntity 051 implements Serializable { 052 053 /** For serialization. */ 054 private static final long serialVersionUID = 9199892576531984162L; 055 056 /** The dataset. */ 057 private PieDataset 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 Comparable 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, 080 PieDataset dataset, 081 int pieIndex, int sectionIndex, 082 Comparable sectionKey, 083 String toolTipText, String urlText) { 084 085 super(area, toolTipText, urlText); 086 this.dataset = dataset; 087 this.pieIndex = pieIndex; 088 this.sectionIndex = sectionIndex; 089 this.sectionKey = sectionKey; 090 091 } 092 093 /** 094 * Returns the dataset this entity refers to. 095 * 096 * @return The dataset. 097 * 098 * @see #setDataset(PieDataset) 099 */ 100 public PieDataset getDataset() { 101 return this.dataset; 102 } 103 104 /** 105 * Sets the dataset this entity refers to. 106 * 107 * @param dataset the dataset. 108 * 109 * @see #getDataset() 110 */ 111 public void setDataset(PieDataset dataset) { 112 this.dataset = dataset; 113 } 114 115 /** 116 * Returns the pie index. For a regular pie chart, the section index is 0. 117 * For a pie chart containing multiple pie plots, the pie index is the row 118 * or column index from which the pie data is extracted. 119 * 120 * @return The pie index. 121 * 122 * @see #setPieIndex(int) 123 */ 124 public int getPieIndex() { 125 return this.pieIndex; 126 } 127 128 /** 129 * Sets the pie index. 130 * 131 * @param index the new index value. 132 * 133 * @see #getPieIndex() 134 */ 135 public void setPieIndex(int index) { 136 this.pieIndex = index; 137 } 138 139 /** 140 * Returns the section index. 141 * 142 * @return The section index. 143 * 144 * @see #setSectionIndex(int) 145 */ 146 public int getSectionIndex() { 147 return this.sectionIndex; 148 } 149 150 /** 151 * Sets the section index. 152 * 153 * @param index the section index. 154 * 155 * @see #getSectionIndex() 156 */ 157 public void setSectionIndex(int index) { 158 this.sectionIndex = index; 159 } 160 161 /** 162 * Returns the section key. 163 * 164 * @return The section key. 165 * 166 * @see #setSectionKey(Comparable) 167 */ 168 public Comparable getSectionKey() { 169 return this.sectionKey; 170 } 171 172 /** 173 * Sets the section key. 174 * 175 * @param key the section key. 176 * 177 * @see #getSectionKey() 178 */ 179 public void setSectionKey(Comparable key) { 180 this.sectionKey = key; 181 } 182 183 /** 184 * Tests this entity for equality with an arbitrary object. 185 * 186 * @param obj the object ({@code null} permitted). 187 * 188 * @return A boolean. 189 */ 190 @Override 191 public boolean equals(Object obj) { 192 if (obj == this) { 193 return true; 194 } 195 if (!(obj instanceof PieSectionEntity)) { 196 return false; 197 } 198 PieSectionEntity that = (PieSectionEntity) obj; 199 if (!Objects.equals(this.dataset, that.dataset)) { 200 return false; 201 } 202 if (this.pieIndex != that.pieIndex) { 203 return false; 204 } 205 if (this.sectionIndex != that.sectionIndex) { 206 return false; 207 } 208 if (!Objects.equals(this.sectionKey, that.sectionKey)) { 209 return false; 210 } 211 return super.equals(obj); 212 } 213 214 /** 215 * Returns a hash code for this instance. 216 * 217 * @return A hash code. 218 */ 219 @Override 220 public int hashCode() { 221 int result = super.hashCode(); 222 result = HashUtils.hashCode(result, this.pieIndex); 223 result = HashUtils.hashCode(result, this.sectionIndex); 224 return result; 225 } 226 227 /** 228 * Returns a string representing the entity. 229 * 230 * @return A string representing the entity. 231 */ 232 @Override 233 public String toString() { 234 return "PieSection: " + this.pieIndex + ", " + this.sectionIndex + "(" 235 + this.sectionKey.toString() + ")"; 236 } 237 238}