001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2021, 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 * Tick.java
029 * ---------
030 * (C) Copyright 2000-2021, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Nicolas Brodu;
034 *
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            Tick t = (Tick) obj;
134            if (!Objects.equals(this.text, t.text)) {
135                return false;
136            }
137            if (!Objects.equals(this.textAnchor, t.textAnchor)) {
138                return false;
139            }
140            if (!Objects.equals(this.rotationAnchor, t.rotationAnchor)) {
141                return false;
142            }
143            if (!(this.angle == t.angle)) {
144                return false;
145            }
146            return true;
147        }
148        return false;
149    }
150
151    /**
152     * Returns a clone of the tick.
153     *
154     * @return A clone.
155     *
156     * @throws CloneNotSupportedException if there is a problem cloning.
157     */
158    @Override
159    public Object clone() throws CloneNotSupportedException {
160        Tick clone = (Tick) super.clone();
161        return clone;
162    }
163
164    /**
165     * Returns a string representation of the tick.
166     *
167     * @return A string.
168     */
169    @Override
170    public String toString() {
171        return this.text;
172    }
173}