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 * HexNumberFormat.java
029 * --------------------
030 * (C) Copyright 2007, 2008, by Richard West and Contributors.
031 *
032 * Original Author:  Richard West, Advanced Micro Devices, Inc.;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes:
036 * --------
037 * 14-Jun-2007 : Version 1 (RW);
038 *
039 */
040
041package org.jfree.chart.util;
042
043import java.text.FieldPosition;
044import java.text.NumberFormat;
045import java.text.ParsePosition;
046
047/**
048 * A custom number formatter that formats numbers as hexadecimal strings.
049 * There are some limitations, so be careful using this class.
050 *
051 * @since 1.0.6
052 */
053public class HexNumberFormat extends NumberFormat {
054
055    /** Number of hexadecimal digits for a byte. */
056    public static final int BYTE = 2;
057
058    /** Number of hexadecimal digits for a word. */
059    public static final int WORD = 4;
060
061    /** Number of hexadecimal digits for a double word. */
062    public static final int DWORD = 8;
063
064    /** Number of hexadecimal digits for a quad word. */
065    public static final int QWORD = 16;
066
067    /** The number of digits (shorter strings will be left padded). */
068    private int m_numDigits = DWORD;
069
070    /**
071     * Creates a new instance with 8 digits.
072     */
073    public HexNumberFormat() {
074        this(DWORD);
075    }
076
077    /**
078     * Creates a new instance with the specified number of digits.
079
080     * @param digits  the digits.
081     */
082    public HexNumberFormat(int digits) {
083        super();
084        this.m_numDigits = digits;
085    }
086
087    /**
088     * Returns the number of digits.
089     *
090     * @return The number of digits.
091     */
092    public final int getNumberOfDigits() {
093        return this.m_numDigits;
094    }
095
096    /**
097     * Sets the number of digits.
098     *
099     * @param digits  the number of digits.
100     */
101    public void setNumberOfDigits(int digits) {
102        this.m_numDigits = digits;
103    }
104
105    /**
106     * Formats the specified number as a hexadecimal string.  The decimal
107     * fraction is ignored.
108     *
109     * @param number  the number to format.
110     * @param toAppendTo  the buffer to append to (ignored here).
111     * @param pos  the field position (ignored here).
112     *
113     * @return The string buffer.
114     */
115    @Override
116    public StringBuffer format(double number, StringBuffer toAppendTo,
117            FieldPosition pos) {
118        return format((long) number, toAppendTo, pos);
119    }
120
121    /**
122     * Formats the specified number as a hexadecimal string.  The decimal
123     * fraction is ignored.
124     *
125     * @param number  the number to format.
126     * @param toAppendTo  the buffer to append to (ignored here).
127     * @param pos  the field position (ignored here).
128     *
129     * @return The string buffer.
130     */
131    @Override
132    public StringBuffer format(long number, StringBuffer toAppendTo,
133            FieldPosition pos) {
134        String l_hex = Long.toHexString(number).toUpperCase();
135
136        int l_pad = this.m_numDigits - l_hex.length();
137        l_pad = (0 < l_pad) ? l_pad : 0;
138
139        StringBuffer l_extended = new StringBuffer("0x");
140        for (int i = 0; i < l_pad; i++) {
141            l_extended.append(0);
142        }
143        l_extended.append(l_hex);
144
145        return l_extended;
146    }
147
148    /**
149     * Parsing is not implemented, so this method always returns
150     * <code>null</code>.
151     *
152     * @param source  ignored.
153     * @param parsePosition  ignored.
154     *
155     * @return Always <code>null</code>.
156     */
157    @Override
158    public Number parse (String source, ParsePosition parsePosition) {
159        return null; // don't bother with parsing
160    }
161
162}