Back to index

wims  3.65+svn20090927
Util.java
Go to the documentation of this file.
00001 /*
00002     Sketch Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package SketchEl;
00012 
00013 import java.util.*;
00014 import java.text.*;
00015 import java.lang.*;
00016 import javax.swing.*;
00017 
00018 // A very generic class containing static utility functions, which are painfully absent from the Java libraries.
00019 
00020 public class Util
00021 {
00022     // pure convenience
00023     public static void write(String S) {System.out.print(S);}
00024     public static void writeln(String S) {System.out.println(S);}
00025 
00026     public static void errmsg(String Title,String Text)
00027     {
00028        JOptionPane.showMessageDialog(null,Text,Title,JOptionPane.ERROR_MESSAGE);
00029     }
00030     
00031     public static String arrayStr(int[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
00032     public static String arrayStr(double[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
00033     public static String arrayStr(String[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
00034     public static String arrayStr(boolean[] A) {String str=""; for (int n=0;n<A.length;n++) str+=(n>0 ? " " : "")+A[n]; return str;}
00035     
00036     // parse number-from-string functions which return a default value if it's badly formatted, instead of throwing an exception
00037     public static int safeInt(String S,int Def) 
00038     {
00039        if (S==null) return Def;
00040        try {return new Integer(S).intValue();} 
00041        catch (NumberFormatException e) {return Def;}
00042     }
00043     public static int safeInt(String S) {return safeInt(S,0);}
00044     public static double safeDouble(String S,double Def) 
00045     {
00046        if (S==null) return Def;
00047        try {return new Double(S).doubleValue();} 
00048        catch (NumberFormatException e) {return Def;}
00049     }
00050     public static double safeDouble(String S) {return safeDouble(S,0);}
00051     
00052     public static int iround(double V) {return (int)Math.round(V);}
00053     
00054     public static double sqr(double V) {return V*V;}
00055     public static double norm2(double x,double y) {return x*x+y*y;}
00056     public static double norm2(double x,double y,double z) {return x*x+y*y+z*z;}
00057     public static double norm(double x,double y) {return Math.sqrt(x*x+y*y);}
00058     public static double norm(double x,double y,double z) {return Math.sqrt(x*x+y*y+z*z);}
00059     
00060     // returns {Theta1}-{Theta2}, where both are in radians; the result is corrected to be between -PI and +PI
00061     public static double angleDiff(double Th1,double Th2)
00062     {
00063        double theta=Th1-Th2;
00064        return theta-(theta>Math.PI ? 2*Math.PI : 0)+(theta<-Math.PI ? 2*Math.PI : 0);
00065     }
00066     
00067     // equality of real numbers, which are not significantly less than 1
00068     public static boolean dblEqual(double d1,double d2) {return Math.abs(d1-d2)<1E-10;}
00069 
00070     // integer colour to HTML-style hex colour
00071     public static String colourHTML(int col)
00072     {
00073        String str=Integer.toHexString(col);
00074        while (str.length()<6) str="0"+str;
00075        return "#"+str;
00076     }
00077 }