com.compomics.util
Class Util

java.lang.Object
  extended by com.compomics.util.Util

public class Util
extends Object

Includes general help methods that are used by the other classes.

Author:
Harald Barsnes, Marc Vaudel

Field Summary
static String[] forbiddenCharacters
          Forbidden characters in file names.
 
Constructor Summary
Util()
           
 
Method Summary
static String appendSuffix(String fileName, String suffix)
          Appends a suffix to a file name before the file extension.
static String color2Hex(Color color)
          Converts a color to hex format for use in HTML tags.
static boolean containsForbiddenCharacter(String string)
          Indicates whether a string contains characters forbidden in file names.
static void copyFile(File in, File out)
          Copy the content of one file to another.
static boolean deleteDir(File dir)
          Deletes all files and subdirectories under dir.
static String getExtension(File file)
          Returns the extensions of a file.
static String getFileName(File file)
          An OS independent getName alternative.
static String getFileName(String filePath)
          An OS independent getName alternative.
static ArrayList<Integer> getIndexes(String bigString, String smallString)
          Returns at which indexes a small string can be found in a big string.
static int getNumberOfLines(File file)
          Returns the number of lines in the given file.
static double getPpmError(double theoreticalMzValue, double massError)
          Returns the ppm value of the given mass error relative to its theoretical m/z value.
static File getUserSelectedFile(Component parent, String aFileEnding, String aFileFormatDescription, String aDialogTitle, String lastSelectedFolder, boolean openDialog)
          Returns the file selected by the user, or null if no file was selected.
static File getUserSelectedFolder(Component parent, String aDialogTitle, String lastSelectedFolder, String aFolderDescription, String approveButtonText, boolean openDialog)
          Returns the folder selected by the user, or null if no folder was selected.
static String removeExtension(String fileName)
          Removes the extension from a file name or path.
static String removeForbiddenCharacters(String string)
          Removes the forbidden characters from a string
static double roundDouble(double d, int places)
          Rounds a double value to the wanted number of decimal places.
static boolean sameLists(ArrayList<Integer> list1, ArrayList<Integer> list2)
          Convenience methods indicating whether the content of two lists have the same content.
static void tableToFile(JTable table, String separator, ProgressDialogX progressDialog, boolean removeHtml, BufferedWriter writer)
          Writes the table to a file as separated text.
static String tableToText(JTable table, String separator, ProgressDialogX progressDialog, boolean removeHtml)
          Returns the table as a separated text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

forbiddenCharacters

public static final String[] forbiddenCharacters
Forbidden characters in file names.

Constructor Detail

Util

public Util()
Method Detail

removeForbiddenCharacters

public static String removeForbiddenCharacters(String string)
Removes the forbidden characters from a string

Parameters:
string - the string of interest
Returns:
a version without forbidden characters

containsForbiddenCharacter

public static boolean containsForbiddenCharacter(String string)
Indicates whether a string contains characters forbidden in file names.

Parameters:
string - the string of interest
Returns:
a boolean indicating whether a string contains characters forbidden in file names

roundDouble

public static double roundDouble(double d,
                                 int places)
Rounds a double value to the wanted number of decimal places.

Parameters:
d - the double to round of
places - number of decimal places wanted
Returns:
double - the new double

deleteDir

public static boolean deleteDir(File dir)
Deletes all files and subdirectories under dir. Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false.

Parameters:
dir -
Returns:
rue if all deletions were successful

getPpmError

public static double getPpmError(double theoreticalMzValue,
                                 double massError)
Returns the ppm value of the given mass error relative to its theoretical m/z value.

Parameters:
theoreticalMzValue - the theoretical mass
massError - the mass error
Returns:
the mass error as a ppm value relative to the theoretical mass

color2Hex

public static String color2Hex(Color color)
Converts a color to hex format for use in HTML tags.

Parameters:
color - the color to convert
Returns:
the color in hex format

getFileName

public static String getFileName(String filePath)
An OS independent getName alternative. Useful if the path is provided as a hardcoded string and opened in a different OS.

Parameters:
filePath - the file path as a string
Returns:
the file name, or the complete path of no file name is detected

