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 * TickUnit.java
029 * -------------
030 * (C) Copyright 2001-present, 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    /** The number of minor ticks. */
062    private int minorTickCount;
063
064    /**
065     * Constructs a new tick unit.
066     *
067     * @param size  the tick unit size.
068     */
069    public TickUnit(double size) {
070        this.size = size;
071    }
072
073    /**
074     * Constructs a new tick unit.
075     *
076     * @param size  the tick unit size.
077     * @param minorTickCount  the minor tick count.
078     */
079    public TickUnit(double size, int minorTickCount) {
080        this.size = size;
081        this.minorTickCount = minorTickCount;
082    }
083
084    /**
085     * Returns the size of the tick unit.
086     *
087     * @return The size of the tick unit.
088     */
089    public double getSize() {
090        return this.size;
091    }
092
093    /**
094     * Returns the minor tick count.
095     *
096     * @return The minor tick count.
097     */
098    public int getMinorTickCount() {
099        return this.minorTickCount;
100    }
101
102    /**
103     * Converts the supplied value to a string.
104     * <P>
105     * Subclasses may implement special formatting by overriding this method.
106     *
107     * @param value  the data value.
108     *
109     * @return Value as string.
110     */
111    public String valueToString(double value) {
112        return String.valueOf(value);
113    }
114
115    /**
116     * Compares this tick unit to an arbitrary object.
117     *
118     * @param object  the object to compare against.
119     *
120     * @return {@code 1} if the size of the other object is less than this,
121     *      {@code 0} if both have the same size and {@code -1} this
122     *      size is less than the others.
123     */
124    @Override
125    public int compareTo(Object object) {
126
127        if (object instanceof TickUnit) {
128            TickUnit other = (TickUnit) object;
129            if (this.size > other.getSize()) {
130                return 1;
131            }
132            else if (this.size < other.getSize()) {
133                return -1;
134            }
135            else {
136                return 0;
137            }
138        }
139        else {
140            return -1;
141        }
142
143    }
144
145    /**
146     * Tests this unit for equality with another object.
147     *
148     * @param obj  the object.
149     *
150     * @return {@code true} or {@code false}.
151     */
152    @Override
153    public boolean equals(Object obj) {
154        if (obj == this) {
155            return true;
156        }
157        if (!(obj instanceof TickUnit)) {
158            return false;
159        }
160        TickUnit that = (TickUnit) obj;
161        if (this.size != that.size) {
162            return false;
163        }
164        if (this.minorTickCount != that.minorTickCount) {
165            return false;
166        }
167        return true;
168    }
169
170    /**
171     * Returns a hash code for this instance.
172     *
173     * @return A hash code.
174     */
175    @Override
176    public int hashCode() {
177        long temp = this.size != +0.0d ? Double.doubleToLongBits(this.size)
178                : 0L;
179        return (int) (temp ^ (temp >>> 32));
180    }
181
182}