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