Source for org.jfree.chart.axis.NumberTickUnit

   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:  * NumberTickUnit.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 : Updated for changed to TickUnit class (DG);
  39:  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40:  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
  41:  * 09-Jan-2002 : Added a new constructor (DG);
  42:  * 26-Mar-2003 : Implemented Serializable (DG);
  43:  * 05-Jul-2005 : Added equals() implementation (DG);
  44:  * 05-Sep-2005 : Implemented hashCode(), thanks to Thomas Morgner (DG);
  45:  * 02-Aug-2007 : Added new constructor with minorTickCount (DG);
  46:  *
  47:  */
  48: 
  49: package org.jfree.chart.axis;
  50: 
  51: import java.io.Serializable;
  52: import java.text.NumberFormat;
  53: 
  54: /**
  55:  * A numerical tick unit.
  56:  */
  57: public class NumberTickUnit extends TickUnit implements Serializable {
  58: 
  59:     /** For serialization. */
  60:     private static final long serialVersionUID = 3849459506627654442L;
  61:     
  62:     /** A formatter for the tick unit. */
  63:     private NumberFormat formatter;
  64: 
  65:     /**
  66:      * Creates a new number tick unit.
  67:      *
  68:      * @param size  the size of the tick unit.
  69:      */
  70:     public NumberTickUnit(double size) {
  71:         this(size, NumberFormat.getNumberInstance());
  72:     }
  73: 
  74:     /**
  75:      * Creates a new number tick unit.
  76:      *
  77:      * @param size  the size of the tick unit.
  78:      * @param formatter  a number formatter for the tick unit (<code>null</code>
  79:      *                   not permitted).
  80:      */
  81:     public NumberTickUnit(double size, NumberFormat formatter) {
  82:         super(size);
  83:         if (formatter == null) {
  84:             throw new IllegalArgumentException("Null 'formatter' argument.");
  85:         }
  86:         this.formatter = formatter;
  87:     }
  88: 
  89:     /**
  90:      * Creates a new number tick unit.
  91:      *
  92:      * @param size  the size of the tick unit.
  93:      * @param formatter  a number formatter for the tick unit (<code>null</code>
  94:      *                   not permitted).
  95:      * @param minorTickCount  the number of minor ticks.
  96:      *
  97:      * @since 1.0.7
  98:      */
  99:     public NumberTickUnit(double size, NumberFormat formatter,
 100:             int minorTickCount) {
 101:         super(size, minorTickCount);
 102:         if (formatter == null) {
 103:             throw new IllegalArgumentException("Null 'formatter' argument.");
 104:         }
 105:         this.formatter = formatter;
 106:     }
 107: 
 108:     /**
 109:      * Converts a value to a string.
 110:      *
 111:      * @param value  the value.
 112:      *
 113:      * @return The formatted string.
 114:      */
 115:     public String valueToString(double value) {
 116:         return this.formatter.format(value);
 117:     }
 118:     
 119:     /**
 120:      * Tests this formatter for equality with an arbitrary object.
 121:      * 
 122:      * @param obj  the object (<code>null</code> permitted).
 123:      * 
 124:      * @return A boolean.
 125:      */
 126:     public boolean equals(Object obj) {
 127:         if (obj == this) {
 128:             return true;
 129:         }
 130:         if (!(obj instanceof NumberTickUnit)) {
 131:             return false;
 132:         }
 133:         if (!super.equals(obj)) {
 134:             return false;
 135:         }
 136:         NumberTickUnit that = (NumberTickUnit) obj;
 137:         if (!this.formatter.equals(that.formatter)) {
 138:             return false;
 139:         }
 140:         return true;
 141:     }
 142:     
 143:     /**
 144:      * Returns a string representing this unit.
 145:      * 
 146:      * @return A string.
 147:      */
 148:     public String toString() {
 149:         return "[size=" + this.valueToString(this.getSize()) + "]";
 150:     }
 151: 
 152:     /**
 153:      * Returns a hash code for this instance.
 154:      * 
 155:      * @return A hash code.
 156:      */
 157:     public int hashCode() {
 158:         int result = super.hashCode();
 159:         result = 29 * result + (this.formatter != null 
 160:                 ? this.formatter.hashCode() : 0);
 161:         return result;
 162:     }
 163: 
 164: }