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 * LegendItemEntity.java
029 * ---------------------
030 * (C) Copyright 2003-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
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    /**
065     * Creates a legend item entity.
066     *
067     * @param area  the area.
068     */
069    public LegendItemEntity(Shape area) {
070        super(area);
071    }
072
073    /**
074     * Returns a reference to the dataset that this legend item is derived
075     * from.
076     *
077     * @return The dataset.
078     *
079     * @see #setDataset(Dataset)
080     */
081    public Dataset getDataset() {
082        return this.dataset;
083    }
084
085    /**
086     * Sets a reference to the dataset that this legend item is derived from.
087     *
088     * @param dataset  the dataset.
089     */
090    public void setDataset(Dataset dataset) {
091        this.dataset = dataset;
092    }
093
094    /**
095     * Returns the series key that identifies the legend item.
096     *
097     * @return The series key.
098     *
099     * @see #setSeriesKey(Comparable)
100     */
101    public Comparable getSeriesKey() {
102        return this.seriesKey;
103    }
104
105    /**
106     * Sets the key for the series.
107     *
108     * @param key  the key.
109     *
110     * @see #getSeriesKey()
111     */
112    public void setSeriesKey(Comparable key) {
113        this.seriesKey = key;
114    }
115
116    /**
117     * Tests this object for equality with an arbitrary object.
118     *
119     * @param obj  the object ({@code null} permitted).
120     *
121     * @return A boolean.
122     */
123    @Override
124    public boolean equals(Object obj) {
125        if (obj == this) {
126            return true;
127        }
128        if (!(obj instanceof LegendItemEntity)) {
129            return false;
130        }
131        LegendItemEntity that = (LegendItemEntity) obj;
132
133        // fix the "equals not symmetric" problem
134        if (!that.canEqual(this)) {
135            return false;
136        }
137        if (!Objects.equals(this.seriesKey, that.seriesKey)) {
138            return false;
139        }
140        if (!Objects.equals(this.dataset, that.dataset)) {
141            return false;
142        }
143        return super.equals(obj);
144    }
145
146    /**
147     * Ensures symmetry between super/subclass implementations of equals. For
148     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
149     *
150     * @param other Object
151     * 
152     * @return true ONLY if the parameter is THIS class type
153     */
154    @Override
155    public boolean canEqual(Object other) {
156        // Solves Problem: equals not symmetric
157        return (other instanceof LegendItemEntity);
158    }
159
160    @Override
161    public int hashCode() {
162        int hash = super.hashCode(); // equals calls superclass function, so hashCode must also
163        hash = 97 * hash + Objects.hashCode(this.dataset);
164        hash = 97 * hash + Objects.hashCode(this.seriesKey);
165        return hash;
166    }
167
168    /**
169     * Returns a clone of the entity.
170     *
171     * @return A clone.
172     *
173     * @throws CloneNotSupportedException if there is a problem cloning the
174     *         object.
175     */
176    @Override
177    public Object clone() throws CloneNotSupportedException {
178        return super.clone();
179    }
180
181    /**
182     * Returns a string representing this object (useful for debugging
183     * purposes).
184     *
185     * @return A string (never {@code null}).
186     */
187    @Override
188    public String toString() {
189        return "LegendItemEntity: seriesKey=" + this.seriesKey
190                + ", dataset=" + this.dataset;
191    }
192
193}