X-Git-Url: http://source.jalview.org/gitweb/?p=vamsas.git;a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fobjects%2Futils%2FFormat.java;h=f67f698b57135d4af7789f456d62fb4f769c6e44;hp=b4bd72277f5c52af24bdcb9c1239eb58ca9395fd;hb=844ccad5a3fcbedec17b2af66d460f31abc7cff1;hpb=6f33f705957d674dc2ab6c994a6ea87f7a91f40f diff --git a/src/uk/ac/vamsas/objects/utils/Format.java b/src/uk/ac/vamsas/objects/utils/Format.java index b4bd722..f67f698 100644 --- a/src/uk/ac/vamsas/objects/utils/Format.java +++ b/src/uk/ac/vamsas/objects/utils/Format.java @@ -1,24 +1,24 @@ /* - * Cay S. Horstmann & Gary Cornell, Core Java - * Published By Sun Microsystems Press/Prentice-Hall - * Copyright (C) 1997 Sun Microsystems Inc. - * All Rights Reserved. - * - * Permission to use, copy, modify, and distribute this - * software and its documentation for NON-COMMERCIAL purposes - * and without fee is hereby granted provided that this - * copyright notice appears in all copies. + * This file is part of the Vamsas Client version 0.1. + * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, + * Andrew Waterhouse and Dominik Lindner. * - * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR - * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE - * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS - * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED - * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING - * THIS SOFTWARE OR ITS DERIVATIVES. + * Earlier versions have also been incorporated into Jalview version 2.4 + * since 2008, and TOPALi version 2 since 2007. + * + * The Vamsas Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Vamsas Client 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the Vamsas Client. If not, see . */ - /** * A class for formatting numbers that follows printf conventions. * Also implements C-like atoi and atof functions @@ -30,7 +30,8 @@ package uk.ac.vamsas.objects.utils; import java.io.*; -public class Format { /** +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 @@ -154,62 +155,81 @@ public class Format { /** } /** - * prints a formatted number following printf conventions - * @param s a PrintStream - * @param fmt the format string - * @param x the double to print - */ + * prints a formatted number following printf conventions + * + * @param s + * a PrintStream + * @param fmt + * the format string + * @param x + * the double to print + */ public static void print(java.io.PrintStream s, String fmt, double x) { s.print(new Format(fmt).form(x)); } /** - * prints a formatted number following printf conventions - * @param s a PrintStream - * @param fmt the format string - * @param x the long to print - */ + * prints a formatted number following printf conventions + * + * @param s + * a PrintStream + * @param fmt + * the format string + * @param x + * the long to print + */ public static void print(java.io.PrintStream s, String fmt, long x) { s.print(new Format(fmt).form(x)); } /** - * prints a formatted number following printf conventions - * @param s a PrintStream - * @param fmt the format string - * @param x the character to - */ + * prints a formatted number following printf conventions + * + * @param s + * a PrintStream + * @param fmt + * the format string + * @param x + * the character to + */ public static void print(java.io.PrintStream s, String fmt, char x) { s.print(new Format(fmt).form(x)); } /** - * prints a formatted number following printf conventions - * @param s a PrintStream, fmt the format string - * @param x a string that represents the digits to print - */ + * prints a formatted number following printf conventions + * + * @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) { s.print(new Format(fmt).form(x)); } /** - * Converts a string of digits (decimal, octal or hex) to an integer - * @param s a string - * @return the numeric value of the prefix of s representing a base 10 integer - */ + * Converts a string of digits (decimal, octal or hex) to an integer + * + * @param s + * a string + * @return the numeric value of the prefix of s representing a base 10 integer + */ public static int atoi(String s) { - return (int)atol(s); + return (int) atol(s); } /** - * Converts a string of digits (decimal, octal or hex) to a long integer - * @param s a string - * @return the numeric value of the prefix of s representing a base 10 integer - */ + * Converts a string of digits (decimal, octal or hex) to a long integer + * + * @param s + * a string + * @return the numeric value of the prefix of s representing a base 10 integer + */ public static long atol(String s) { int i = 0; @@ -217,7 +237,8 @@ public class Format { /** while (i < s.length() && Character.isWhitespace(s.charAt(i))) i++; if (i < s.length() && s.charAt(i) == '0') { - if (i + 1 < s.length() && (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X')) + if (i + 1 < s.length() + && (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X')) return parseLong(s.substring(i + 2), 16); else return parseLong(s, 8); @@ -243,9 +264,9 @@ public class Format { /** if ('0' <= ch && ch < '0' + base) r = r * base + ch - '0'; else if ('A' <= ch && ch < 'A' + base - 10) - r = r * base + ch - 'A' + 10 ; + r = r * base + ch - 'A' + 10; else if ('a' <= ch && ch < 'a' + base - 10) - r = r * base + ch - 'a' + 10 ; + r = r * base + ch - 'a' + 10; else return r * sign; i++; @@ -254,9 +275,11 @@ public class Format { /** } /** - * Converts a string of digits to an double - * @param s a string - */ + * Converts a string of digits to an double + * + * @param s + * a string + */ public static double atof(String s) { int i = 0; @@ -289,7 +312,7 @@ public class Format { /** else return sign * r; } else if (ch == 'e' || ch == 'E') { - long e = (int)parseLong(s.substring(i + 1), 10); + long e = (int) parseLong(s.substring(i + 1), 10); return sign * r * Math.pow(10, e); } else return sign * r; @@ -299,11 +322,14 @@ public class Format { /** } /** - * Formats a double into a string (like sprintf in C) - * @param x the number to format - * @return the formatted string - * @exception IllegalArgumentException if bad argument - */ + * Formats a double into a string (like sprintf in C) + * + * @param x + * the number to format + * @return the formatted string + * @exception IllegalArgumentException + * if bad argument + */ public String form(double x) { String r; @@ -325,10 +351,12 @@ public class Format { /** } /** - * Formats a long integer into a string (like sprintf in C) - * @param x the number to format - * @return the formatted string - */ + * Formats a long integer into a string (like sprintf in C) + * + * @param x + * the number to format + * @return the formatted string + */ public String form(long x) { String r; @@ -354,10 +382,12 @@ public class Format { /** } /** - * Formats a character into a string (like sprintf in C) - * @param x the value to format - * @return the formatted string - */ + * Formats a character into a string (like sprintf in C) + * + * @param x + * the value to format + * @return the formatted string + */ public String form(char c) { if (fmt != 'c') @@ -368,10 +398,12 @@ public class Format { /** } /** - * Formats a string into a larger string (like sprintf in C) - * @param x the value to format - * @return the formatted string - */ + * Formats a string into a larger string (like sprintf in C) + * + * @param x + * the value to format + * @return the formatted string + */ public String form(String s) { if (fmt != 's') @@ -381,10 +413,9 @@ public class Format { /** return pad(s); } - /** - * a test stub for the format class - */ + * a test stub for the format class + */ public static void main(String[] a) { double x = 1.23456789012; @@ -471,7 +502,7 @@ public class Format { /** return "0"; String r = ""; while (x != 0) { - r = d.charAt((int)(x & m)) + r; + r = d.charAt((int) (x & m)) + r; x = x >>> n; } return r; @@ -506,23 +537,22 @@ public class Format { /** if (leading_zeroes) w = width; else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o') - && precision > 0) + && precision > 0) w = precision; return p + repeat('0', w - p.length() - r.length()) + r; } private String fixed_format(double d) { - boolean removeTrailing - = (fmt == 'G' || fmt == 'g') && !alternate; + boolean removeTrailing = (fmt == 'G' || fmt == 'g') && !alternate; // remove trailing zeroes and decimal point if (d > 0x7FFFFFFFFFFFFFFFL) return exp_format(d); if (precision == 0) - return (long)(d + 0.5) + (removeTrailing ? "" : "."); + return (long) (d + 0.5) + (removeTrailing ? "" : "."); - long whole = (long)d; + long whole = (long) d; double fr = d - whole; // fractional part if (fr >= 1 || fr < 0) return exp_format(d); @@ -595,18 +625,22 @@ public class Format { /** } private int width; + private int precision; + private String pre; + private String post; + private boolean leading_zeroes; + private boolean show_plus; + private boolean alternate; + private boolean show_space; + private boolean left_align; + private char fmt; // one of cdeEfgGiosxXos } - - - - -