001/* ======================================================
002 * JFreeChart : a chart library for the Java(tm) platform
003 * ======================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://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 * HMSNumberFormat.java
029 * --------------------
030 * (C) Copyright 2013-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.util;
038
039import java.text.DecimalFormat;
040import java.text.FieldPosition;
041import java.text.NumberFormat;
042import java.text.ParsePosition;
043
044/**
045 * A custom number formatter that formats numbers (in seconds) as HH:MM:SS.
046 * Created in response to:
047 * <p>
048 * http://stackoverflow.com/questions/19028908/jfreechart-need-to-customize-y-axis-just-for-printing
049 */
050public class HMSNumberFormat extends NumberFormat {
051
052    /** Number formatter. */
053    private NumberFormat formatter = new DecimalFormat("00");
054    
055    /**
056     * Creates a new instance.
057     */
058    public HMSNumberFormat() {
059        // nothing to do
060    }
061
062    /**
063     * Formats the specified number as a string of the form HH:MM:SS.  The 
064     * decimal fraction is ignored.
065     *
066     * @param number  the number to format.
067     * @param toAppendTo  the buffer to append to (ignored here).
068     * @param pos  the field position (ignored here).
069     *
070     * @return The string buffer.
071     */
072    @Override
073    public StringBuffer format(double number, StringBuffer toAppendTo,
074            FieldPosition pos) {
075        return format((long) number, toAppendTo, pos);
076    }
077
078    /**
079     * Formats the specified number as a string of the form HH:MM:SS.
080     *
081     * @param number  the number to format.
082     * @param toAppendTo  the buffer to append to (ignored here).
083     * @param pos  the field position (ignored here).
084     *
085     * @return The string buffer.
086     */
087    @Override
088    public StringBuffer format(long number, StringBuffer toAppendTo,
089            FieldPosition pos) {
090        StringBuffer sb = new StringBuffer();
091        long hours = number / 3600;
092        sb.append(this.formatter.format(hours)).append(":");
093        long remaining = number - (hours * 3600);
094        long minutes = remaining / 60;
095        sb.append(this.formatter.format(minutes)).append(":");
096        long seconds = remaining - (minutes * 60);
097        sb.append(this.formatter.format(seconds));
098        return sb;
099    }
100
101    /**
102     * Parsing is not implemented, so this method always returns
103     * {@code null}.
104     *
105     * @param source  ignored.
106     * @param parsePosition  ignored.
107     *
108     * @return Always {@code null}.
109     */
110    @Override
111    public Number parse (String source, ParsePosition parsePosition) {
112        return null; // don't bother with parsing
113    }
114
115}