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