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 * Tick.java
029 * ---------
030 * (C) Copyright 2000-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Nicolas Brodu;
034 *                   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.Serializable;
040import java.util.Objects;
041import org.jfree.chart.ui.TextAnchor;
042import org.jfree.chart.util.Args;
043
044/**
045 * The base class used to represent labeled ticks along an axis.
046 */
047public abstract class Tick implements Serializable, Cloneable {
048
049    /** For serialization. */
050    private static final long serialVersionUID = 6668230383875149773L;
051
052    /** A text version of the tick value. */
053    private String text;
054
055    /** The text anchor for the tick label. */
056    private TextAnchor textAnchor;
057
058    /** The rotation anchor for the tick label. */
059    private TextAnchor rotationAnchor;
060
061    /** The rotation angle. */
062    private double angle;
063
064    /**
065     * Creates a new tick.
066     *
067     * @param text  the formatted version of the tick value.
068     * @param textAnchor  the text anchor ({@code null} not permitted).
069     * @param rotationAnchor  the rotation anchor ({@code null} not
070     *                        permitted).
071     * @param angle  the angle.
072     */
073    public Tick(String text, TextAnchor textAnchor, TextAnchor rotationAnchor,
074                double angle) {
075        Args.nullNotPermitted(textAnchor, "textAnchor");
076        Args.nullNotPermitted(rotationAnchor, "rotationAnchor");
077        this.text = text;
078        this.textAnchor = textAnchor;
079        this.rotationAnchor = rotationAnchor;
080        this.angle = angle;
081    }
082
083    /**
084     * Returns the text version of the tick value.
085     *
086     * @return A string (possibly {@code null});
087     */
088    public String getText() {
089        return this.text;
090    }
091
092    /**
093     * Returns the text anchor.
094     *
095     * @return The text anchor (never {@code null}).
096     */
097    public TextAnchor getTextAnchor() {
098        return this.textAnchor;
099    }
100
101    /**
102     * Returns the text anchor that defines the point around which the label is
103     * rotated.
104     *
105     * @return A text anchor (never {@code null}).
106     */
107    public TextAnchor getRotationAnchor() {
108        return this.rotationAnchor;
109    }
110
111    /**
112     * Returns the angle.
113     *
114     * @return The angle.
115     */
116    public double getAngle() {
117        return this.angle;
118    }
119
120    /**
121     * Tests this tick for equality with an arbitrary object.
122     *
123     * @param obj  the object ({@code null} permitted).
124     *
125     * @return A boolean.
126     */
127    @Override
128    public boolean equals(Object obj) {
129        if (this == obj) {
130            return true;
131        }
132        if (!(obj instanceof Tick)) {
133            return false;
134        }
135        Tick that = (Tick) obj;
136        if (Double.doubleToLongBits(this.angle) !=
137            Double.doubleToLongBits(that.angle)) {
138            return false;
139        }
140        if (!Objects.equals(this.text, that.text)) {
141            return false;
142        }
143        if (!Objects.equals(this.textAnchor, that.textAnchor)) {
144            return false;
145        }
146        if (!Objects.equals(this.rotationAnchor, that.rotationAnchor)) {
147            return false;
148        }
149        if (!that.canEqual(this)) {
150            return false;
151        }
152        return true;
153    }
154
155    /**
156     * Ensures symmetry between super/subclass implementations of equals. For
157     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
158     *
159     * @param other Object
160     * 
161     * @return true ONLY if the parameter is THIS class type
162     */
163    public boolean canEqual(Object other) {
164        // fix the "equals not symmetric" problem
165        return (other instanceof Tick);
166    }
167
168    @Override
169    public int hashCode() {
170        int hash = 7;
171        hash = 79 * hash + Objects.hashCode(this.text);
172        hash = 79 * hash + Objects.hashCode(this.textAnchor);
173        hash = 79 * hash + Objects.hashCode(this.rotationAnchor);
174        hash = 79 * hash + (int) (Double.doubleToLongBits(this.angle) ^
175                                 (Double.doubleToLongBits(this.angle) >>> 32));
176        return hash;
177    }
178
179    /**
180     * Returns a clone of the tick.
181     *
182     * @return A clone.
183     *
184     * @throws CloneNotSupportedException if there is a problem cloning.
185     */
186    @Override
187    public Object clone() throws CloneNotSupportedException {
188        Tick clone = (Tick) super.clone();
189        return clone;
190    }
191
192    /**
193     * Returns a string representation of the tick.
194     *
195     * @return A string.
196     */
197    @Override
198    public String toString() {
199        return this.text;
200    }
201}