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 * TickUnit.java
029 * -------------
030 * (C) Copyright 2001-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.Serializable;
040
041/**
042 * Base class representing a tick unit.  This determines the spacing of the
043 * tick marks on an axis.
044 * <P>
045 * This class (and any subclasses) should be immutable, the reason being that
046 * ORDERED collections of tick units are maintained and if one instance can be
047 * changed, it may destroy the order of the collection that it belongs to.
048 * In addition, if the implementations are immutable, they can belong to
049 * multiple collections.
050 *
051 * @see ValueAxis
052 */
053public abstract class TickUnit implements Comparable, Serializable {
054
055    /** For serialization. */
056    private static final long serialVersionUID = 510179855057013974L;
057
058    /** The size of the tick unit. */
059    private double size;
060
061    /**
062     * The number of minor ticks.
063     */
064    private int minorTickCount;
065
066    /**
067     * Constructs a new tick unit.
068     *
069     * @param size  the tick unit size.
070     */
071    public TickUnit(double size) {
072        this.size = size;
073    }
074
075    /**
076     * Constructs a new tick unit.
077     *
078     * @param size  the tick unit size.
079     * @param minorTickCount  the minor tick count.
080     */
081    public TickUnit(double size, int minorTickCount) {
082        this.size = size;
083        this.minorTickCount = minorTickCount;
084    }
085
086    /**
087     * Returns the size of the tick unit.
088     *
089     * @return The size of the tick unit.
090     */
091    public double getSize() {
092        return this.size;
093    }
094
095    /**
096     * Returns the minor tick count.
097     *
098     * @return The minor tick count.
099     */
100    public int getMinorTickCount() {
101        return this.minorTickCount;
102    }
103
104    /**
105     * Converts the supplied value to a string.
106     * <P>
107     * Subclasses may implement special formatting by overriding this method.
108     *
109     * @param value  the data value.
110     *
111     * @return Value as string.
112     */
113    public String valueToString(double value) {
114        return String.valueOf(value);
115    }
116
117    /**
118     * Compares this tick unit to an arbitrary object.
119     *
120     * @param object  the object to compare against.
121     *
122     * @return {@code 1} if the size of the other object is less than this,
123     *      {@code 0} if both have the same size and {@code -1} this
124     *      size is less than the others.
125     */
126    @Override
127    public int compareTo(Object object) {
128
129        if (object instanceof TickUnit) {
130            TickUnit other = (TickUnit) object;
131            if (this.size > other.getSize()) {
132                return 1;
133            }
134            else if (this.size < other.getSize()) {
135                return -1;
136            }
137            else {
138                return 0;
139            }
140        }
141        else {
142            return -1;
143        }
144
145    }
146
147    /**
148     * Tests this unit for equality with another object.
149     *
150     * @param obj  the object.
151     *
152     * @return {@code true} or {@code false}.
153     */
154    @Override
155    public boolean equals(Object obj) {
156        if (obj == this) {
157            return true;
158        }
159        if (!(obj instanceof TickUnit)) {
160            return false;
161        }
162        TickUnit that = (TickUnit) obj;
163        if (this.size != that.size) {
164            return false;
165        }
166        if (this.minorTickCount != that.minorTickCount) {
167            return false;
168        }
169        return true;
170    }
171
172    /**
173     * Returns a hash code for this instance.
174     *
175     * @return A hash code.
176     */
177    @Override
178    public int hashCode() {
179        long temp = this.size != +0.0d ? Double.doubleToLongBits(this.size)
180                : 0L;
181        return (int) (temp ^ (temp >>> 32));
182    }
183
184}