Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     Sketch Elements: Chemistry molecular diagram drawing tool.
  3.    
  4.     (c) 2005 Dr. Alex M. Clark
  5.    
  6.     Released as GNUware, under the Gnu Public License (GPL)
  7.    
  8.     See www.gnu.org for details.
  9. */
  10.  
  11. package WIMSchem;
  12.  
  13. import java.util.*;
  14. import java.text.*;
  15. import java.lang.*;
  16. import javax.swing.*;
  17.  
  18. // A very generic class containing static utility functions, which are painfully absent from the Java libraries.
  19.  
  20. public class Util
  21. {
  22.     // pure convenience
  23.     public static void write(String S) {System.out.print(S);}
  24.     public static void writeln(String S) {System.out.println(S);}
  25.  
  26.     public static void errmsg(String Title,String Text)
  27.     {
  28.         JOptionPane.showMessageDialog(null,Text,Title,JOptionPane.ERROR_MESSAGE);
  29.     }
  30.    
  31.     public static String arrayStr(int[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
  32.     public static String arrayStr(double[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
  33.     public static String arrayStr(String[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
  34.     public static String arrayStr(boolean[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
  35.    
  36.     // parse number-from-string functions which return a default value if it's badly formatted, instead of throwing an exception
  37.     public static int safeInt(String S,int Def)
  38.     {
  39.         if (S==null) return Def;
  40.         try {return new Integer(S).intValue();}
  41.         catch (NumberFormatException e) {return Def;}
  42.     }
  43.     public static int safeInt(String S) {return safeInt(S,0);}
  44.     public static double safeDouble(String S,double Def)
  45.     {
  46.         if (S==null) return Def;
  47.         try {return new Double(S).doubleValue();}
  48.         catch (NumberFormatException e) {return Def;}
  49.     }
  50.     public static double safeDouble(String S) {return safeDouble(S,0);}
  51.    
  52.     public static int iround(double V) {return (int)Math.round(V);}
  53.    
  54.     public static double sqr(double V) {return V*V;}
  55.     public static double norm2(double x,double y) {return x*x+y*y;}
  56.     public static double norm2(double x,double y,double z) {return x*x+y*y+z*z;}
  57.     public static double norm(double x,double y) {return Math.sqrt(x*x+y*y);}
  58.     public static double norm(double x,double y,double z) {return Math.sqrt(x*x+y*y+z*z);}
  59.    
  60.     // returns {Theta1}-{Theta2}, where both are in radians; the result is corrected to be between -PI and +PI
  61.     public static double angleDiff(double Th1,double Th2)
  62.     {
  63.         double theta=Th1-Th2;
  64.         return theta-(theta>Math.PI ? 2*Math.PI : 0)+(theta<-Math.PI ? 2*Math.PI : 0);
  65.     }
  66.    
  67.     // equality of real numbers, which are not significantly less than 1
  68.     public static boolean dblEqual(double d1,double d2) {return Math.abs(d1-d2)<1E-10;}
  69.  
  70.     // integer colour to HTML-style hex colour
  71.     public static String colourHTML(int col)
  72.     {
  73.         String str=Integer.toHexString(col);
  74.         while (str.length()<6) str="0"+str;
  75.         return "#"+str;
  76.     }
  77. }
  78.