001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * AxisEntity.java
029 * ----------------
030 * (C) Copyright 2009-2020, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  Peter Kolb;
033 * Contributor(s):   ;
034 *
035 */
036
037package org.jfree.chart.entity;
038
039import java.awt.Shape;
040import java.io.IOException;
041import java.io.ObjectInputStream;
042import java.io.ObjectOutputStream;
043
044import org.jfree.chart.HashUtils;
045import org.jfree.chart.axis.Axis;
046import org.jfree.chart.util.ObjectUtils;
047import org.jfree.chart.util.Args;
048import org.jfree.chart.util.SerialUtils;
049
050/**
051 * A class that captures information about an {@link Axis} belonging to a 
052 * chart.
053 *
054 * @since 1.0.13
055 */
056public class AxisEntity extends ChartEntity {
057
058    /** For serialization. */
059    private static final long serialVersionUID = -4445994133561919083L;
060                  //same as for ChartEntity!
061
062    /** The axis for the entity. */
063    private Axis axis;
064
065    /**
066     * Creates a new axis entity.
067     *
068     * @param area  the area ({@code null} not permitted).
069     * @param axis  the axis ({@code null} not permitted).
070     */
071    public AxisEntity(Shape area, Axis axis) {
072        // defer argument checks...
073        this(area, axis, null);
074    }
075
076    /**
077     * Creates a new axis entity.
078     *
079     * @param area  the area ({@code null} not permitted).
080     * @param axis  the axis ({@code null} not permitted).
081     * @param toolTipText  the tool tip text ({@code null} permitted).
082     */
083    public AxisEntity(Shape area, Axis axis, String toolTipText) {
084        // defer argument checks...
085        this(area, axis, toolTipText, null);
086    }
087
088    /**
089     * Creates a new axis entity.
090     *
091     * @param area  the area ({@code null} not permitted).
092     * @param axis  the axis ({@code null} not permitted).
093     * @param toolTipText  the tool tip text ({@code null} permitted).
094     * @param urlText  the URL text for HTML image maps ({@code null}
095     *                 permitted).
096     */
097    public AxisEntity(Shape area, Axis axis, String toolTipText,
098            String urlText) {
099        super(area, toolTipText, urlText);
100        Args.nullNotPermitted(axis, "axis");
101        this.axis = axis;
102    }
103
104    /**
105     * Returns the axis that occupies the entity area.
106     *
107     * @return The axis (never {@code null}).
108     */
109    public Axis getAxis() {
110        return this.axis;
111    }
112
113    /**
114     * Returns a string representation of the chart entity, useful for
115     * debugging.
116     *
117     * @return A string.
118     */
119    @Override
120    public String toString() {
121        StringBuilder sb = new StringBuilder("AxisEntity: ");
122        sb.append("tooltip = ");
123        sb.append(getToolTipText());
124        return sb.toString();
125    }
126
127    /**
128     * Tests the entity for equality with an arbitrary object.
129     *
130     * @param obj  the object to test against ({@code null} permitted).
131     *
132     * @return A boolean.
133     */
134    @Override
135    public boolean equals(Object obj) {
136        if (obj == this) {
137            return true;
138        }
139        if (!(obj instanceof AxisEntity)) {
140            return false;
141        }
142        AxisEntity that = (AxisEntity) obj;
143        if (!getArea().equals(that.getArea())) {
144            return false;
145        }
146        if (!ObjectUtils.equal(getToolTipText(), that.getToolTipText())) {
147            return false;
148        }
149        if (!ObjectUtils.equal(getURLText(), that.getURLText())) {
150            return false;
151        }
152        if (!(this.axis.equals(that.axis))) {
153            return false;
154        }
155        return true;
156    }
157
158    /**
159     * Returns a hash code for this instance.
160     *
161     * @return A hash code.
162     */
163    @Override
164    public int hashCode() {
165        int result = 39;
166        result = HashUtils.hashCode(result, getToolTipText());
167        result = HashUtils.hashCode(result, getURLText());
168        return result;
169    }
170
171    /**
172     * Returns a clone of the entity.
173     *
174     * @return A clone.
175     *
176     * @throws CloneNotSupportedException if there is a problem cloning the
177     *         entity.
178     */
179    @Override
180    public Object clone() throws CloneNotSupportedException {
181        return super.clone();
182    }
183
184    /**
185     * Provides serialization support.
186     *
187     * @param stream  the output stream.
188     *
189     * @throws IOException  if there is an I/O error.
190     */
191    private void writeObject(ObjectOutputStream stream) throws IOException {
192        stream.defaultWriteObject();
193        SerialUtils.writeShape(getArea(), stream);
194    }
195
196    /**
197     * Provides serialization support.
198     *
199     * @param stream  the input stream.
200     *
201     * @throws IOException  if there is an I/O error.
202     * @throws ClassNotFoundException  if there is a classpath problem.
203     */
204    private void readObject(ObjectInputStream stream)
205            throws IOException, ClassNotFoundException {
206        stream.defaultReadObject();
207        setArea(SerialUtils.readShape(stream));
208    }
209
210}