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