001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, 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-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Nicolas Brodu;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.Serializable;
040import java.util.Objects;
041
042import org.jfree.chart.text.TextAnchor;
043import org.jfree.chart.internal.Args;
044
045/**
046 * The base class used to represent labeled ticks along an axis.
047 */
048public abstract class Tick implements Serializable, Cloneable {
049
050    /** For serialization. */
051    private static final long serialVersionUID = 6668230383875149773L;
052
053    /** A text version of the tick value. */
054    private String text;
055
056    /** The text anchor for the tick label. */
057    private TextAnchor textAnchor;
058
059    /** The rotation anchor for the tick label. */
060    private TextAnchor rotationAnchor;
061
062    /** The rotation angle. */
063    private double angle;
064
065    /**
066     * Creates a new tick.
067     *
068     * @param text  the formatted version of the tick value.
069     * @param textAnchor  the text anchor ({@code null} not permitted).
070     * @param rotationAnchor  the rotation anchor ({@code null} not
071     *                        permitted).
072     * @param angle  the angle.
073     */
074    public Tick(String text, TextAnchor textAnchor, TextAnchor rotationAnchor,
075                double angle) {
076        Args.nullNotPermitted(textAnchor, "textAnchor");
077        Args.nullNotPermitted(rotationAnchor, "rotationAnchor");
078        this.text = text;
079        this.textAnchor = textAnchor;
080        this.rotationAnchor = rotationAnchor;
081        this.angle = angle;
082    }
083
084    /**
085     * Returns the text version of the tick value.
086     *
087     * @return A string (possibly {@code null});
088     */
089    public String getText() {
090        return this.text;
091    }
092
093    /**
094     * Returns the text anchor.
095     *
096     * @return The text anchor (never {@code null}).
097     */
098    public TextAnchor getTextAnchor() {
099        return this.textAnchor;
100    }
101
102    /**
103     * Returns the text anchor that defines the point around which the label is
104     * rotated.
105     *
106     * @return A text anchor (never {@code null}).
107     */
108    public TextAnchor getRotationAnchor() {
109        return this.rotationAnchor;
110    }
111
112    /**
113     * Returns the angle.
114     *
115     * @return The angle.
116     */
117    public double getAngle() {
118        return this.angle;
119    }
120
121    /**
122     * Tests this tick for equality with an arbitrary object.
123     *
124     * @param obj  the object ({@code null} permitted).
125     *
126     * @return A boolean.
127     */
128    @Override
129    public boolean equals(Object obj) {
130        if (this == obj) {
131            return true;
132        }
133        if (obj instanceof Tick) {
134            Tick t = (Tick) obj;
135            if (!Objects.equals(this.text, t.text)) {
136                return false;
137            }
138            if (!Objects.equals(this.textAnchor, t.textAnchor)) {
139                return false;
140            }
141            if (!Objects.equals(this.rotationAnchor, t.rotationAnchor)) {
142                return false;
143            }
144            if (!(this.angle == t.angle)) {
145                return false;
146            }
147            return true;
148        }
149        return false;
150    }
151
152    /**
153     * Returns a clone of the tick.
154     *
155     * @return A clone.
156     *
157     * @throws CloneNotSupportedException if there is a problem cloning.
158     */
159    @Override
160    public Object clone() throws CloneNotSupportedException {
161        Tick clone = (Tick) super.clone();
162        return clone;
163    }
164
165    /**
166     * Returns a string representation of the tick.
167     *
168     * @return A string.
169     */
170    @Override
171    public String toString() {
172        return this.text;
173    }
174}