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 jalview.api.ColorI;
31 import java.awt.Color;
43 private int precision;
49 private boolean leading_zeroes;
51 private boolean show_plus;
53 private boolean alternate;
55 private boolean show_space;
57 private boolean left_align;
59 private char fmt; // one of cdeEfgGiosxXos
61 private final String formatString;
64 * Creates a new Format object.
69 public Format(String s)
76 leading_zeroes = false;
83 int length = s.length();
86 // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
87 // 4 = format, 5 = end
90 while (parse_state == 0)
96 else if (s.charAt(i) == '%')
100 if (s.charAt(i + 1) == '%')
112 throw new java.lang.IllegalArgumentException();
117 pre = pre + s.charAt(i);
123 while (parse_state == 1)
129 else if (s.charAt(i) == ' ')
133 else if (s.charAt(i) == '-')
137 else if (s.charAt(i) == '+')
141 else if (s.charAt(i) == '0')
143 leading_zeroes = true;
145 else if (s.charAt(i) == '#')
158 while (parse_state == 2)
164 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
166 width = ((width * 10) + s.charAt(i)) - '0';
169 else if (s.charAt(i) == '.')
181 while (parse_state == 3)
187 else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
189 precision = ((precision * 10) + s.charAt(i)) - '0';
198 if (parse_state == 4)
214 post = s.substring(i, length);
219 * Formats the number following printf conventions. Main limitation: Can only
220 * handle one format parameter at a time Use multiple Format objects to format
221 * more than one number
224 * the format string following printf conventions The string has a
225 * prefix, a format code and a suffix. The prefix and suffix become
226 * part of the formatted output. The format code directs the
227 * formatting of the (single) parameter to be formatted. The code has
228 * the following structure
231 * <li>a modifier (optional)
234 * <dd>forces display of + for positive numbers
236 * <dd>show leading zeroes
238 * <dd>align left in the field
240 * <dd>prepend a space in front of positive numbers
242 * <dd>use "alternate" format. Add 0 or 0x for octal or hexadecimal
243 * numbers. Don't suppress trailing zeroes in general floating point
246 * <li>an integer denoting field width (optional)
247 * <li>a period followed by an integer denoting precision (optional)
248 * <li>a format descriptor (required)
251 * <dd>floating point number in fixed format
253 * <dd>floating point number in exponential notation (scientific
254 * format). The E format results in an uppercase E for the exponent
255 * (1.14130E+003), the e format in a lowercase e.
257 * <dd>floating point number in general format (fixed format for
258 * small numbers, exponential format for large numbers). Trailing
259 * zeroes are suppressed. The G format results in an uppercase E for
260 * the exponent (if any), the g format in a lowercase e.
262 * <dd>integer in decimal
264 * <dd>integer in hexadecimal
266 * <dd>integer in octal
273 * @exception IllegalArgumentException
277 public static String getHexString(final Color c)
279 return getHexString(new ColorI()
289 public int getGreen()
307 public static String getHexString(ColorI color)
312 r = Integer.toHexString(color.getRed());
319 g = Integer.toHexString(color.getGreen());
326 b = Integer.toHexString(color.getBlue());
337 * prints a formatted number following printf conventions
344 * the double to print
346 public static void print(java.io.PrintStream s, String fmt, double x)
348 s.print(new Format(fmt).form(x));
352 * prints a formatted number following printf conventions
361 public static void print(java.io.PrintStream s, String fmt, long x)
363 s.print(new Format(fmt).form(x));
367 * prints a formatted number following printf conventions
376 public static void print(java.io.PrintStream s, String fmt, char x)
378 s.print(new Format(fmt).form(x));
382 * prints a formatted number following printf conventions
385 * a PrintStream, fmt the format string
387 * a string that represents the digits to print
389 public static void print(java.io.PrintStream s, String fmt, String x)
391 s.print(new Format(fmt).form(x));
395 * Converts a string of digits (decimal, octal or hex) to an integer
399 * @return the numeric value of the prefix of s representing a base 10 integer
401 public static int atoi(String s)
403 return (int) atol(s);
407 * Converts a string of digits (decimal, octal or hex) to a long integer
411 * @return the numeric value of the prefix of s representing a base 10 integer
413 public static long atol(String s)
417 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
422 if ((i < s.length()) && (s.charAt(i) == '0'))
424 if (((i + 1) < s.length())
425 && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
427 return parseLong(s.substring(i + 2), 16);
431 return parseLong(s, 8);
436 return parseLong(s, 10);
448 * @return DOCUMENT ME!
450 private static long parseLong(String s, int base)
456 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
461 if ((i < s.length()) && (s.charAt(i) == '-'))
466 else if ((i < s.length()) && (s.charAt(i) == '+'))
471 while (i < s.length())
473 char ch = s.charAt(i);
475 if (('0' <= ch) && (ch < ('0' + base)))
477 r = ((r * base) + ch) - '0';
479 else if (('A' <= ch) && (ch < (('A' + base) - 10)))
481 r = ((r * base) + ch) - 'A' + 10;
483 else if (('a' <= ch) && (ch < (('a' + base) - 10)))
485 r = ((r * base) + ch) - 'a' + 10;
499 * Converts a string of digits to an double
504 public static double atof(String s)
508 double r = 0; // integer part
509 // double f = 0; // fractional part
510 double p = 1; // exponent of fractional part
511 int state = 0; // 0 = int part, 1 = frac part
513 while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
518 if ((i < s.length()) && (s.charAt(i) == '-'))
523 else if ((i < s.length()) && (s.charAt(i) == '+'))
528 while (i < s.length())
530 char ch = s.charAt(i);
532 if (('0' <= ch) && (ch <= '9'))
536 r = ((r * 10) + ch) - '0';
541 r = r + (p * (ch - '0'));
555 else if ((ch == 'e') || (ch == 'E'))
557 long e = (int) parseLong(s.substring(i + 1), 10);
559 return sign * r * Math.pow(10, e);
573 * Formats a double into a string (like sprintf in C)
576 * the number to format
577 * @return the formatted string
578 * @exception IllegalArgumentException
581 public String form(double x)
602 else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
608 throw new java.lang.IllegalArgumentException();
611 return pad(sign(s, r));
615 * Formats a long integer into a string (like sprintf in C)
618 * the number to format
619 * @return the formatted string
621 public String form(long x)
626 if ((fmt == 'd') || (fmt == 'i'))
630 r = ("" + x).substring(1);
641 r = convert(x, 3, 7, "01234567");
645 r = convert(x, 4, 15, "0123456789abcdef");
649 r = convert(x, 4, 15, "0123456789ABCDEF");
653 throw new java.lang.IllegalArgumentException();
656 return pad(sign(s, r));
660 * Formats a character into a string (like sprintf in C)
662 * @param debounceTrap
663 * the value to format
664 * @return the formatted string
666 public String form(char c)
670 throw new java.lang.IllegalArgumentException();
679 * Formats a string into a larger string (like sprintf in C)
681 * @param debounceTrap
682 * the value to format
683 * @return the formatted string
685 public String form(String s)
689 throw new java.lang.IllegalArgumentException();
694 s = s.substring(0, precision);
708 * @return DOCUMENT ME!
710 private static String repeat(char c, int n)
717 StringBuffer s = new StringBuffer(n);
719 for (int i = 0; i < n; i++)
739 * @return DOCUMENT ME!
741 private static String convert(long x, int n, int m, String d)
752 r = d.charAt((int) (x & m)) + r;
765 * @return DOCUMENT ME!
767 private String pad(String r)
769 String p = repeat(' ', width - r.length());
773 return pre + r + p + post;
777 return pre + p + r + post;
789 * @return DOCUMENT ME!
791 private String sign(int s, String r)
812 if ((fmt == 'o') && alternate && (r.length() > 0)
813 && (r.charAt(0) != '0'))
817 else if ((fmt == 'x') && alternate)
821 else if ((fmt == 'X') && alternate)
833 else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
839 return p + repeat('0', w - p.length() - r.length()) + r;
848 * @return DOCUMENT ME!
850 private String fixed_format(double d)
852 boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
854 // remove trailing zeroes and decimal point
855 if (d > 0x7FFFFFFFFFFFFFFFL)
857 return exp_format(d);
862 return (long) (d + 0.5) + (removeTrailing ? "" : ".");
865 long whole = (long) d;
866 double fr = d - whole; // fractional part
868 if ((fr >= 1) || (fr < 0))
870 return exp_format(d);
874 String leading_zeroes = "";
876 for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
879 leading_zeroes = leading_zeroes + "0";
882 long l = (long) ((factor * fr) + 0.5);
891 String z = leading_zeroes + l;
892 z = "." + z.substring(z.length() - precision, z.length());
896 int t = z.length() - 1;
898 while ((t >= 0) && (z.charAt(t) == '0'))
903 if ((t >= 0) && (z.charAt(t) == '.'))
908 z = z.substring(0, t + 1);
920 * @return DOCUMENT ME!
922 private String exp_format(double d)
946 if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
948 return fixed_format(d);
952 f = f + fixed_format(d);
954 if ((fmt == 'e') || (fmt == 'g'))
976 return f + p.substring(p.length() - 3, p.length());
980 public String toString()