    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * NumberTickUnit.java
    029     * -------------------
    030     * (C) Copyright 2001-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: NumberTickUnit.java,v 1.3.2.3 2005/10/25 20:37:34 mungady Exp $
    036     *
    037     * Changes (from 19-Dec-2001)
    038     * --------------------------
    039     * 19-Dec-2001 : Added standard header (DG);
    040     * 01-May-2002 : Updated for changed to TickUnit class (DG);
    041     * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    042     * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
    043     * 09-Jan-2002 : Added a new constructor (DG);
    044     * 26-Mar-2003 : Implemented Serializable (DG);
    045     * 05-Jul-2005 : Added equals() implementation (DG);
    046     * 05-Sep-2005 : Implemented hashCode(), thanks to Thomas Morgner (DG);
    047     *
    048     */
    049
    050    package org.jfree.chart.axis;
    051
    052    import java.io.Serializable;
    053    import java.text.NumberFormat;
    054
    055    /**
    056     * A numerical tick unit.
    057     */
    058    public class NumberTickUnit extends TickUnit implements Serializable {
    059
    060        /** For serialization. */
    061        private static final long serialVersionUID = 3849459506627654442L;
    062
    063        /** A formatter for the tick unit. */
    064        private NumberFormat formatter;
    065
    066        /**
    067         * Creates a new number tick unit.
    068         *
    069         * @param size  the size of the tick unit.
    070         */
    071        public NumberTickUnit(double size) {
    072            this(size, NumberFormat.getNumberInstance());
    073        }
    074
    075        /**
    076         * Creates a new number tick unit.
    077         *
    078         * @param size  the size of the tick unit.
    079         * @param formatter  a number formatter for the tick unit (<code>null</code>
    080         *                   not permitted).
    081         */
    082        public NumberTickUnit(double size, NumberFormat formatter) {
    083            super(size);
    084            if (formatter == null) {
    085                throw new IllegalArgumentException("Null 'formatter' argument.");
    086            }
    087            this.formatter = formatter;
    088        }
    089
    090        /**
    091         * Converts a value to a string.
    092         *
    093         * @param value  the value.
    094         *
    095         * @return The formatted string.
    096         */
    097        public String valueToString(double value) {
    098            return this.formatter.format(value);
    099        }
    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    }