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