2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 * A class for formatting numbers that follows printf conventions.
23 * Also implements C-like atoi and atof functions
24 * @version 1.03 25 Oct 1997
25 * @author Cay Horstmann
29 import java.util.Arrays;
32 * A utility class that provides a variety of formatting methods.
34 * Principle method {@code format} formats the number following printf
35 * conventions. Main limitation: Can only handle one format parameter at a time
36 * Use multiple Format objects to format more than one number
44 private int precision;
50 private boolean leading_zeroes;
52 private boolean show_plus;
54 private boolean alternate;
56 private boolean show_space;
58 private boolean left_align;
60 private char fmt; // one of cdeEfgGiosxXos
62 private final String formatString;
65 * Creates a new Format object given a format descriptor, which follows printf
66 * conventions The string has a prefix, a format code and a suffix. The prefix
67 * and suffix become part of the formatted output. The format code directs the
68 * formatting of the (single) parameter to be formatted. The code has the
72 * <li>a modifier (optional)
75 * <dd>forces display of + for positive numbers
77 * <dd>show leading zeroes
79 * <dd>align left in the field
81 * <dd>prepend a space in front of positive numbers
83 * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers.
84 * Don't suppress trailing zeroes in general floating point format.
86 * <li>an integer denoting field width (optional)
87 * <li>a period followed by an integer denoting precision (optional)
88 * <li>a format descriptor (required)
91 * <dd>floating point number in fixed format
93 * <dd>floating point number in exponential notation (scientific format). The
94 * E format results in an uppercase E for the exponent (1.14130E+003), the e
95 * format in a lowercase e.
97 * <dd>floating point number in general format (fixed format for small
98 * numbers, exponential format for large numbers). Trailing zeroes are
99 * suppressed. The G format results in an uppercase E for the exponent (if
100 * any), the g format in a lowercase e.
102 * <dd>integer in decimal
104 * <dd>integer in hexadecimal
106 * <dd>integer in octal
115 * the format descriptor
117 public Format(final String s)
124 leading_zeroes = false;
131 int length = s.length();
134 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
135 // 4 = format, 5 = end
138 while (parse_state == 0)
144 else if (s.charAt(i) == '%')
146 if (i < (length - 1))
148 if (s.charAt(i + 1) == '%')
160 throw new java.lang.IllegalArgumentException();
165 pre = pre + s.charAt(i);
171 while (parse_state == 1)
177 else if (s.charAt(i) == ' ')
181 else if (s.charAt(i) == '-')
185 else if (s.charAt(i) == '+')
189 else if (s.charAt(i) == '0')
191 leading_zeroes = true;
193 else if (s.charAt(i) == '#')
206 while (parse_state == 2)
212 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
214 width = ((width * 10) + s.charAt(i)) - '0';
217 else if (s.charAt(i) == '.')
229 while (parse_state == 3)
235 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
237 precision = ((precision * 10) + s.charAt(i)) - '0';
246 if (parse_state == 4)
262 post = s.substring(i, length);
267 * Returns a 6 character string consisting of the hex values of the colour's
268 * rgb values (left padded with zeroes if necessary)
273 public static String getHexString(java.awt.Color color)
278 r = Integer.toHexString(color.getRed());
285 g = Integer.toHexString(color.getGreen());
292 b = Integer.toHexString(color.getBlue());
303 * prints a formatted number following printf conventions
310 * the double to print
312 public static void print(java.io.PrintStream s, String fmt, double x)
314 s.print(new Format(fmt).form(x));
318 * prints a formatted number following printf conventions
327 public static void print(java.io.PrintStream s, String fmt, long x)
329 s.print(new Format(fmt).form(x));
333 * prints a formatted number following printf conventions
342 public static void print(java.io.PrintStream s, String fmt, char x)
344 s.print(new Format(fmt).form(x));
348 * prints a formatted number following printf conventions
351 * a PrintStream, fmt the format string
353 * a string that represents the digits to print
355 public static void print(java.io.PrintStream s, String fmt, String x)
357 s.print(new Format(fmt).form(x));
361 * Converts a string of digits (decimal, octal or hex) to an integer
365 * @return the numeric value of the prefix of s representing a base 10 integer
367 public static int atoi(String s)
369 return (int) atol(s);
373 * Converts a string of digits (decimal, octal or hex) to a long integer
377 * @return the numeric value of the prefix of s representing a base 10 integer
379 public static long atol(String s)
383 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
388 if ((i < s.length()) && (s.charAt(i) == '0'))
390 if (((i + 1) < s.length())
391 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
393 return parseLong(s.substring(i + 2), 16);
397 return parseLong(s, 8);
402 return parseLong(s, 10);
414 * @return DOCUMENT ME!
416 private static long parseLong(String s, int base)
422 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
427 if ((i < s.length()) && (s.charAt(i) == '-'))
432 else if ((i < s.length()) && (s.charAt(i) == '+'))
437 while (i < s.length())
439 char ch = s.charAt(i);
441 if (('0' <= ch) && (ch < ('0' + base)))
443 r = ((r * base) + ch) - '0';
445 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
447 r = ((r * base) + ch) - 'A' + 10;
449 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
451 r = ((r * base) + ch) - 'a' + 10;
465 * Converts a string of digits to an double
470 public static double atof(String s)
474 double r = 0; // integer part
475 // double f = 0; // fractional part
476 double p = 1; // exponent of fractional part
477 int state = 0; // 0 = int part, 1 = frac part
479 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
484 if ((i < s.length()) && (s.charAt(i) == '-'))
489 else if ((i < s.length()) && (s.charAt(i) == '+'))
494 while (i < s.length())
496 char ch = s.charAt(i);
498 if (('0' <= ch) && (ch <= '9'))
502 r = ((r * 10) + ch) - '0';
507 r = r + (p * (ch - '0'));
521 else if ((ch == 'e') || (ch == 'E'))
523 long e = (int) parseLong(s.substring(i + 1), 10);
525 return sign * r * Math.pow(10, e);
539 * Formats a double into a string (like sprintf in C)
542 * the number to format
543 * @return the formatted string
544 * @exception IllegalArgumentException
547 public String form(double x)
568 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
574 throw new java.lang.IllegalArgumentException();
577 return pad(sign(s, r));
581 * Formats a long integer into a string (like sprintf in C)
584 * the number to format
585 * @return the formatted string
587 public String form(long x)
592 if ((fmt == 'd') || (fmt == 'i'))
596 r = ("" + x).substring(1);
607 r = convert(x, 3, 7, "01234567");
611 r = convert(x, 4, 15, "0123456789abcdef");
615 r = convert(x, 4, 15, "0123456789ABCDEF");
619 throw new java.lang.IllegalArgumentException();
622 return pad(sign(s, r));
626 * Formats a character into a string (like sprintf in C)
628 * @param debounceTrap
629 * the value to format
630 * @return the formatted string
632 public String form(char c)
636 throw new java.lang.IllegalArgumentException();
645 * Formats a string into a larger string (like sprintf in C)
647 * @param debounceTrap
648 * the value to format
649 * @return the formatted string
651 public String form(String s)
655 throw new java.lang.IllegalArgumentException();
660 s = s.substring(0, precision);
667 * Returns a string consisting of n repeats of character c
674 static String repeat(char c, int n)
680 char[] chars = new char[n];
681 Arrays.fill(chars, c);
682 return new String(chars);
697 * @return DOCUMENT ME!
699 private static String convert(long x, int n, int m, String d)
710 r = d.charAt((int) (x & m)) + r;
723 * @return DOCUMENT ME!
725 private String pad(String r)
727 String p = repeat(' ', width - r.length());
731 return pre + r + p + post;
735 return pre + p + r + post;
747 * @return DOCUMENT ME!
749 private String sign(int s, String r)
770 if ((fmt == 'o') && alternate && (r.length() > 0)
771 && (r.charAt(0) != '0'))
775 else if ((fmt == 'x') && alternate)
779 else if ((fmt == 'X') && alternate)
791 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X')
792 || (fmt == 'o')) && (precision > 0))
797 return p + repeat('0', w - p.length() - r.length()) + r;
806 * @return DOCUMENT ME!
808 private String fixed_format(double d)
810 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
812 // remove trailing zeroes and decimal point
813 if (d > 0x7FFFFFFFFFFFFFFFL)
815 return exp_format(d);
820 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
823 long whole = (long) d;
824 double fr = d - whole; // fractional part
826 if ((fr >= 1) || (fr < 0))
828 return exp_format(d);
832 String leading_zeroes = "";
834 for (int i = 1; (i <= precision)
835 && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
838 leading_zeroes = leading_zeroes + "0";
841 long l = (long) ((factor * fr) + 0.5);
850 String z = leading_zeroes + l;
851 z = "." + z.substring(z.length() - precision, z.length());
855 int t = z.length() - 1;
857 while ((t >= 0) && (z.charAt(t) == '0'))
862 if ((t >= 0) && (z.charAt(t) == '.'))
867 z = z.substring(0, t + 1);
879 * @return DOCUMENT ME!
881 private String exp_format(double d)
902 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
904 return fixed_format(d);
907 f = f + fixed_format(dd);
909 if ((fmt == 'e') || (fmt == 'g'))
931 return f + p.substring(p.length() - 3, p.length());
935 public String toString()
941 * Bespoke method to format percentage float value to the specified number of
942 * decimal places. Avoids use of general-purpose format parsers as a
943 * processing hotspot.
949 public static void appendPercentage(StringBuilder sb, float value, int dp)
956 for (int i = 0; i < dp; i++)
966 value = (float) (d / factor);
967 sb.append((long) value);
977 value = value - (int) value;
979 sb.append((int) value);