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