Jalview 2.6 source licence
[jalview.git] / src / jalview / util / Format.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /**
19  * A class for formatting numbers that follows printf conventions.
20  * Also implements C-like atoi and atof functions
21  * @version 1.03 25 Oct 1997
22  * @author Cay Horstmann
23  */
24 package jalview.util;
25
26 /**
27  * DOCUMENT ME!
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class Format
33 {
34   private int width;
35
36   private int precision;
37
38   private String pre;
39
40   private String post;
41
42   private boolean leading_zeroes;
43
44   private boolean show_plus;
45
46   private boolean alternate;
47
48   private boolean show_space;
49
50   private boolean left_align;
51
52   private char fmt; // one of cdeEfgGiosxXos
53
54   /**
55    * Creates a new Format object.
56    * 
57    * @param s
58    *          DOCUMENT ME!
59    */
60   public Format(String s)
61   {
62     width = 0;
63     precision = -1;
64     pre = "";
65     post = "";
66     leading_zeroes = false;
67     show_plus = false;
68     alternate = false;
69     show_space = false;
70     left_align = false;
71     fmt = ' ';
72
73     int length = s.length();
74     int parse_state = 0;
75
76     // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
77     // 4 = format, 5 = end
78     int i = 0;
79
80     while (parse_state == 0)
81     {
82       if (i >= length)
83       {
84         parse_state = 5;
85       }
86       else if (s.charAt(i) == '%')
87       {
88         if (i < (length - 1))
89         {
90           if (s.charAt(i + 1) == '%')
91           {
92             pre = pre + '%';
93             i++;
94           }
95           else
96           {
97             parse_state = 1;
98           }
99         }
100         else
101         {
102           throw new java.lang.IllegalArgumentException();
103         }
104       }
105       else
106       {
107         pre = pre + s.charAt(i);
108       }
109
110       i++;
111     }
112
113     while (parse_state == 1)
114     {
115       if (i >= length)
116       {
117         parse_state = 5;
118       }
119       else if (s.charAt(i) == ' ')
120       {
121         show_space = true;
122       }
123       else if (s.charAt(i) == '-')
124       {
125         left_align = true;
126       }
127       else if (s.charAt(i) == '+')
128       {
129         show_plus = true;
130       }
131       else if (s.charAt(i) == '0')
132       {
133         leading_zeroes = true;
134       }
135       else if (s.charAt(i) == '#')
136       {
137         alternate = true;
138       }
139       else
140       {
141         parse_state = 2;
142         i--;
143       }
144
145       i++;
146     }
147
148     while (parse_state == 2)
149     {
150       if (i >= length)
151       {
152         parse_state = 5;
153       }
154       else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
155       {
156         width = ((width * 10) + s.charAt(i)) - '0';
157         i++;
158       }
159       else if (s.charAt(i) == '.')
160       {
161         parse_state = 3;
162         precision = 0;
163         i++;
164       }
165       else
166       {
167         parse_state = 4;
168       }
169     }
170
171     while (parse_state == 3)
172     {
173       if (i >= length)
174       {
175         parse_state = 5;
176       }
177       else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9'))
178       {
179         precision = ((precision * 10) + s.charAt(i)) - '0';
180         i++;
181       }
182       else
183       {
184         parse_state = 4;
185       }
186     }
187
188     if (parse_state == 4)
189     {
190       if (i >= length)
191       {
192         parse_state = 5;
193       }
194       else
195       {
196         fmt = s.charAt(i);
197       }
198
199       i++;
200     }
201
202     if (i < length)
203     {
204       post = s.substring(i, length);
205     }
206   }
207
208 /**
209    * Formats the number following printf conventions. Main limitation: Can only
210    * handle one format parameter at a time Use multiple Format objects to format
211    * more than one number
212    * 
213    * @param s
214    *                the format string following printf conventions The string
215    *                has a prefix, a format code and a suffix. The prefix and
216    *                suffix become part of the formatted output. The format code
217    *                directs the formatting of the (single) parameter to be
218    *                formatted. The code has the following structure
219    *                <ul>
220    *                <li> a % (required)
221    *                <li> a modifier (optional)
222    *                <dl>
223    *                <dt> +
224    *                <dd> forces display of + for positive numbers
225    *                <dt> 0
226    *                <dd> show leading zeroes
227    *                <dt> -
228    *                <dd> align left in the field
229    *                <dt> space
230    *                <dd> prepend a space in front of positive numbers
231    *                <dt> #
232    *                <dd> use "alternate" format. Add 0 or 0x for octal or
233    *                hexadecimal numbers. Don't suppress trailing zeroes in
234    *                general floating point format.
235    *                </dl>
236    *                <li> an integer denoting field width (optional)
237    *                <li> a period followed by an integer denoting precision
238    *                (optional)
239    *                <li> a format descriptor (required)
240    *                <dl>
241    *                <dt>f
242    *                <dd> floating point number in fixed format
243    *                <dt>e, E
244    *                <dd> floating point number in exponential notation
245    *                (scientific format). The E format results in an uppercase E
246    *                for the exponent (1.14130E+003), the e format in a lowercase
247    *                e.
248    *                <dt>g, G
249    *                <dd> floating point number in general format (fixed format
250    *                for small numbers, exponential format for large numbers).
251    *                Trailing zeroes are suppressed. The G format results in an
252    *                uppercase E for the exponent (if any), the g format in a
253    *                lowercase e.
254    *                <dt>d, i
255    *                <dd> integer in decimal
256    *                <dt>x
257    *                <dd> integer in hexadecimal
258    *                <dt>o
259    *                <dd> integer in octal
260    *                <dt>s
261    *                <dd> string
262    *                <dt>c
263    *                <dd> character
264    *                </dl>
265    *                </ul>
266    * @exception IllegalArgumentException
267    *                    if bad format
268    * 
269    */
270   public static String getHexString(java.awt.Color color)
271   {
272     String r;
273     String g;
274     String b;
275     r = Integer.toHexString(color.getRed());
276
277     if (r.length() < 2)
278     {
279       r = "0" + r;
280     }
281
282     g = Integer.toHexString(color.getGreen());
283
284     if (g.length() < 2)
285     {
286       g = "0" + g;
287     }
288
289     b = Integer.toHexString(color.getBlue());
290
291     if (b.length() < 2)
292     {
293       b = "0" + b;
294     }
295
296     return r + g + b;
297   }
298
299   /**
300    * prints a formatted number following printf conventions
301    * 
302    * @param s
303    *          a PrintStream
304    * @param fmt
305    *          the format string
306    * @param x
307    *          the double to print
308    */
309   public static void print(java.io.PrintStream s, String fmt, double x)
310   {
311     s.print(new Format(fmt).form(x));
312   }
313
314   /**
315    * prints a formatted number following printf conventions
316    * 
317    * @param s
318    *          a PrintStream
319    * @param fmt
320    *          the format string
321    * @param x
322    *          the long to print
323    */
324   public static void print(java.io.PrintStream s, String fmt, long x)
325   {
326     s.print(new Format(fmt).form(x));
327   }
328
329   /**
330    * prints a formatted number following printf conventions
331    * 
332    * @param s
333    *          a PrintStream
334    * @param fmt
335    *          the format string
336    * @param x
337    *          the character to
338    */
339   public static void print(java.io.PrintStream s, String fmt, char x)
340   {
341     s.print(new Format(fmt).form(x));
342   }
343
344   /**
345    * prints a formatted number following printf conventions
346    * 
347    * @param s
348    *          a PrintStream, fmt the format string
349    * @param x
350    *          a string that represents the digits to print
351    */
352   public static void print(java.io.PrintStream s, String fmt, String x)
353   {
354     s.print(new Format(fmt).form(x));
355   }
356
357   /**
358    * Converts a string of digits (decimal, octal or hex) to an integer
359    * 
360    * @param s
361    *          a string
362    * @return the numeric value of the prefix of s representing a base 10 integer
363    */
364   public static int atoi(String s)
365   {
366     return (int) atol(s);
367   }
368
369   /**
370    * Converts a string of digits (decimal, octal or hex) to a long integer
371    * 
372    * @param s
373    *          a string
374    * @return the numeric value of the prefix of s representing a base 10 integer
375    */
376   public static long atol(String s)
377   {
378     int i = 0;
379
380     while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
381     {
382       i++;
383     }
384
385     if ((i < s.length()) && (s.charAt(i) == '0'))
386     {
387       if (((i + 1) < s.length())
388               && ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X')))
389       {
390         return parseLong(s.substring(i + 2), 16);
391       }
392       else
393       {
394         return parseLong(s, 8);
395       }
396     }
397     else
398     {
399       return parseLong(s, 10);
400     }
401   }
402
403   /**
404    * DOCUMENT ME!
405    * 
406    * @param s
407    *          DOCUMENT ME!
408    * @param base
409    *          DOCUMENT ME!
410    * 
411    * @return DOCUMENT ME!
412    */
413   private static long parseLong(String s, int base)
414   {
415     int i = 0;
416     int sign = 1;
417     long r = 0;
418
419     while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
420     {
421       i++;
422     }
423
424     if ((i < s.length()) && (s.charAt(i) == '-'))
425     {
426       sign = -1;
427       i++;
428     }
429     else if ((i < s.length()) && (s.charAt(i) == '+'))
430     {
431       i++;
432     }
433
434     while (i < s.length())
435     {
436       char ch = s.charAt(i);
437
438       if (('0' <= ch) && (ch < ('0' + base)))
439       {
440         r = ((r * base) + ch) - '0';
441       }
442       else if (('A' <= ch) && (ch < (('A' + base) - 10)))
443       {
444         r = ((r * base) + ch) - 'A' + 10;
445       }
446       else if (('a' <= ch) && (ch < (('a' + base) - 10)))
447       {
448         r = ((r * base) + ch) - 'a' + 10;
449       }
450       else
451       {
452         return r * sign;
453       }
454
455       i++;
456     }
457
458     return r * sign;
459   }
460
461   /**
462    * Converts a string of digits to an double
463    * 
464    * @param s
465    *          a string
466    */
467   public static double atof(String s)
468   {
469     int i = 0;
470     int sign = 1;
471     double r = 0; // integer part
472     // double f = 0; // fractional part
473     double p = 1; // exponent of fractional part
474     int state = 0; // 0 = int part, 1 = frac part
475
476     while ((i < s.length()) && Character.isWhitespace(s.charAt(i)))
477     {
478       i++;
479     }
480
481     if ((i < s.length()) && (s.charAt(i) == '-'))
482     {
483       sign = -1;
484       i++;
485     }
486     else if ((i < s.length()) && (s.charAt(i) == '+'))
487     {
488       i++;
489     }
490
491     while (i < s.length())
492     {
493       char ch = s.charAt(i);
494
495       if (('0' <= ch) && (ch <= '9'))
496       {
497         if (state == 0)
498         {
499           r = ((r * 10) + ch) - '0';
500         }
501         else if (state == 1)
502         {
503           p = p / 10;
504           r = r + (p * (ch - '0'));
505         }
506       }
507       else if (ch == '.')
508       {
509         if (state == 0)
510         {
511           state = 1;
512         }
513         else
514         {
515           return sign * r;
516         }
517       }
518       else if ((ch == 'e') || (ch == 'E'))
519       {
520         long e = (int) parseLong(s.substring(i + 1), 10);
521
522         return sign * r * Math.pow(10, e);
523       }
524       else
525       {
526         return sign * r;
527       }
528
529       i++;
530     }
531
532     return sign * r;
533   }
534
535   /**
536    * Formats a double into a string (like sprintf in C)
537    * 
538    * @param x
539    *          the number to format
540    * @return the formatted string
541    * @exception IllegalArgumentException
542    *              if bad argument
543    */
544   public String form(double x)
545   {
546     String r;
547
548     if (precision < 0)
549     {
550       precision = 6;
551     }
552
553     int s = 1;
554
555     if (x < 0)
556     {
557       x = -x;
558       s = -1;
559     }
560
561     if (fmt == 'f')
562     {
563       r = fixed_format(x);
564     }
565     else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G'))
566     {
567       r = exp_format(x);
568     }
569     else
570     {
571       throw new java.lang.IllegalArgumentException();
572     }
573
574     return pad(sign(s, r));
575   }
576
577   /**
578    * Formats a long integer into a string (like sprintf in C)
579    * 
580    * @param x
581    *          the number to format
582    * @return the formatted string
583    */
584   public String form(long x)
585   {
586     String r;
587     int s = 0;
588
589     if ((fmt == 'd') || (fmt == 'i'))
590     {
591       if (x < 0)
592       {
593         r = ("" + x).substring(1);
594         s = -1;
595       }
596       else
597       {
598         r = "" + x;
599         s = 1;
600       }
601     }
602     else if (fmt == 'o')
603     {
604       r = convert(x, 3, 7, "01234567");
605     }
606     else if (fmt == 'x')
607     {
608       r = convert(x, 4, 15, "0123456789abcdef");
609     }
610     else if (fmt == 'X')
611     {
612       r = convert(x, 4, 15, "0123456789ABCDEF");
613     }
614     else
615     {
616       throw new java.lang.IllegalArgumentException();
617     }
618
619     return pad(sign(s, r));
620   }
621
622   /**
623    * Formats a character into a string (like sprintf in C)
624    * 
625    * @param x
626    *          the value to format
627    * @return the formatted string
628    */
629   public String form(char c)
630   {
631     if (fmt != 'c')
632     {
633       throw new java.lang.IllegalArgumentException();
634     }
635
636     String r = "" + c;
637
638     return pad(r);
639   }
640
641   /**
642    * Formats a string into a larger string (like sprintf in C)
643    * 
644    * @param x
645    *          the value to format
646    * @return the formatted string
647    */
648   public String form(String s)
649   {
650     if (fmt != 's')
651     {
652       throw new java.lang.IllegalArgumentException();
653     }
654
655     if (precision >= 0)
656     {
657       s = s.substring(0, precision);
658     }
659
660     return pad(s);
661   }
662
663   /**
664    * DOCUMENT ME!
665    * 
666    * @param c
667    *          DOCUMENT ME!
668    * @param n
669    *          DOCUMENT ME!
670    * 
671    * @return DOCUMENT ME!
672    */
673   private static String repeat(char c, int n)
674   {
675     if (n <= 0)
676     {
677       return "";
678     }
679
680     StringBuffer s = new StringBuffer(n);
681
682     for (int i = 0; i < n; i++)
683     {
684       s.append(c);
685     }
686
687     return s.toString();
688   }
689
690   /**
691    * DOCUMENT ME!
692    * 
693    * @param x
694    *          DOCUMENT ME!
695    * @param n
696    *          DOCUMENT ME!
697    * @param m
698    *          DOCUMENT ME!
699    * @param d
700    *          DOCUMENT ME!
701    * 
702    * @return DOCUMENT ME!
703    */
704   private static String convert(long x, int n, int m, String d)
705   {
706     if (x == 0)
707     {
708       return "0";
709     }
710
711     String r = "";
712
713     while (x != 0)
714     {
715       r = d.charAt((int) (x & m)) + r;
716       x = x >>> n;
717     }
718
719     return r;
720   }
721
722   /**
723    * DOCUMENT ME!
724    * 
725    * @param r
726    *          DOCUMENT ME!
727    * 
728    * @return DOCUMENT ME!
729    */
730   private String pad(String r)
731   {
732     String p = repeat(' ', width - r.length());
733
734     if (left_align)
735     {
736       return pre + r + p + post;
737     }
738     else
739     {
740       return pre + p + r + post;
741     }
742   }
743
744   /**
745    * DOCUMENT ME!
746    * 
747    * @param s
748    *          DOCUMENT ME!
749    * @param r
750    *          DOCUMENT ME!
751    * 
752    * @return DOCUMENT ME!
753    */
754   private String sign(int s, String r)
755   {
756     String p = "";
757
758     if (s < 0)
759     {
760       p = "-";
761     }
762     else if (s > 0)
763     {
764       if (show_plus)
765       {
766         p = "+";
767       }
768       else if (show_space)
769       {
770         p = " ";
771       }
772     }
773     else
774     {
775       if ((fmt == 'o') && alternate && (r.length() > 0)
776               && (r.charAt(0) != '0'))
777       {
778         p = "0";
779       }
780       else if ((fmt == 'x') && alternate)
781       {
782         p = "0x";
783       }
784       else if ((fmt == 'X') && alternate)
785       {
786         p = "0X";
787       }
788     }
789
790     int w = 0;
791
792     if (leading_zeroes)
793     {
794       w = width;
795     }
796     else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || (fmt == 'o'))
797             && (precision > 0))
798     {
799       w = precision;
800     }
801
802     return p + repeat('0', w - p.length() - r.length()) + r;
803   }
804
805   /**
806    * DOCUMENT ME!
807    * 
808    * @param d
809    *          DOCUMENT ME!
810    * 
811    * @return DOCUMENT ME!
812    */
813   private String fixed_format(double d)
814   {
815     boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate;
816
817     // remove trailing zeroes and decimal point
818     if (d > 0x7FFFFFFFFFFFFFFFL)
819     {
820       return exp_format(d);
821     }
822
823     if (precision == 0)
824     {
825       return (long) (d + 0.5) + (removeTrailing ? "" : ".");
826     }
827
828     long whole = (long) d;
829     double fr = d - whole; // fractional part
830
831     if ((fr >= 1) || (fr < 0))
832     {
833       return exp_format(d);
834     }
835
836     double factor = 1;
837     String leading_zeroes = "";
838
839     for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); i++)
840     {
841       factor *= 10;
842       leading_zeroes = leading_zeroes + "0";
843     }
844
845     long l = (long) ((factor * fr) + 0.5);
846
847     if (l >= factor)
848     {
849       l = 0;
850       whole++;
851     }
852
853     // CSH 10-25-97
854     String z = leading_zeroes + l;
855     z = "." + z.substring(z.length() - precision, z.length());
856
857     if (removeTrailing)
858     {
859       int t = z.length() - 1;
860
861       while ((t >= 0) && (z.charAt(t) == '0'))
862       {
863         t--;
864       }
865
866       if ((t >= 0) && (z.charAt(t) == '.'))
867       {
868         t--;
869       }
870
871       z = z.substring(0, t + 1);
872     }
873
874     return whole + z;
875   }
876
877   /**
878    * DOCUMENT ME!
879    * 
880    * @param d
881    *          DOCUMENT ME!
882    * 
883    * @return DOCUMENT ME!
884    */
885   private String exp_format(double d)
886   {
887     String f = "";
888     int e = 0;
889     double dd = d;
890     double factor = 1;
891
892     if (d != 0)
893     {
894       while (dd > 10)
895       {
896         e++;
897         factor /= 10;
898         dd = dd / 10;
899       }
900
901       while (dd < 1)
902       {
903         e--;
904         factor *= 10;
905         dd = dd * 10;
906       }
907     }
908
909     if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision))
910     {
911       return fixed_format(d);
912     }
913
914     d = d * factor;
915     f = f + fixed_format(d);
916
917     if ((fmt == 'e') || (fmt == 'g'))
918     {
919       f = f + "e";
920     }
921     else
922     {
923       f = f + "E";
924     }
925
926     String p = "000";
927
928     if (e >= 0)
929     {
930       f = f + "+";
931       p = p + e;
932     }
933     else
934     {
935       f = f + "-";
936       p = p + (-e);
937     }
938
939     return f + p.substring(p.length() - 3, p.length());
940   }
941 }