X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FFormat.java;h=a78123fd3f95ad29465e20bd5334369c52f9055d;hb=ff5f2b888385f85e9fd99a226c9da098a06016d8;hp=4c47f656a6b43c59121ebdd9d1ac8151ced28619;hpb=588042b69abf8e60bcc950b24c283933c7dd422f;p=jalview.git diff --git a/src/jalview/util/Format.java b/src/jalview/util/Format.java index 4c47f65..a78123f 100755 --- a/src/jalview/util/Format.java +++ b/src/jalview/util/Format.java @@ -27,10 +27,16 @@ */ package jalview.util; -import java.io.*; -public class Format { +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class Format +{ private int width; private int precision; private String pre; @@ -42,7 +48,13 @@ public class Format { private boolean left_align; private char fmt; // one of cdeEfgGiosxXos - public Format(String s) { + /** + * Creates a new Format object. + * + * @param s DOCUMENT ME! + */ + public Format(String s) + { width = 0; precision = -1; pre = ""; @@ -54,7 +66,6 @@ public class Format { left_align = false; fmt = ' '; - int state = 0; int length = s.length(); int parse_state = 0; @@ -62,41 +73,67 @@ public class Format { // 4 = format, 5 = end int i = 0; - while (parse_state == 0) { - if (i >= length) { + while (parse_state == 0) + { + if (i >= length) + { parse_state = 5; - } else if (s.charAt(i) == '%') { - if (i < (length - 1)) { - if (s.charAt(i + 1) == '%') { + } + else if (s.charAt(i) == '%') + { + if (i < (length - 1)) + { + if (s.charAt(i + 1) == '%') + { pre = pre + '%'; i++; - } else { + } + else + { parse_state = 1; } - } else { + } + else + { throw new java.lang.IllegalArgumentException(); } - } else { + } + else + { pre = pre + s.charAt(i); } i++; } - while (parse_state == 1) { - if (i >= length) { + while (parse_state == 1) + { + if (i >= length) + { parse_state = 5; - } else if (s.charAt(i) == ' ') { + } + else if (s.charAt(i) == ' ') + { show_space = true; - } else if (s.charAt(i) == '-') { + } + else if (s.charAt(i) == '-') + { left_align = true; - } else if (s.charAt(i) == '+') { + } + else if (s.charAt(i) == '+') + { show_plus = true; - } else if (s.charAt(i) == '0') { + } + else if (s.charAt(i) == '0') + { leading_zeroes = true; - } else if (s.charAt(i) == '#') { + } + else if (s.charAt(i) == '#') + { alternate = true; - } else { + } + else + { parse_state = 2; i--; } @@ -104,43 +141,62 @@ public class Format { i++; } - while (parse_state == 2) { - if (i >= length) { + while (parse_state == 2) + { + if (i >= length) + { parse_state = 5; - } else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9')) { + } + else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9')) + { width = ((width * 10) + s.charAt(i)) - '0'; i++; - } else if (s.charAt(i) == '.') { + } + else if (s.charAt(i) == '.') + { parse_state = 3; precision = 0; i++; - } else { + } + else + { parse_state = 4; } } - while (parse_state == 3) { - if (i >= length) { + while (parse_state == 3) + { + if (i >= length) + { parse_state = 5; - } else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9')) { + } + else if (('0' <= s.charAt(i)) && (s.charAt(i) <= '9')) + { precision = ((precision * 10) + s.charAt(i)) - '0'; i++; - } else { + } + else + { parse_state = 4; } } - if (parse_state == 4) { - if (i >= length) { + if (parse_state == 4) + { + if (i >= length) + { parse_state = 5; - } else { + } + else + { fmt = s.charAt(i); } i++; } - if (i < length) { + if (i < length) + { post = s.substring(i, length); } } @@ -181,25 +237,29 @@ public class Format { * @exception IllegalArgumentException if bad format * */ - public static String getHexString(java.awt.Color color) { + public static String getHexString(java.awt.Color color) + { String r; String g; String b; r = Integer.toHexString(color.getRed()); - if (r.length() < 2) { + if (r.length() < 2) + { r = "0" + r; } g = Integer.toHexString(color.getGreen()); - if (g.length() < 2) { + if (g.length() < 2) + { g = "0" + g; } b = Integer.toHexString(color.getBlue()); - if (b.length() < 2) { + if (b.length() < 2) + { b = "0" + b; } @@ -212,7 +272,8 @@ public class Format { * @param fmt the format string * @param x the double to print */ - public static void print(java.io.PrintStream s, String fmt, double x) { + public static void print(java.io.PrintStream s, String fmt, double x) + { s.print(new Format(fmt).form(x)); } @@ -222,7 +283,8 @@ public class Format { * @param fmt the format string * @param x the long to print */ - public static void print(java.io.PrintStream s, String fmt, long x) { + public static void print(java.io.PrintStream s, String fmt, long x) + { s.print(new Format(fmt).form(x)); } @@ -232,7 +294,8 @@ public class Format { * @param fmt the format string * @param x the character to */ - public static void print(java.io.PrintStream s, String fmt, char x) { + public static void print(java.io.PrintStream s, String fmt, char x) + { s.print(new Format(fmt).form(x)); } @@ -241,7 +304,8 @@ public class Format { * @param s a PrintStream, fmt the format string * @param x a string that represents the digits to print */ - public static void print(java.io.PrintStream s, String fmt, String x) { + public static void print(java.io.PrintStream s, String fmt, String x) + { s.print(new Format(fmt).form(x)); } @@ -250,7 +314,8 @@ public class Format { * @param s a string * @return the numeric value of the prefix of s representing a base 10 integer */ - public static int atoi(String s) { + public static int atoi(String s) + { return (int) atol(s); } @@ -259,25 +324,41 @@ public class Format { * @param s a string * @return the numeric value of the prefix of s representing a base 10 integer */ - public static long atol(String s) { + public static long atol(String s) + { int i = 0; while ((i < s.length()) && Character.isWhitespace(s.charAt(i))) i++; - if ((i < s.length()) && (s.charAt(i) == '0')) { + if ((i < s.length()) && (s.charAt(i) == '0')) + { if (((i + 1) < s.length()) && - ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X'))) { + ((s.charAt(i + 1) == 'x') || (s.charAt(i + 1) == 'X'))) + { return parseLong(s.substring(i + 2), 16); - } else { + } + else + { return parseLong(s, 8); } - } else { + } + else + { return parseLong(s, 10); } } - private static long parseLong(String s, int base) { + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param base DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private static long parseLong(String s, int base) + { int i = 0; int sign = 1; long r = 0; @@ -285,23 +366,34 @@ public class Format { while ((i < s.length()) && Character.isWhitespace(s.charAt(i))) i++; - if ((i < s.length()) && (s.charAt(i) == '-')) { + if ((i < s.length()) && (s.charAt(i) == '-')) + { sign = -1; i++; - } else if ((i < s.length()) && (s.charAt(i) == '+')) { + } + else if ((i < s.length()) && (s.charAt(i) == '+')) + { i++; } - while (i < s.length()) { + while (i < s.length()) + { char ch = s.charAt(i); - if (('0' <= ch) && (ch < ('0' + base))) { + if (('0' <= ch) && (ch < ('0' + base))) + { r = ((r * base) + ch) - '0'; - } else if (('A' <= ch) && (ch < (('A' + base) - 10))) { + } + else if (('A' <= ch) && (ch < (('A' + base) - 10))) + { r = ((r * base) + ch) - 'A' + 10; - } else if (('a' <= ch) && (ch < (('a' + base) - 10))) { + } + else if (('a' <= ch) && (ch < (('a' + base) - 10))) + { r = ((r * base) + ch) - 'a' + 10; - } else { + } + else + { return r * sign; } @@ -315,45 +407,63 @@ public class Format { * Converts a string of digits to an double * @param s a string */ - public static double atof(String s) { + public static double atof(String s) + { int i = 0; int sign = 1; double r = 0; // integer part - double f = 0; // fractional part + //double f = 0; // fractional part double p = 1; // exponent of fractional part int state = 0; // 0 = int part, 1 = frac part while ((i < s.length()) && Character.isWhitespace(s.charAt(i))) i++; - if ((i < s.length()) && (s.charAt(i) == '-')) { + if ((i < s.length()) && (s.charAt(i) == '-')) + { sign = -1; i++; - } else if ((i < s.length()) && (s.charAt(i) == '+')) { + } + else if ((i < s.length()) && (s.charAt(i) == '+')) + { i++; } - while (i < s.length()) { + while (i < s.length()) + { char ch = s.charAt(i); - if (('0' <= ch) && (ch <= '9')) { - if (state == 0) { + if (('0' <= ch) && (ch <= '9')) + { + if (state == 0) + { r = ((r * 10) + ch) - '0'; - } else if (state == 1) { + } + else if (state == 1) + { p = p / 10; r = r + (p * (ch - '0')); } - } else if (ch == '.') { - if (state == 0) { + } + else if (ch == '.') + { + if (state == 0) + { state = 1; - } else { + } + else + { return sign * r; } - } else if ((ch == 'e') || (ch == 'E')) { + } + else if ((ch == 'e') || (ch == 'E')) + { long e = (int) parseLong(s.substring(i + 1), 10); return sign * r * Math.pow(10, e); - } else { + } + else + { return sign * r; } @@ -369,26 +479,33 @@ public class Format { * @return the formatted string * @exception IllegalArgumentException if bad argument */ - public String form(double x) { + public String form(double x) + { String r; - if (precision < 0) { + if (precision < 0) + { precision = 6; } int s = 1; - if (x < 0) { + if (x < 0) + { x = -x; s = -1; } - if (fmt == 'f') { + if (fmt == 'f') + { r = fixed_format(x); - } else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || - (fmt == 'G')) { + } + else if ((fmt == 'e') || (fmt == 'E') || (fmt == 'g') || (fmt == 'G')) + { r = exp_format(x); - } else { + } + else + { throw new java.lang.IllegalArgumentException(); } @@ -400,25 +517,38 @@ public class Format { * @param x the number to format * @return the formatted string */ - public String form(long x) { + public String form(long x) + { String r; int s = 0; - if ((fmt == 'd') || (fmt == 'i')) { - if (x < 0) { + if ((fmt == 'd') || (fmt == 'i')) + { + if (x < 0) + { r = ("" + x).substring(1); s = -1; - } else { + } + else + { r = "" + x; s = 1; } - } else if (fmt == 'o') { + } + else if (fmt == 'o') + { r = convert(x, 3, 7, "01234567"); - } else if (fmt == 'x') { + } + else if (fmt == 'x') + { r = convert(x, 4, 15, "0123456789abcdef"); - } else if (fmt == 'X') { + } + else if (fmt == 'X') + { r = convert(x, 4, 15, "0123456789ABCDEF"); - } else { + } + else + { throw new java.lang.IllegalArgumentException(); } @@ -430,8 +560,10 @@ public class Format { * @param x the value to format * @return the formatted string */ - public String form(char c) { - if (fmt != 'c') { + public String form(char c) + { + if (fmt != 'c') + { throw new java.lang.IllegalArgumentException(); } @@ -445,12 +577,15 @@ public class Format { * @param x the value to format * @return the formatted string */ - public String form(String s) { - if (fmt != 's') { + public String form(String s) + { + if (fmt != 's') + { throw new java.lang.IllegalArgumentException(); } - if (precision >= 0) { + if (precision >= 0) + { s = s.substring(0, precision); } @@ -460,7 +595,8 @@ public class Format { /** * a test stub for the format class */ - public static void main(String[] a) { + public static void main(String[] a) + { double x = 1.23456789012; double y = 123; double z = 1.2345e30; @@ -530,8 +666,18 @@ public class Format { Format.print(System.out, "|%6.0f|\n", 9.999); } - private static String repeat(char c, int n) { - if (n <= 0) { + /** + * DOCUMENT ME! + * + * @param c DOCUMENT ME! + * @param n DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private static String repeat(char c, int n) + { + if (n <= 0) + { return ""; } @@ -543,14 +689,27 @@ public class Format { return s.toString(); } - private static String convert(long x, int n, int m, String d) { - if (x == 0) { + /** + * DOCUMENT ME! + * + * @param x DOCUMENT ME! + * @param n DOCUMENT ME! + * @param m DOCUMENT ME! + * @param d DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private static String convert(long x, int n, int m, String d) + { + if (x == 0) + { return "0"; } String r = ""; - while (x != 0) { + while (x != 0) + { r = d.charAt((int) (x & m)) + r; x = x >>> n; } @@ -558,66 +717,113 @@ public class Format { return r; } - private String pad(String r) { + /** + * DOCUMENT ME! + * + * @param r DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private String pad(String r) + { String p = repeat(' ', width - r.length()); - if (left_align) { + if (left_align) + { return pre + r + p + post; - } else { + } + else + { return pre + p + r + post; } } - private String sign(int s, String r) { + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param r DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private String sign(int s, String r) + { String p = ""; - if (s < 0) { + if (s < 0) + { p = "-"; - } else if (s > 0) { - if (show_plus) { + } + else if (s > 0) + { + if (show_plus) + { p = "+"; - } else if (show_space) { + } + else if (show_space) + { p = " "; } - } else { + } + else + { if ((fmt == 'o') && alternate && (r.length() > 0) && - (r.charAt(0) != '0')) { + (r.charAt(0) != '0')) + { p = "0"; - } else if ((fmt == 'x') && alternate) { + } + else if ((fmt == 'x') && alternate) + { p = "0x"; - } else if ((fmt == 'X') && alternate) { + } + else if ((fmt == 'X') && alternate) + { p = "0X"; } } int w = 0; - if (leading_zeroes) { + if (leading_zeroes) + { w = width; - } else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || - (fmt == 'X') || (fmt == 'o')) && (precision > 0)) { + } + else if (((fmt == 'd') || (fmt == 'i') || (fmt == 'x') || (fmt == 'X') || + (fmt == 'o')) && (precision > 0)) + { w = precision; } return p + repeat('0', w - p.length() - r.length()) + r; } - private String fixed_format(double d) { + /** + * DOCUMENT ME! + * + * @param d DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private String fixed_format(double d) + { boolean removeTrailing = ((fmt == 'G') || (fmt == 'g')) && !alternate; // remove trailing zeroes and decimal point - if (d > 0x7FFFFFFFFFFFFFFFL) { + if (d > 0x7FFFFFFFFFFFFFFFL) + { return exp_format(d); } - if (precision == 0) { + if (precision == 0) + { return (long) (d + 0.5) + (removeTrailing ? "" : "."); } long whole = (long) d; double fr = d - whole; // fractional part - if ((fr >= 1) || (fr < 0)) { + if ((fr >= 1) || (fr < 0)) + { return exp_format(d); } @@ -625,29 +831,33 @@ public class Format { String leading_zeroes = ""; for (int i = 1; (i <= precision) && (factor <= 0x7FFFFFFFFFFFFFFFL); - i++) { + i++) + { factor *= 10; leading_zeroes = leading_zeroes + "0"; } long l = (long) ((factor * fr) + 0.5); - if (l >= factor) { + if (l >= factor) + { l = 0; whole++; } - // CSH 10-25-97 + // CSH 10-25-97 String z = leading_zeroes + l; z = "." + z.substring(z.length() - precision, z.length()); - if (removeTrailing) { + if (removeTrailing) + { int t = z.length() - 1; while ((t >= 0) && (z.charAt(t) == '0')) t--; - if ((t >= 0) && (z.charAt(t) == '.')) { + if ((t >= 0) && (z.charAt(t) == '.')) + { t--; } @@ -657,45 +867,63 @@ public class Format { return whole + z; } - private String exp_format(double d) { + /** + * DOCUMENT ME! + * + * @param d DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + private String exp_format(double d) + { String f = ""; int e = 0; double dd = d; double factor = 1; - if (d != 0) { - while (dd > 10) { + if (d != 0) + { + while (dd > 10) + { e++; factor /= 10; dd = dd / 10; } - while (dd < 1) { + while (dd < 1) + { e--; factor *= 10; dd = dd * 10; } } - if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision)) { + if (((fmt == 'g') || (fmt == 'G')) && (e >= -4) && (e < precision)) + { return fixed_format(d); } d = d * factor; f = f + fixed_format(d); - if ((fmt == 'e') || (fmt == 'g')) { + if ((fmt == 'e') || (fmt == 'g')) + { f = f + "e"; - } else { + } + else + { f = f + "E"; } String p = "000"; - if (e >= 0) { + if (e >= 0) + { f = f + "+"; p = p + e; - } else { + } + else + { f = f + "-"; p = p + (-e); }