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 * CategoryLabelEntity.java
029 * ------------------------
030 * (C) Copyright 2006-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.util.Objects;
041
042import org.jfree.chart.HashUtils;
043import org.jfree.chart.axis.CategoryAxis;
044
045/**
046 * An entity to represent the labels on a {@link CategoryAxis}.
047 */
048public class CategoryLabelEntity extends TickLabelEntity {
049
050    /** The category key. */
051    private final Comparable key;
052
053    /**
054     * Creates a new entity.
055     *
056     * @param key  the category key ({@code null} not permitted).
057     * @param area  the hotspot.
058     * @param toolTipText  the tool tip text.
059     * @param urlText  the URL text.
060     */
061    public CategoryLabelEntity(Comparable key, Shape area,
062            String toolTipText, String urlText) {
063        super(area, toolTipText, urlText);
064        Objects.requireNonNull(key);
065        this.key = key;
066    }
067
068    /**
069     * Returns the category key.
070     *
071     * @return The category key.
072     */
073    public Comparable getKey() {
074        return this.key;
075    }
076
077    /**
078     * Tests this instance for equality with an arbitrary object.
079     *
080     * @param obj  the object ({@code null} permitted).
081     *
082     * @return A boolean.
083     */
084    @Override
085    public boolean equals(Object obj) {
086        if (obj == this) {
087            return true;
088        }
089        if (!(obj instanceof CategoryLabelEntity)) {
090            return false;
091        }
092        CategoryLabelEntity that = (CategoryLabelEntity) obj;
093
094        // fix the "equals not symmetric" problem
095        if (!that.canEqual(this)) {
096            return false;
097        }
098        if (!Objects.equals(this.key, that.key)) {
099            return false;
100        }
101        return super.equals(obj);
102    }
103
104    /**
105     * Ensures symmetry between super/subclass implementations of equals. For
106     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
107     *
108     * @param other Object
109     * 
110     * @return true ONLY if the parameter is THIS class type
111     */
112    @Override
113    public boolean canEqual(Object other) {
114        // Solves Problem: equals not symmetric
115        return (other instanceof CategoryLabelEntity);
116    }
117
118    /**
119     * Returns a hash code for this instance.
120     *
121     * @return A hash code.
122     */
123    @Override
124    public int hashCode() {
125        int result = super.hashCode(); // equals calls superclass, hashCode must also
126        result = HashUtils.hashCode(result, this.key);
127        return result;
128    }
129
130    /**
131     * Returns a string representation of this entity.  This is primarily
132     * useful for debugging.
133     *
134     * @return A string representation of this entity.
135     */
136    @Override
137    public String toString() {
138        StringBuilder sb = new StringBuilder("CategoryLabelEntity: ");
139        sb.append("category=");
140        sb.append(this.key);
141        sb.append(", tooltip=").append(getToolTipText());
142        sb.append(", url=").append(getURLText());
143        return sb.toString();
144    }
145}