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 * LegendItemEntity.java
029 * ---------------------
030 * (C) Copyright 2003-2021, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.entity;
038
039import java.awt.Shape;
040import java.io.Serializable;
041import java.util.Objects;
042
043import org.jfree.data.general.Dataset;
044
045/**
046 * An entity that represents an item within a legend.
047 */
048public class LegendItemEntity extends ChartEntity
049                              implements Cloneable, Serializable {
050
051    /** For serialization. */
052    private static final long serialVersionUID = -7435683933545666702L;
053
054    /**
055     * The dataset.
056     */
057    private Dataset dataset;
058
059    /**
060     * The series key.
061     */
062    private Comparable seriesKey;
063
064    /** The series index. */
065    private int seriesIndex;
066
067    /**
068     * Creates a legend item entity.
069     *
070     * @param area  the area.
071     */
072    public LegendItemEntity(Shape area) {
073        super(area);
074    }
075
076    /**
077     * Returns a reference to the dataset that this legend item is derived
078     * from.
079     *
080     * @return The dataset.
081     *
082     * @see #setDataset(Dataset)
083     */
084    public Dataset getDataset() {
085        return this.dataset;
086    }
087
088    /**
089     * Sets a reference to the dataset that this legend item is derived from.
090     *
091     * @param dataset  the dataset.
092     */
093    public void setDataset(Dataset dataset) {
094        this.dataset = dataset;
095    }
096
097    /**
098     * Returns the series key that identifies the legend item.
099     *
100     * @return The series key.
101     *
102     * @see #setSeriesKey(Comparable)
103     */
104    public Comparable getSeriesKey() {
105        return this.seriesKey;
106    }
107
108    /**
109     * Sets the key for the series.
110     *
111     * @param key  the key.
112     *
113     * @see #getSeriesKey()
114     */
115    public void setSeriesKey(Comparable key) {
116        this.seriesKey = key;
117    }
118
119    /**
120     * Tests this object for equality with an arbitrary object.
121     *
122     * @param obj  the object ({@code null} permitted).
123     *
124     * @return A boolean.
125     */
126    @Override
127    public boolean equals(Object obj) {
128        if (obj == this) {
129            return true;
130        }
131        if (!(obj instanceof LegendItemEntity)) {
132            return false;
133        }
134        LegendItemEntity that = (LegendItemEntity) obj;
135        if (!Objects.equals(this.seriesKey, that.seriesKey)) {
136            return false;
137        }
138        if (this.seriesIndex != that.seriesIndex) {
139            return false;
140        }
141        if (!Objects.equals(this.dataset, that.dataset)) {
142            return false;
143        }
144        return super.equals(obj);
145    }
146
147    /**
148     * Returns a clone of the entity.
149     *
150     * @return A clone.
151     *
152     * @throws CloneNotSupportedException if there is a problem cloning the
153     *         object.
154     */
155    @Override
156    public Object clone() throws CloneNotSupportedException {
157        return super.clone();
158    }
159
160    /**
161     * Returns a string representing this object (useful for debugging
162     * purposes).
163     *
164     * @return A string (never {@code null}).
165     */
166    @Override
167    public String toString() {
168        return "LegendItemEntity: seriesKey=" + this.seriesKey
169                + ", dataset=" + this.dataset;
170    }
171
172}