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