Source for org.jfree.chart.util.HexNumberFormat

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * --------------------
  28:  * HexNumberFormat.java
  29:  * --------------------
  30:  * (C) Copyright 2007, by Richard West and Contributors.
  31:  *
  32:  * Original Author:  Richard West, Advanced Micro Devices, Inc.;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 14-Jun-2007 : Version 1 (RW);
  38:  *
  39:  */
  40: 
  41: package org.jfree.chart.util;
  42: 
  43: import java.text.FieldPosition;
  44: import java.text.NumberFormat;
  45: import java.text.ParsePosition;
  46: 
  47: /**
  48:  * A custom number formatter that formats numbers as hexadecimal strings.
  49:  * There are some limitations, so be careful using this class.
  50:  * 
  51:  * @since 1.0.6
  52:  */
  53: public class HexNumberFormat extends NumberFormat {
  54: 
  55:     /** Number of hexadecimal digits for a byte. */
  56:     public static final int BYTE = 2;
  57:     
  58:     /** Number of hexadecimal digits for a word. */
  59:     public static final int WORD = 4;
  60: 
  61:     /** Number of hexadecimal digits for a double word. */
  62:     public static final int DWORD = 8;
  63: 
  64:     /** Number of hexadecimal digits for a quad word. */
  65:     public static final int QWORD = 16;
  66: 
  67:     /** The number of digits (shorter strings will be left padded). */
  68:     private int m_numDigits = DWORD;
  69: 
  70:     /**
  71:      * Creates a new instance with 8 digits.
  72:      */
  73:     public HexNumberFormat() {
  74:         this(DWORD);
  75:     }
  76: 
  77:     /**
  78:      * Creates a new instance with the specified number of digits.
  79: 
  80:      * @param digits  the digits.
  81:      */
  82:     public HexNumberFormat(int digits) {
  83:         super();
  84:         this.m_numDigits = digits;
  85:     }
  86: 
  87:     /**
  88:      * Returns the number of digits.
  89:      * 
  90:      * @return The number of digits.
  91:      */
  92:     public final int getNumberOfDigits() {
  93:         return this.m_numDigits;
  94:     }
  95: 
  96:     /**
  97:      * Sets the number of digits.
  98:      * 
  99:      * @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: }