X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FFormat.java;h=bf520272c05cd20416444adb946bb1ad280e578f;hb=d065bc916cb63af83cdab7319f5177a855724aba;hp=c6348b22dec48de17ebafa63339232dbcad238bd;hpb=153dd62dc91da13ae732600e6ea55ddbe15eab39;p=jalview.git diff --git a/src/jalview/util/Format.java b/src/jalview/util/Format.java index c6348b2..bf52027 100755 --- a/src/jalview/util/Format.java +++ b/src/jalview/util/Format.java @@ -1,19 +1,22 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6) - * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ /** * A class for formatting numbers that follows printf conventions. @@ -23,6 +26,8 @@ */ package jalview.util; +import java.util.Arrays; + /** * DOCUMENT ME! * @@ -51,6 +56,8 @@ public class Format private char fmt; // one of cdeEfgGiosxXos + private final String formatString; + /** * Creates a new Format object. * @@ -59,6 +66,7 @@ public class Format */ public Format(String s) { + formatString = s; width = 0; precision = -1; pre = ""; @@ -205,66 +213,63 @@ public class Format } } -/** + /** * Formats the number following printf conventions. Main limitation: Can only * handle one format parameter at a time Use multiple Format objects to format * more than one number * * @param s - * the format string following printf conventions The string - * has a prefix, a format code and a suffix. The prefix and - * suffix become part of the formatted output. The format code - * directs the formatting of the (single) parameter to be - * formatted. The code has the following structure - * + * the format string following printf conventions The string has a + * prefix, a format code and a suffix. The prefix and suffix become + * part of the formatted output. The format code directs the + * formatting of the (single) parameter to be formatted. The code has + * the following structure + * * @exception IllegalArgumentException - * if bad format + * if bad format * */ public static String getHexString(java.awt.Color color) @@ -622,7 +627,7 @@ public class Format /** * Formats a character into a string (like sprintf in C) * - * @param x + * @param debounceTrap * the value to format * @return the formatted string */ @@ -641,7 +646,7 @@ public class Format /** * Formats a string into a larger string (like sprintf in C) * - * @param x + * @param debounceTrap * the value to format * @return the formatted string */ @@ -661,30 +666,22 @@ public class Format } /** - * DOCUMENT ME! + * Returns a string consisting of n repeats of character c * * @param c - * DOCUMENT ME! * @param n - * DOCUMENT ME! * - * @return DOCUMENT ME! + * @return */ - private static String repeat(char c, int n) + static String repeat(char c, int n) { if (n <= 0) { return ""; } - - StringBuffer s = new StringBuffer(n); - - for (int i = 0; i < n; i++) - { - s.append(c); - } - - return s.toString(); + char[] chars = new char[n]; + Arrays.fill(chars, c); + return new String(chars); } /** @@ -887,21 +884,18 @@ public class Format String f = ""; int e = 0; double dd = d; - double factor = 1; if (d != 0) { while (dd > 10) { e++; - factor /= 10; dd = dd / 10; } while (dd < 1) { e--; - factor *= 10; dd = dd * 10; } } @@ -911,8 +905,7 @@ public class Format return fixed_format(d); } - d = d * factor; - f = f + fixed_format(d); + f = f + fixed_format(dd); if ((fmt == 'e') || (fmt == 'g')) { @@ -938,4 +931,55 @@ public class Format return f + p.substring(p.length() - 3, p.length()); } + + @Override + public String toString() + { + return formatString; + } + + /** + * Bespoke method to format percentage float value to the specified number of + * decimal places. Avoids use of general-purpose format parsers as a + * processing hotspot. + * + * @param sb + * @param value + * @param dp + */ + public static void appendPercentage(StringBuilder sb, float value, int dp) + { + /* + * rounding first + */ + double d = value; + long factor = 1L; + for (int i = 0; i < dp; i++) + { + factor *= 10; + } + d *= factor; + d += 0.5; + + /* + * integer part + */ + value = (float) (d / factor); + sb.append((long) value); + + /* + * decimal places + */ + if (dp > 0) + { + sb.append("."); + while (dp > 0) + { + value = value - (int) value; + value *= 10; + sb.append((int) value); + dp--; + } + } + } }