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