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-2005, 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-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: NumberTickUnit.java,v 1.3.2.3 2005/10/25 20:37:34 mungady Exp $
  36:  *
  37:  * Changes (from 19-Dec-2001)
  38:  * --------------------------
  39:  * 19-Dec-2001 : Added standard header (DG);
  40:  * 01-May-2002 : Updated for changed to TickUnit class (DG);
  41:  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  42:  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
  43:  * 09-Jan-2002 : Added a new constructor (DG);
  44:  * 26-Mar-2003 : Implemented Serializable (DG);
  45:  * 05-Jul-2005 : Added equals() implementation (DG);
  46:  * 05-Sep-2005 : Implemented hashCode(), thanks to Thomas Morgner (DG);
  47:  *
  48:  */
  49: 
  50: package org.jfree.chart.axis;
  51: 
  52: import java.io.Serializable;
  53: import java.text.NumberFormat;
  54: 
  55: /**
  56:  * A numerical tick unit.
  57:  */
  58: public class NumberTickUnit extends TickUnit implements Serializable {
  59: 
  60:     /** For serialization. */
  61:     private static final long serialVersionUID = 3849459506627654442L;
  62:     
  63:     /** A formatter for the tick unit. */
  64:     private NumberFormat formatter;
  65: 
  66:     /**
  67:      * Creates a new number tick unit.
  68:      *
  69:      * @param size  the size of the tick unit.
  70:      */
  71:     public NumberTickUnit(double size) {
  72:         this(size, NumberFormat.getNumberInstance());
  73:     }
  74: 
  75:     /**
  76:      * Creates a new number tick unit.
  77:      *
  78:      * @param size  the size of the tick unit.
  79:      * @param formatter  a number formatter for the tick unit (<code>null</code>
  80:      *                   not permitted).
  81:      */
  82:     public NumberTickUnit(double size, NumberFormat formatter) {
  83:         super(size);
  84:         if (formatter == null) {
  85:             throw new IllegalArgumentException("Null 'formatter' argument.");
  86:         }
  87:         this.formatter = formatter;
  88:     }
  89: 
  90:     /**
  91:      * Converts a value to a string.
  92:      *
  93:      * @param value  the value.
  94:      *
  95:      * @return The formatted string.
  96:      */
  97:     public String valueToString(double value) {
  98:         return this.formatter.format(value);
  99:     }
 100:     
 101:     /**
 102:      * Tests this formatter for equality with an arbitrary object.
 103:      * 
 104:      * @param obj  the object (<code>null</code> permitted).
 105:      * 
 106:      * @return A boolean.
 107:      */
 108:     public boolean equals(Object obj) {
 109:         if (obj == this) {
 110:             return true;
 111:         }
 112:         if (!(obj instanceof NumberTickUnit)) {
 113:             return false;
 114:         }
 115:         if (!super.equals(obj)) {
 116:             return false;
 117:         }
 118:         NumberTickUnit that = (NumberTickUnit) obj;
 119:         if (!this.formatter.equals(that.formatter)) {
 120:             return false;
 121:         }
 122:         return true;
 123:     }
 124: 
 125:     /**
 126:      * Returns a hash code for this instance.
 127:      * 
 128:      * @return A hash code.
 129:      */
 130:     public int hashCode() {
 131:         int result = super.hashCode();
 132:         result = 29 * result + (formatter != null ? formatter.hashCode() : 0);
 133:         return result;
 134:     }
 135: 
 136: }