    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     * NumberTickUnit.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 : Updated for changed to TickUnit class (DG);
    039     * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    040     * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
    041     * 09-Jan-2002 : Added a new constructor (DG);
    042     * 26-Mar-2003 : Implemented Serializable (DG);
    043     * 05-Jul-2005 : Added equals() implementation (DG);
    044     * 05-Sep-2005 : Implemented hashCode(), thanks to Thomas Morgner (DG);
    045     * 02-Aug-2007 : Added new constructor with minorTickCount (DG);
    046     *
    047     */
    048
    049    package org.jfree.chart.axis;
    050
    051    import java.io.Serializable;
    052    import java.text.NumberFormat;
    053
    054    /**
    055     * A numerical tick unit.
    056     */
    057    public class NumberTickUnit extends TickUnit implements Serializable {
    058
    059        /** For serialization. */
    060        private static final long serialVersionUID = 3849459506627654442L;
    061
    062        /** A formatter for the tick unit. */
    063        private NumberFormat formatter;
    064
    065        /**
    066         * Creates a new number tick unit.
    067         *
    068         * @param size  the size of the tick unit.
    069         */
    070        public NumberTickUnit(double size) {
    071            this(size, NumberFormat.getNumberInstance());
    072        }
    073
    074        /**
    075         * Creates a new number tick unit.
    076         *
    077         * @param size  the size of the tick unit.
    078         * @param formatter  a number formatter for the tick unit (<code>null</code>
    079         *                   not permitted).
    080         */
    081        public NumberTickUnit(double size, NumberFormat formatter) {
    082            super(size);
    083            if (formatter == null) {
    084                throw new IllegalArgumentException("Null 'formatter' argument.");
    085            }
    086            this.formatter = formatter;
    087        }
    088
    089        /**
    090         * Creates a new number tick unit.
    091         *
    092         * @param size  the size of the tick unit.
    093         * @param formatter  a number formatter for the tick unit (<code>null</code>
    094         *                   not permitted).
    095         * @param minorTickCount  the number of minor ticks.
    096         *
    097         * @since 1.0.7
    098         */
    099        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    }