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