001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------
028 * NumberTickUnit.java
029 * -------------------
030 * (C) Copyright 2001-2013, 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 * 02-Jul-2013 : Use ParamChecks (DG);
047 *
048 */
049
050package org.jfree.chart.axis;
051
052import java.io.Serializable;
053import java.text.NumberFormat;
054import org.jfree.chart.util.ParamChecks;
055
056/**
057 * A numerical tick unit.
058 */
059public class NumberTickUnit extends TickUnit implements Serializable {
060
061    /** For serialization. */
062    private static final long serialVersionUID = 3849459506627654442L;
063
064    /** A formatter for the tick unit. */
065    private NumberFormat formatter;
066
067    /**
068     * Creates a new number tick unit.
069     *
070     * @param size  the size of the tick unit.
071     */
072    public NumberTickUnit(double size) {
073        this(size, NumberFormat.getNumberInstance());
074    }
075
076    /**
077     * Creates a new number tick unit.
078     *
079     * @param size  the size of the tick unit.
080     * @param formatter  a number formatter for the tick unit (<code>null</code>
081     *                   not permitted).
082     */
083    public NumberTickUnit(double size, NumberFormat formatter) {
084        super(size);
085        ParamChecks.nullNotPermitted(formatter, "formatter");
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        ParamChecks.nullNotPermitted(formatter, "formatter");
103        this.formatter = formatter;
104    }
105
106    /**
107     * Converts a value to a string.
108     *
109     * @param value  the value.
110     *
111     * @return The formatted string.
112     */
113    @Override
114    public String valueToString(double value) {
115        return this.formatter.format(value);
116    }
117
118    /**
119     * Tests this formatter for equality with an arbitrary object.
120     *
121     * @param obj  the object (<code>null</code> permitted).
122     *
123     * @return A boolean.
124     */
125    @Override
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    @Override
149    public String toString() {
150        return "[size=" + this.valueToString(this.getSize()) + "]";
151    }
152
153    /**
154     * Returns a hash code for this instance.
155     *
156     * @return A hash code.
157     */
158    @Override
159    public int hashCode() {
160        int result = super.hashCode();
161        result = 29 * result + (this.formatter != null
162                ? this.formatter.hashCode() : 0);
163        return result;
164    }
165
166}