KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > util > Format


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.util;
20
21 import java.util.Vector JavaDoc;
22
23 /**
24  * A helper class for formatting numbers according to
25  * a <tt>printf</tt>-like format string. Each instance of
26  * this class is initialized by a format string for a
27  * single number.
28  *
29  * @author Franz-Josef Elmer
30  */

31 public class Format implements TicLabelFormat {
32   /**
33    * Creates a new instance based of specified key-value pair of the
34    * specified configuration parameters.
35    * @param config Config parameters.
36    * @param key The key of the key-value pair in <tt>config</tt> containing
37    * the format string.
38    * @return <tt>null</tt> if undefined key-value pair or format string
39    * is an empty string.
40    * @throws FactoryException if the format string is invalid.
41    */

42   public static Format create(ConfigParameters config, String JavaDoc key) {
43     Format result = null;
44     String JavaDoc format = config.get(key, null);
45     if (format != null && format.length() > 0) {
46       try {
47         result = new Format(format);
48       } catch (Exception JavaDoc e) {
49         throw new FactoryException(config, key, e);
50       }
51     }
52     return result;
53   }
54
55   private final FormatElement[] _formatElements;
56   private final Vector JavaDoc _staticParts;
57
58   /**
59    * Creates an instance for the specified format string.
60    * The format string is an alternation of some static texts and
61    * format elements.
62    * A format element has to start with '%' and it must end with
63    * one of the following format descriptors:
64    * <table border=0 cellpadding=5>
65    * <tr><td><tt>d</tt></td>
66    * <td>decimal integer</td></tr>
67    * <tr><td><tt>o</tt></td>
68    * <td>octal integer</td></tr>
69    * <tr><td><tt>x</tt></td>
70    * <td>hex integer</td></tr>
71    * <tr><td><tt>f</tt></td>
72    * <td>floating point number with a fixed decimal point</td></tr>
73    * <tr><td><tt>e,&nbsp;E</tt></td>
74    * <td>floating point number in logarithmic format</td></tr>
75    * <tr><td><tt>g,&nbsp;G</tt></td>
76    * <td>floating point number rendered either in fixed-decimal
77    * format of logarithmic format depending on the size of
78    * the mantissa.</td></tr>
79    * </table>
80    * The characters between '%' and the decriptor are optional.
81    * They can be grouped into
82    * <ul><li>modifier<br>
83    * it is
84    * <ul><li>'-' if the formated result should be flushed left
85    * <li>'+' if the sign should be always appear
86    * <li>'0' if the leading space should be filled with zeros
87    * </ul>
88    * <li>width<br>
89    * a decimal number given the minimum number of characters
90    * of the result
91    * <li>precision
92    * </ul>
93    * A plain '%' is coded as '%%'.
94    * @param formatString The format string.
95    * @exception IllegalArgumentException if invalid format string.
96    */

97   public Format(String JavaDoc formatString) {
98     _staticParts = new Vector JavaDoc();
99     Vector JavaDoc formatElements = new Vector JavaDoc();
100     StringBuffer JavaDoc part = new StringBuffer JavaDoc();
101     boolean insideFormatElement = false;
102     boolean atPercentSymbol = false;
103     for (int i = 0, n = formatString.length(); i < n; i++) {
104       char c = formatString.charAt(i);
105       if (insideFormatElement) {
106         part.append(c);
107         if (FormatElement.DESCRIPTORS.indexOf(c) >= 0) {
108           formatElements.addElement(new String JavaDoc(part));
109           part.setLength(0);
110           insideFormatElement = false;
111         }
112       } else if (atPercentSymbol) {
113         atPercentSymbol = false;
114         if (c != '%') {
115           _staticParts.addElement(new String JavaDoc(part));
116           part.setLength(0);
117           insideFormatElement = true;
118         }
119         part.append(c);
120         if (FormatElement.DESCRIPTORS.indexOf(c) >= 0) {
121           formatElements.addElement(new String JavaDoc(part));
122           part.setLength(0);
123           insideFormatElement = false;
124         }
125       } else {
126         if (c == '%') {
127           atPercentSymbol = true;
128         } else {
129           part.append(c);
130         }
131       }
132     }
133     if (insideFormatElement) {
134       formatElements.addElement(new String JavaDoc(part));
135     } else {
136       _staticParts.addElement(new String JavaDoc(part));
137     }
138     
139     _formatElements = new FormatElement[formatElements.size()];
140     for (int i = 0; i < _formatElements.length; i++) {
141       _formatElements[i]
142           = new FormatElement((String JavaDoc) formatElements.elementAt(i));
143     }
144   }
145
146   /**
147    * Format a number.
148    * If there are no format elements the numbers will be ignored.
149    * If there are more than one format elements the
150    * additional format elements will be ignored and only the static parts
151    * are taken.
152    * @param number Number to be formated.
153    * @return Formated number.
154    */

155   public String JavaDoc form(long number) {
156     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
157     result.append(_staticParts.elementAt(0));
158     if (_formatElements.length > 0) {
159       _formatElements[0].form(result, number);
160     }
161     return appendRest(result);
162   }
163   
164   /**
165    * Format a number.
166    * If there are no format elements the numbers will be ignored.
167    * If there are more than one format elements the
168    * additional format elements will be ignored and only the static parts
169    * are taken.
170    * @param number Number to be formated.
171    * @return Formated number.
172    */

173   public String JavaDoc form(double number) {
174     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
175     result.append(_staticParts.elementAt(0));
176     if (_formatElements.length > 0) {
177       _formatElements[0].form(result, number);
178     }
179     return appendRest(result);
180   }
181   
182   private String JavaDoc appendRest(StringBuffer JavaDoc buffer) {
183     for (int i = 1, n = _staticParts.size(); i < n; i++) {
184       buffer.append(_staticParts.elementAt(i));
185     }
186     return new String JavaDoc(buffer);
187   }
188   
189   /**
190    * Format an array of double numbers.
191    * If there are less format elements than numbers the additional numbers
192    * will be ignored. If there are less numbers than format elements the
193    * additional format elements will be ignored and only the static parts
194    * are taken.
195    * @param numbers Numbers to be formated.
196    * @return Formated numbers.
197    */

198   public String JavaDoc form(double[] numbers) {
199     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
200     for (int i = 0, n = _staticParts.size(); i < n; i++) {
201       result.append(_staticParts.elementAt(i));
202       if (i < _formatElements.length && i < numbers.length) {
203         _formatElements[i].form(result, numbers[i]);
204       }
205     }
206     return new String JavaDoc(result);
207   }
208 }
209
Popular Tags