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
34 * @version $Revision$
\r
39 private int precision;
\r
41 private String post;
\r
42 private boolean leading_zeroes;
\r
43 private boolean show_plus;
\r
44 private boolean alternate;
\r
45 private boolean show_space;
\r
46 private boolean left_align;
\r
47 private char fmt; // one of cdeEfgGiosxXos
\r
50 * Creates a new Format object.
\r
52 * @param s DOCUMENT ME!
\r
54 public Format(String s)
\r
60 leading_zeroes = false;
\r
67 int length = s.length();
\r
68 int parse_state = 0;
\r
70 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
\r
71 // 4 = format, 5 = end
\r
74 while (parse_state == 0)
\r
80 else if (s.charAt(i) == '%')
\r
82 if (i < (length - 1))
\r
84 if (s.charAt(i + 1) == '%')
\r
96 throw new java.lang.IllegalArgumentException();
\r
101 pre = pre + s.charAt(i);
\r
107 while (parse_state == 1)
\r
113 else if (s.charAt(i) == ' ')
\r
117 else if (s.charAt(i) == '-')
\r
121 else if (s.charAt(i) == '+')
\r
125 else if (s.charAt(i) == '0')
\r
127 leading_zeroes = true;
\r
129 else if (s.charAt(i) == '#')
\r
142 while (parse_state == 2)
\r
148 else if ( ('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
\r
150 width = ( (width * 10) + s.charAt(i)) - '0';
\r
153 else if (s.charAt(i) == '.')
\r
165 while (parse_state == 3)
\r
171 else if ( ('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
\r
173 precision = ( (precision * 10) + s.charAt(i)) - '0';
\r
182 if (parse_state == 4)
\r
198 post = s.substring(i, length);
\r
203 * Formats the number following printf conventions.
\r
204 * Main limitation: Can only handle one format parameter at a time
\r
205 * Use multiple Format objects to format more than one number
\r
206 * @param s the format string following printf conventions
\r
207 * The string has a prefix, a format code and a suffix. The prefix and suffix
\r
208 * become part of the formatted output. The format code directs the
\r
209 * formatting of the (single) parameter to be formatted. The code has the
\r
210 * following structure
\r
212 * <li> a % (required)
\r
213 * <li> a modifier (optional)
\r
215 * <dt> + <dd> forces display of + for positive numbers
\r
216 * <dt> 0 <dd> show leading zeroes
\r
217 * <dt> - <dd> align left in the field
\r
218 * <dt> space <dd> prepend a space in front of positive numbers
\r
219 * <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
221 * <li> an integer denoting field width (optional)
\r
222 * <li> a period followed by an integer denoting precision (optional)
\r
223 * <li> a format descriptor (required)
\r
225 * <dt>f <dd> floating point number in fixed format
\r
226 * <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
227 * <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
228 * <dt>d, i <dd> integer in decimal
\r
229 * <dt>x <dd> integer in hexadecimal
\r
230 * <dt>o <dd> integer in octal
\r
231 * <dt>s <dd> string
\r
232 * <dt>c <dd> character
\r
235 * @exception IllegalArgumentException if bad format
\r
238 public static String getHexString(java.awt.Color color)
\r
243 r = Integer.toHexString(color.getRed());
\r
245 if (r.length() < 2)
\r
250 g = Integer.toHexString(color.getGreen());
\r
252 if (g.length() < 2)
\r
257 b = Integer.toHexString(color.getBlue());
\r
259 if (b.length() < 2)
\r
268 * prints a formatted number following printf conventions
\r
269 * @param s a PrintStream
\r
270 * @param fmt the format string
\r
271 * @param x the double to print
\r
273 public static void print(java.io.PrintStream s, String fmt, double x)
\r
275 s.print(new Format(fmt).form(x));
\r
279 * prints a formatted number following printf conventions
\r
280 * @param s a PrintStream
\r
281 * @param fmt the format string
\r
282 * @param x the long to print
\r
284 public static void print(java.io.PrintStream s, String fmt, long x)
\r
286 s.print(new Format(fmt).form(x));
\r
290 * prints a formatted number following printf conventions
\r
291 * @param s a PrintStream
\r
292 * @param fmt the format string
\r
293 * @param x the character to
\r
295 public static void print(java.io.PrintStream s, String fmt, char x)
\r
297 s.print(new Format(fmt).form(x));
\r
301 * prints a formatted number following printf conventions
\r
302 * @param s a PrintStream, fmt the format string
\r
303 * @param x a string that represents the digits to print
\r
305 public static void print(java.io.PrintStream s, String fmt, String x)
\r
307 s.print(new Format(fmt).form(x));
\r
311 * Converts a string of digits (decimal, octal or hex) to an integer
\r
312 * @param s a string
\r
313 * @return the numeric value of the prefix of s representing a base 10 integer
\r
315 public static int atoi(String s)
\r
317 return (int) atol(s);
\r
321 * Converts a string of digits (decimal, octal or hex) to a long integer
\r
322 * @param s a string
\r
323 * @return the numeric value of the prefix of s representing a base 10 integer
\r
325 public static long atol(String s)
\r
329 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
371 if ( (i < s.length()) && (s.charAt(i) == '-'))
\r
376 else if ( (i < s.length()) && (s.charAt(i) == '+'))
\r
381 while (i < s.length())
\r
383 char ch = s.charAt(i);
\r
385 if ( ('0' <= ch) && (ch < ('0' + base)))
\r
387 r = ( (r * base) + ch) - '0';
\r
389 else if ( ('A' <= ch) && (ch < ( ('A' + base) - 10)))
\r
391 r = ( (r * base) + ch) - 'A' + 10;
\r
393 else if ( ('a' <= ch) && (ch < ( ('a' + base) - 10)))
\r
395 r = ( (r * base) + ch) - 'a' + 10;
\r
409 * Converts a string of digits to an double
\r
410 * @param s a string
\r
412 public static double atof(String s)
\r
416 double r = 0; // integer part
\r
417 //double f = 0; // fractional part
\r
418 double p = 1; // exponent of fractional part
\r
419 int state = 0; // 0 = int part, 1 = frac part
\r
421 while ( (i < s.length()) && Character.isWhitespace(s.charAt(i)))
\r
426 if ( (i < s.length()) && (s.charAt(i) == '-'))
\r
431 else if ( (i < s.length()) && (s.charAt(i) == '+'))
\r
436 while (i < s.length())
\r
438 char ch = s.charAt(i);
\r
440 if ( ('0' <= ch) && (ch <= '9'))
\r
444 r = ( (r * 10) + ch) - '0';
\r
446 else if (state == 1)
\r
449 r = r + (p * (ch - '0'));
\r
452 else if (ch == '.')
\r
463 else if ( (ch == 'e') || (ch == 'E'))
\r
465 long e = (int) parseLong(s.substring(i + 1), 10);
\r
467 return sign * r * Math.pow(10, e);
\r
481 * Formats a double into a string (like sprintf in C)
\r
482 * @param x the number to format
\r
483 * @return the formatted string
\r
484 * @exception IllegalArgumentException if bad argument
\r
486 public String form(double x)
\r
505 r = fixed_format(x);
\r
507 else if ( (fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
\r
513 throw new java.lang.IllegalArgumentException();
\r
516 return pad(sign(s, r));
\r
520 * Formats a long integer into a string (like sprintf in C)
\r
521 * @param x the number to format
\r
522 * @return the formatted string
\r
524 public String form(long x)
\r
529 if ( (fmt == 'd') || (fmt == 'i'))
\r
533 r = ("" + x).substring(1);
\r
542 else if (fmt == 'o')
\r
544 r = convert(x, 3, 7, "01234567");
\r
546 else if (fmt == 'x')
\r
548 r = convert(x, 4, 15, "0123456789abcdef");
\r
550 else if (fmt == 'X')
\r
552 r = convert(x, 4, 15, "0123456789ABCDEF");
\r
556 throw new java.lang.IllegalArgumentException();
\r
559 return pad(sign(s, r));
\r
563 * Formats a character into a string (like sprintf in C)
\r
564 * @param x the value to format
\r
565 * @return the formatted string
\r
567 public String form(char c)
\r
571 throw new java.lang.IllegalArgumentException();
\r
580 * Formats a string into a larger string (like sprintf in C)
\r
581 * @param x the value to format
\r
582 * @return the formatted string
\r
584 public String form(String s)
\r
588 throw new java.lang.IllegalArgumentException();
\r
591 if (precision >= 0)
\r
593 s = s.substring(0, precision);
\r
602 * @param c DOCUMENT ME!
\r
603 * @param n DOCUMENT ME!
\r
605 * @return DOCUMENT ME!
\r
607 private static String repeat(char c, int n)
\r
614 StringBuffer s = new StringBuffer(n);
\r
616 for (int i = 0; i < n; i++)
\r
621 return s.toString();
\r
627 * @param x DOCUMENT ME!
\r
628 * @param n DOCUMENT ME!
\r
629 * @param m DOCUMENT ME!
\r
630 * @param d DOCUMENT ME!
\r
632 * @return DOCUMENT ME!
\r
634 private static String convert(long x, int n, int m, String d)
\r
645 r = d.charAt( (int) (x & m)) + r;
\r
655 * @param r DOCUMENT ME!
\r
657 * @return DOCUMENT ME!
\r
659 private String pad(String r)
\r
661 String p = repeat(' ', width - r.length());
\r
665 return pre + r + p + post;
\r
669 return pre + p + r + post;
\r
676 * @param s DOCUMENT ME!
\r
677 * @param r DOCUMENT ME!
\r
679 * @return DOCUMENT ME!
\r
681 private String sign(int s, String r)
\r
695 else if (show_space)
\r
702 if ( (fmt == 'o') && alternate && (r.length() > 0) &&
\r
703 (r.charAt(0) != '0'))
\r
707 else if ( (fmt == 'x') && alternate)
\r
711 else if ( (fmt == 'X') && alternate)
\r
719 if (leading_zeroes)
\r
723 else if ( ( (fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') ||
\r
724 (fmt == 'o')) && (precision > 0))
\r
729 return p + repeat('0', w - p.length() - r.length()) + r;
\r
735 * @param d DOCUMENT ME!
\r
737 * @return DOCUMENT ME!
\r
739 private String fixed_format(double d)
\r
741 boolean removeTrailing = ( (fmt == 'G') || (fmt == 'g')) && !alternate;
\r
743 // remove trailing zeroes and decimal point
\r
744 if (d > 0x7FFFFFFFFFFFFFFFL)
\r
746 return exp_format(d);
\r
749 if (precision == 0)
\r
751 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
\r
754 long whole = (long) d;
\r
755 double fr = d - whole; // fractional part
\r
757 if ( (fr >= 1) || (fr < 0))
\r
759 return exp_format(d);
\r
763 String leading_zeroes = "";
\r
765 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL);
\r
769 leading_zeroes = leading_zeroes + "0";
\r
772 long l = (long) ( (factor * fr) + 0.5);
\r
781 String z = leading_zeroes + l;
\r
782 z = "." + z.substring(z.length() - precision, z.length());
\r
784 if (removeTrailing)
\r
786 int t = z.length() - 1;
\r
788 while ( (t >= 0) && (z.charAt(t) == '0'))
\r
793 if ( (t >= 0) && (z.charAt(t) == '.'))
\r
798 z = z.substring(0, t + 1);
\r
807 * @param d DOCUMENT ME!
\r
809 * @return DOCUMENT ME!
\r
811 private String exp_format(double d)
\r
835 if ( ( (fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
\r
837 return fixed_format(d);
\r
841 f = f + fixed_format(d);
\r
843 if ( (fmt == 'e') || (fmt == 'g'))
\r
865 return f + p.substring(p.length() - 3, p.length());
\r