001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * -------------
028 * TickUnit.java
029 * -------------
030 * (C) Copyright 2001-2007, 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
047 package org.jfree.chart.axis;
048
049 import 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 */
063 public 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 public int compareTo(Object object) {
143
144 if (object instanceof TickUnit) {
145 TickUnit other = (TickUnit) object;
146 if (this.size > other.getSize()) {
147 return 1;
148 }
149 else if (this.size < other.getSize()) {
150 return -1;
151 }
152 else {
153 return 0;
154 }
155 }
156 else {
157 return -1;
158 }
159
160 }
161
162 /**
163 * Tests this unit for equality with another object.
164 *
165 * @param obj the object.
166 *
167 * @return <code>true</code> or <code>false</code>.
168 */
169 public boolean equals(Object obj) {
170 if (obj == this) {
171 return true;
172 }
173 if (!(obj instanceof TickUnit)) {
174 return false;
175 }
176 TickUnit that = (TickUnit) obj;
177 if (this.size != that.size) {
178 return false;
179 }
180 if (this.minorTickCount != that.minorTickCount) {
181 return false;
182 }
183 return true;
184 }
185
186 /**
187 * Returns a hash code for this instance.
188 *
189 * @return A hash code.
190 */
191 public int hashCode() {
192 long temp = this.size != +0.0d ? Double.doubleToLongBits(this.size)
193 : 0L;
194 return (int) (temp ^ (temp >>> 32));
195 }
196
197 }