getFileName

public static String getFileName(File file)
An OS independent getName alternative. Useful if the path is provided as a hardcoded string and opened in a different OS.

Parameters:
file - the file
Returns:
the file name, or the complete path of no file name is detected

getExtension

public static String getExtension(File file)
Returns the extensions of a file.

Parameters:
file - the file
Returns:
the extension of a file

appendSuffix

public static String appendSuffix(String fileName,
                                  String suffix)
Appends a suffix to a file name before the file extension.

Parameters:
fileName - the file name
suffix - the suffix to add
Returns:
the file name with suffix

removeExtension

public static String removeExtension(String fileName)
Removes the extension from a file name or path.

Parameters:
fileName - the file name
Returns:
the file name without extension

getUserSelectedFile

public static File getUserSelectedFile(Component parent,
                                       String aFileEnding,
                                       String aFileFormatDescription,
                                       String aDialogTitle,
                                       String lastSelectedFolder,
                                       boolean openDialog)
Returns the file selected by the user, or null if no file was selected. Note that the last selected folder value is not updated during this method, and the code calling this method therefore has to take care of this if wanted.

Parameters:
parent - the parent dialog or frame
aFileEnding - the file type, e.g., .txt
aFileFormatDescription - the file format description, e.g., (Mascot Generic Format) *.mgf
aDialogTitle - the title for the dialog
lastSelectedFolder - the last selected folder
openDialog - if true an open dialog is shown, false results in a save dialog
Returns:
the file selected by the user, or null if no file was selected

getUserSelectedFolder

public static File getUserSelectedFolder(Component parent,
                                         String aDialogTitle,
                                         String lastSelectedFolder,
                                         String aFolderDescription,
                                         String approveButtonText,
                                         boolean openDialog)
Returns the folder selected by the user, or null if no folder was selected. Note that the last selected folder value is not updated during this method, and the code calling this method therefore has to take care of this if wanted.

Parameters:
parent - the parent dialog or frame
aDialogTitle - the title for the dialog
lastSelectedFolder - the last selected folder
aFolderDescription - the folder description, e.g., CPS Folder
approveButtonText - the text on the approve button
openDialog - if true the folder has to exist, if false the user will be asked if he/she wants to create the folder is missing
Returns:
the file selected by the user, or null if no file was selected

tableToText

public static String tableToText(JTable table,
                                 String separator,
                                 ProgressDialogX progressDialog,
                                 boolean removeHtml)
Returns the table as a separated text file.

Parameters:
table - the table to turn in to text
separator - the text separator
progressDialog - the progress dialog
removeHtml - if true, HTML is converted to text
Returns:
the table as a separated text file

tableToFile

public static void tableToFile(JTable table,
                               String separator,
                               ProgressDialogX progressDialog,
                               boolean removeHtml,
                               BufferedWriter writer)
                        throws IOException
Writes the table to a file as separated text.

Parameters:
table - the table to write to file
separator - the text separator
progressDialog - the progress dialog
removeHtml - if true, html is converted to text
writer - the writer where the file is to be written
Throws:
IOException

copyFile

public static void copyFile(File in,
                            File out)
                     throws IOException
Copy the content of one file to another.

Parameters:
in - the file to copy from
out - the file to copy to
Throws:
IOException

getNumberOfLines

public static int getNumberOfLines(File file)
                            throws IOException
Returns the number of lines in the given file.

Parameters:
file - the file to find the number of lines in
Returns:
the number of lines in the given file
Throws:
IOException

sameLists

public static boolean sameLists(ArrayList<Integer> list1,
                                ArrayList<Integer> list2)
Convenience methods indicating whether the content of two lists have the same content.

Parameters:
list1 - the first list
list2 - the second list
Returns:
a boolean indicating whether list1 has the same content as list2

getIndexes

public static ArrayList<Integer> getIndexes(String bigString,
                                            String smallString)
Returns at which indexes a small string can be found in a big string.

Parameters:
bigString - the big string
smallString - the small string
Returns:
a list of the indexes where the small string can be found in the big string


Copyright © 2013. All Rights Reserved.