2 * Cay S. Horstmann & Gary Cornell, Core Java
\r
3 * Published By Sun Microsystems Press/Prentice-Hall
\r
4 * Copyright (C) 1997 Sun Microsystems Inc.
\r
5 * All Rights Reserved.
\r
7 * Permission to use, copy, modify, and distribute this
\r
8 * software and its documentation for NON-COMMERCIAL purposes
\r
9 * and without fee is hereby granted provided that this
\r
10 * copyright notice appears in all copies.
\r
12 * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR
\r
13 * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
\r
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
\r
15 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
\r
16 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
\r
17 * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
\r
18 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
\r
19 * THIS SOFTWARE OR ITS DERIVATIVES.
\r
23 * A class for formatting numbers that follows printf conventions.
\r
24 * Also implements C-like atoi and atof functions
\r
25 * @version 1.03 25 Oct 1997
\r
26 * @author Cay Horstmann
\r
28 package jalview.util;
\r
36 * @version $Revision$
\r
41 private int precision;
\r
43 private String post;
\r
44 private boolean leading_zeroes;
\r
45 private boolean show_plus;
\r
46 private boolean alternate;
\r
47 private boolean show_space;
\r
48 private boolean left_align;
\r
49 private char fmt; // one of cdeEfgGiosxXos
\r
52 * Creates a new Format object.
\r
54 * @param s DOCUMENT ME!
\r
56 public Format(String s)
\r
62 leading_zeroes = false;
\r
69 int length = s.length();
\r
70 int parse_state = 0;
\r
72 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
\r
73 // 4 = format, 5 = end
\r
76 while (parse_state == 0)
\r
82 else if (s.charAt(i) == '%')
\r
84 if (i < (length - 1))
\r
86 if (s.charAt(i + 1) == '%')
\r
98 throw new java.lang.IllegalArgumentException();
\r
103 pre = pre + s.charAt(i);
\r
109 while (parse_state == 1)
\r
115 else if (s.charAt(i) == ' ')
\r
119 else if (s.charAt(i) == '-')
\r
123 else if (s.charAt(i) == '+')
\r
127 else if (s.charAt(i) == '0')
\r
129 leading_zeroes = true;
\r
131 else if (s.charAt(i) == '#')
\r
144 while (parse_state == 2)
\r
150 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
\r
152 width = ((width * 10) + s.charAt(i)) - '0';
\r
155 else if (s.charAt(i) == '.')
\r
167 while (parse_state == 3)
\r
173 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
\r
175 precision = ((precision * 10) + s.charAt(i)) - '0';
\r
184 if (parse_state == 4)
\r
200 post = s.substring(i, length);
\r
205 * Formats the number following printf conventions.
\r
206 * Main limitation: Can only handle one format parameter at a time
\r
207 * Use multiple Format objects to format more than one number
\r
208 * @param s the format string following printf conventions
\r
209 * The string has a prefix, a format code and a suffix. The prefix and suffix
\r
210 * become part of the formatted output. The format code directs the
\r
211 * formatting of the (single) parameter to be formatted. The code has the
\r
212 * following structure
\r
214 * <li> a % (required)
\r
215 * <li> a modifier (optional)
\r
217 * <dt> + <dd> forces display of + for positive numbers
\r
218 * <dt> 0 <dd> show leading zeroes
\r
219 * <dt> - <dd> align left in the field
\r
220 * <dt> space <dd> prepend a space in front of positive numbers
\r
221 * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format.
\r
223 * <li> an integer denoting field width (optional)
\r
224 * <li> a period followed by an integer denoting precision (optional)
\r
225 * <li> a format descriptor (required)
\r
227 * <dt>f <dd> floating point number in fixed format
\r
228 * <dt>e, E <dd> floating point number in exponential notation (scientific format). The E format results in an uppercase E for the exponent (1.14130E+003), the e format in a lowercase e.
\r
229 * <dt>g, G <dd> floating point number in general format (fixed format for small numbers, exponential format for large numbers). Trailing zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format in a lowercase e.
\r
230 * <dt>d, i <dd> integer in decimal
\r
231 * <dt>x <dd> integer in hexadecimal
\r
232 * <dt>o <dd> integer in octal
\r
233 * <dt>s <dd> string
\r
234 * <dt>c <dd> character
\r
237 * @exception IllegalArgumentException if bad format
\r
240 public static String getHexString(java.awt.Color color)
\r
245 r = Integer.toHexString(color.getRed());
\r
247 if (r.length() < 2)
\r
252 g = Integer.toHexString(color.getGreen());
\r
254 if (g.length() < 2)
\r
259 b = Integer.toHexString(color.getBlue());
\r
261 if (b.length() < 2)
\r
270 * prints a formatted number following printf conventions
\r
271 * @param s a PrintStream
\r
272 * @param fmt the format string
\r
273 * @param x the double to print
\r
275 public static void print(java.io.PrintStream s, String fmt, double x)
\r
277 s.print(new Format(fmt).form(x));
\r
281 * prints a formatted number following printf conventions
\r
282 * @param s a PrintStream
\r
283 * @param fmt the format string
\r
284 * @param x the long to print
\r
286 public static void print(java.io.PrintStream s, String fmt, long x)
\r
288 s.print(new Format(fmt).form(x));
\r
292 * prints a formatted number following printf conventions
\r
293 * @param s a PrintStream
\r
294 * @param fmt the format string
\r
295 * @param x the character to
\r
297 public static void print(java.io.PrintStream s, String fmt, char x)
\r
299 s.print(new Format(fmt).form(x));
\r
303 * prints a formatted number following printf conventions
\r
304 * @param s a PrintStream, fmt the format string
\r
305 * @param x a string that represents the digits to print
\r
307 public static void print(java.io.PrintStream s, String fmt, String x)
\r
309 s.print(new Format(fmt).form(x));
\r
313 * Converts a string of digits (decimal, octal or hex) to an integer
\r
314 * @param s a string
\r
315 * @return the numeric value of the prefix of s representing a base 10 integer
\r
317 public static int atoi(String s)
\r
319 return (int) atol(s);
\r
323 * Converts a string of digits (decimal, octal or hex) to a long integer
\r
324 * @param s a string
\r
325 * @return the numeric value of the prefix of s representing a base 10 integer
\r
327 public static long atol(String s)
\r
331 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
\r
334 if ((i < s.length()) && (s.charAt(i) == '0'))
\r
336 if (((i + 1) < s.length()) &&
\r
337 ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
\r
339 return parseLong(s.substring(i + 2), 16);
\r
343 return parseLong(s, 8);
\r
348 return parseLong(s, 10);
\r
355 * @param s DOCUMENT ME!
\r
356 * @param base DOCUMENT ME!
\r
358 * @return DOCUMENT ME!
\r
360 private static long parseLong(String s, int base)
\r
366 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
\r
369 if ((i < s.length()) && (s.charAt(i) == '-'))
\r
374 else if ((i < s.length()) && (s.charAt(i) == '+'))
\r
379 while (i < s.length())
\r
381 char ch = s.charAt(i);
\r
383 if (('0' <= ch) && (ch < ('0' + base)))
\r
385 r = ((r * base) + ch) - '0';
\r
387 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
\r
389 r = ((r * base) + ch) - 'A' + 10;
\r
391 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
\r
393 r = ((r * base) + ch) - 'a' + 10;
\r
407 * Converts a string of digits to an double
\r
408 * @param s a string
\r
410 public static double atof(String s)
\r
414 double r = 0; // integer part
\r
415 //double f = 0; // fractional part
\r
416 double p = 1; // exponent of fractional part
\r
417 int state = 0; // 0 = int part, 1 = frac part
\r
419 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
\r
422 if ((i < s.length()) && (s.charAt(i) == '-'))
\r
427 else if ((i < s.length()) && (s.charAt(i) == '+'))
\r
432 while (i < s.length())
\r
434 char ch = s.charAt(i);
\r
436 if (('0' <= ch) && (ch <= '9'))
\r
440 r = ((r * 10) + ch) - '0';
\r
442 else if (state == 1)
\r
445 r = r + (p * (ch - '0'));
\r
448 else if (ch == '.')
\r
459 else if ((ch == 'e') || (ch == 'E'))
\r
461 long e = (int) parseLong(s.substring(i + 1), 10);
\r
463 return sign * r * Math.pow(10, e);
\r
477 * Formats a double into a string (like sprintf in C)
\r
478 * @param x the number to format
\r
479 * @return the formatted string
\r
480 * @exception IllegalArgumentException if bad argument
\r
482 public String form(double x)
\r
501 r = fixed_format(x);
\r
503 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
\r
509 throw new java.lang.IllegalArgumentException();
\r
512 return pad(sign(s, r));
\r
516 * Formats a long integer into a string (like sprintf in C)
\r
517 * @param x the number to format
\r
518 * @return the formatted string
\r
520 public String form(long x)
\r
525 if ((fmt == 'd') || (fmt == 'i'))
\r
529 r = ("" + x).substring(1);
\r
538 else if (fmt == 'o')
\r
540 r = convert(x, 3, 7, "01234567");
\r
542 else if (fmt == 'x')
\r
544 r = convert(x, 4, 15, "0123456789abcdef");
\r
546 else if (fmt == 'X')
\r
548 r = convert(x, 4, 15, "0123456789ABCDEF");
\r
552 throw new java.lang.IllegalArgumentException();
\r
555 return pad(sign(s, r));
\r
559 * Formats a character into a string (like sprintf in C)
\r
560 * @param x the value to format
\r
561 * @return the formatted string
\r
563 public String form(char c)
\r
567 throw new java.lang.IllegalArgumentException();
\r
576 * Formats a string into a larger string (like sprintf in C)
\r
577 * @param x the value to format
\r
578 * @return the formatted string
\r
580 public String form(String s)
\r
584 throw new java.lang.IllegalArgumentException();
\r
587 if (precision >= 0)
\r
589 s = s.substring(0, precision);
\r
598 * @param c DOCUMENT ME!
\r
599 * @param n DOCUMENT ME!
\r
601 * @return DOCUMENT ME!
\r
603 private static String repeat(char c, int n)
\r
610 StringBuffer s = new StringBuffer(n);
\r
612 for (int i = 0; i < n; i++)
\r
615 return s.toString();
\r
621 * @param x DOCUMENT ME!
\r
622 * @param n DOCUMENT ME!
\r
623 * @param m DOCUMENT ME!
\r
624 * @param d DOCUMENT ME!
\r
626 * @return DOCUMENT ME!
\r
628 private static String convert(long x, int n, int m, String d)
\r
639 r = d.charAt((int) (x & m)) + r;
\r
649 * @param r DOCUMENT ME!
\r
651 * @return DOCUMENT ME!
\r
653 private String pad(String r)
\r
655 String p = repeat(' ', width - r.length());
\r
659 return pre + r + p + post;
\r
663 return pre + p + r + post;
\r
670 * @param s DOCUMENT ME!
\r
671 * @param r DOCUMENT ME!
\r
673 * @return DOCUMENT ME!
\r
675 private String sign(int s, String r)
\r
689 else if (show_space)
\r
696 if ((fmt == 'o') && alternate && (r.length() > 0) &&
\r
697 (r.charAt(0) != '0'))
\r
701 else if ((fmt == 'x') && alternate)
\r
705 else if ((fmt == 'X') && alternate)
\r
713 if (leading_zeroes)
\r
717 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') ||
\r
718 (fmt == 'o')) && (precision > 0))
\r
723 return p + repeat('0', w - p.length() - r.length()) + r;
\r
729 * @param d DOCUMENT ME!
\r
731 * @return DOCUMENT ME!
\r
733 private String fixed_format(double d)
\r
735 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
\r
737 // remove trailing zeroes and decimal point
\r
738 if (d > 0x7FFFFFFFFFFFFFFFL)
\r
740 return exp_format(d);
\r
743 if (precision == 0)
\r
745 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
\r
748 long whole = (long) d;
\r
749 double fr = d - whole; // fractional part
\r
751 if ((fr >= 1) || (fr < 0))
\r
753 return exp_format(d);
\r
757 String leading_zeroes = "";
\r
759 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL);
\r
763 leading_zeroes = leading_zeroes + "0";
\r
766 long l = (long) ((factor * fr) + 0.5);
\r
775 String z = leading_zeroes + l;
\r
776 z = "." + z.substring(z.length() - precision, z.length());
\r
778 if (removeTrailing)
\r
780 int t = z.length() - 1;
\r
782 while ((t >= 0) && (z.charAt(t) == '0'))
\r
785 if ((t >= 0) && (z.charAt(t) == '.'))
\r
790 z = z.substring(0, t + 1);
\r
799 * @param d DOCUMENT ME!
\r
801 * @return DOCUMENT ME!
\r
803 private String exp_format(double d)
\r
827 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
\r
829 return fixed_format(d);
\r
833 f = f + fixed_format(d);
\r
835 if ((fmt == 'e') || (fmt == 'g'))
\r
857 return f + p.substring(p.length() - 3, p.length());
\r