001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 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 041 package org.jfree.chart.util; 042 043 import java.text.FieldPosition; 044 import java.text.NumberFormat; 045 import 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 */ 053 public 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 public StringBuffer format(double number, StringBuffer toAppendTo, 116 FieldPosition pos) { 117 return format((long) number, toAppendTo, pos); 118 } 119 120 /** 121 * Formats the specified number as a hexadecimal string. The decimal 122 * fraction is ignored. 123 * 124 * @param number the number to format. 125 * @param toAppendTo the buffer to append to (ignored here). 126 * @param pos the field position (ignored here). 127 * 128 * @return The string buffer. 129 */ 130 public StringBuffer format(long number, StringBuffer toAppendTo, 131 FieldPosition pos) { 132 String l_hex = Long.toHexString(number).toUpperCase(); 133 134 int l_pad = this.m_numDigits - l_hex.length(); 135 l_pad = (0 < l_pad) ? l_pad : 0; 136 137 StringBuffer l_extended = new StringBuffer("0x"); 138 for (int i = 0; i < l_pad; i++) { 139 l_extended.append(0); 140 } 141 l_extended.append(l_hex); 142 143 return l_extended; 144 } 145 146 /** 147 * Parsing is not implemented, so this method always returns 148 * <code>null</code>. 149 * 150 * @param source ignored. 151 * @param parsePosition ignored. 152 * 153 * @return Always <code>null</code>. 154 */ 155 public Number parse (String source, ParsePosition parsePosition) { 156 return null; // don't bother with parsing 157 } 158 159 }