Source for org.jfree.chart.axis.TickUnit

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------
  28:  * TickUnit.java
  29:  * -------------
  30:  * (C) Copyright 2001-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 19-Dec-2001 : Added standard header (DG);
  38:  * 01-May-2002 : Changed the unit size from Number to double (DG);
  39:  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  40:  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
  41:  * 26-Mar-2003 : Implemented Serializable (DG);
  42:  * 05-Sep-2005 : Implemented hashCode(), thanks to Thomas Morgner (DG);
  43:  * 02-Aug-2007 : Added minorTickCount attribute (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.chart.axis;
  48: 
  49: import java.io.Serializable;
  50: 
  51: /**
  52:  * Base class representing a tick unit.  This determines the spacing of the
  53:  * tick marks on an axis.
  54:  * <P>
  55:  * This class (and any subclasses) should be immutable, the reason being that
  56:  * ORDERED collections of tick units are maintained and if one instance can be
  57:  * changed, it may destroy the order of the collection that it belongs to.
  58:  * In addition, if the implementations are immutable, they can belong to
  59:  * multiple collections.
  60:  *
  61:  * @see ValueAxis
  62:  */
  63: public abstract class TickUnit implements Comparable, Serializable {
  64: 
  65:     /** For serialization. */
  66:     private static final long serialVersionUID = 510179855057013974L;
  67:     
  68:     /** The size of the tick unit. */
  69:     private double size;
  70: 
  71:     /**
  72:      * The number of minor ticks.
  73:      * 
  74:      * @since 1.0.7
  75:      */
  76:     private int minorTickCount;
  77:     
  78:     /**
  79:      * Constructs a new tick unit.
  80:      *
  81:      * @param size  the tick unit size.
  82:      */
  83:     public TickUnit(double size) {
  84:         this.size = size;
  85:     }
  86:     
  87:     /**
  88:      * Constructs a new tick unit.
  89:      *
  90:      * @param size  the tick unit size.
  91:      * @param minorTickCount  the minor tick count.
  92:      * 
  93:      * @since 1.0.7
  94:      */
  95:     public TickUnit(double size, int minorTickCount) {
  96:         this.size = size;
  97:         this.minorTickCount = minorTickCount;
  98:     }    
  99: 
 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: }