org.easetech.easytest.util
Class CommonUtils

java.lang.Object
  extended by org.easetech.easytest.util.CommonUtils

public class CommonUtils
extends Object

This class contains common utils

Author:
gpcmol

Field Summary
static String URL_PROTOCOL_FILE
          URL protocol for a file in the file system: "file"
 
Constructor Summary
CommonUtils()
           
 
Method Summary
static String cleanPath(String path)
          Normalize the path by suppressing sequences like "path/.." and inner simple dots.
static String collectionToDelimitedString(Collection coll, String delim)
          Convenience method to return a Collection as a delimited (e.g.
static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
          Convenience method to return a Collection as a delimited (e.g.
static String createDefaultOutputFolder(String destinationFolder)
          Create directory
static String createFolder(String absoluteLocation)
          Creates directory.
static String deleteAny(String inString, String charsToDelete)
          Delete any character in a given String.
static String[] delimitedListToStringArray(String str, String delimiter)
          Take a String which is a delimited list and convert it to a String array.
static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
          Take a String which is a delimited list and convert it to a String array.
static String getAbsoluteLocation(String location)
          Returns the absolute path of either the classpath or file location
static String getCurrentFolder()
           
static File getFile(URI resourceUri)
          Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
static File getFile(URI resourceUri, String description)
          Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
static File getFile(URL resourceUrl)
          Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
static File getFile(URL resourceUrl, String description)
          Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
static String getFormattedDate(Date date)
          Format date with pattern ddMMyyyyHHmmss
static Double getRounded(double valueToRound, int numberOfDecimalPlaces)
          Rounds a value with number of decimals
static boolean hasLength(String str)
          Check that the given String is neither null nor of length 0.
static String replace(String inString, String oldPattern, String newPattern)
          Replace all occurences of a substring within a string with another string.
static String[] toStringArray(Collection collection)
          Copy the given Collection into a String array.
static URI toURI(String location)
          Create a URI instance for the given location String, replacing spaces with "%20" quotes first.
static URI toURI(URL url)
          Create a URI instance for the given URL, replacing spaces with "%20" quotes first.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URL_PROTOCOL_FILE

public static final String URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file"

See Also:
Constant Field Values
Constructor Detail

CommonUtils

public CommonUtils()
Method Detail

getRounded

public static Double getRounded(double valueToRound,
                                int numberOfDecimalPlaces)
Rounds a value with number of decimals

Parameters:
valueToRound -
numberOfDecimalPlaces -
Returns:
rounded double

createDefaultOutputFolder

public static String createDefaultOutputFolder(String destinationFolder)
Create directory

Parameters:
destinationFolder -
Returns:
a string representing the path to the output folder.

getCurrentFolder

public static String getCurrentFolder()

createFolder

public static String createFolder(String absoluteLocation)
Creates directory. If absolute location is empty, pick current folder

Parameters:
absoluteLocation -
Returns:
directory

getFormattedDate

public static String getFormattedDate(Date date)
Format date with pattern ddMMyyyyHHmmss

Parameters:
date -
Returns:
string with formatted date

getAbsoluteLocation

public static String getAbsoluteLocation(String location)
Returns the absolute path of either the classpath or file location

Parameters:
location -
Returns:
absolute location

cleanPath

public static String cleanPath(String path)
Normalize the path by suppressing sequences like "path/.." and inner simple dots.

The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.

Parameters:
path - the original path
Returns:
the normalized path

collectionToDelimitedString

public static String collectionToDelimitedString(Collection coll,
                                                 String delim)
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.

Parameters:
coll - the Collection to display
delim - the delimiter to use (probably a ",")
Returns:
the delimited String

collectionToDelimitedString

public static String collectionToDelimitedString(Collection coll,
                                                 String delim,
                                                 String prefix,
                                                 String suffix)
Convenience method to return a Collection as a delimited (e.g. CSV) String. E.g. useful for toString() implementations.

Parameters:
coll - the Collection to display
delim - the delimiter to use (probably a ",")
prefix - the String to start each element with
suffix - the String to end each element with
Returns:
the delimited String

delimitedListToStringArray

public static String[] delimitedListToStringArray(String str,
                                                  String delimiter)
Take a String which is a delimited list and convert it to a String array.

A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

Parameters:
str - the input String
delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
Returns:
an array of the tokens in the list
See Also:
#tokenizeToStringArray

delimitedListToStringArray

public static String[] delimitedListToStringArray(String str,
                                                  String delimiter,
                                                  String charsToDelete)
Take a String which is a delimited list and convert it to a String array.

A single delimiter can consists of more than one character: It will still be considered as single delimiter string, rather than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

Parameters:
str - the input String
delimiter - the delimiter between elements (this is a single delimiter, rather than a bunch individual delimiter characters)
charsToDelete - a set of characters to delete. Useful for deleting unwanted line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String.
Returns:
an array of the tokens in the list
See Also:
#tokenizeToStringArray

toStringArray

public static String[] toStringArray(Collection collection)
Copy the given Collection into a String array. The Collection must contain String elements only.

Parameters:
collection - the Collection to copy
Returns:
the String array (null if the passed-in Collection was null)

deleteAny

public static String deleteAny(String inString,
                               String charsToDelete)
Delete any character in a given String.

Parameters:
inString - the original String
charsToDelete - a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.
Returns:
the resulting String

hasLength

public static boolean hasLength(String str)
Check that the given String is neither null nor of length 0. Note: Will return true for a String that purely consists of whitespace.

 StringUtils.hasLength(null) = false
 StringUtils.hasLength("") = false
 StringUtils.hasLength(" ") = true
 StringUtils.hasLength("Hello") = true
 

Parameters:
str - the String to check (may be null)
Returns:
true if the String is not null and has length
See Also:
#hasText(String)

replace

public static String replace(String inString,
                             String oldPattern,
                             String newPattern)
Replace all occurences of a substring within a string with another string.

Parameters:
inString - String to examine
oldPattern - String to replace
newPattern - String to insert
Returns:
a String with the replacements

getFile

public static File getFile(URI resourceUri)
                    throws FileNotFoundException
Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.

Parameters:
resourceUri - the resource URI to resolve
Returns:
a corresponding File object
Throws:
FileNotFoundException - if the URL cannot be resolved to a file in the file system

getFile

public static File getFile(URI resourceUri,
                           String description)
                    throws FileNotFoundException
Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.

Parameters:
resourceUri - the resource URI to resolve
description - a description of the original resource that the URI was created for (for example, a class path location)
Returns:
a corresponding File object
Throws:
FileNotFoundException - if the URL cannot be resolved to a file in the file system

getFile

public static File getFile(URL resourceUrl)
                    throws FileNotFoundException
Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.

Parameters:
resourceUrl - the resource URL to resolve
Returns:
a corresponding File object
Throws:
FileNotFoundException - if the URL cannot be resolved to a file in the file system

getFile

public static File getFile(URL resourceUrl,
                           String description)
                    throws FileNotFoundException
Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.

Parameters:
resourceUrl - the resource URL to resolve
description - a description of the original resource that the URL was created for (for example, a class path location)
Returns:
a corresponding File object
Throws:
FileNotFoundException - if the URL cannot be resolved to a file in the file system

toURI

public static URI toURI(URL url)
                 throws URISyntaxException
Create a URI instance for the given URL, replacing spaces with "%20" quotes first.

Furthermore, this method works on JDK 1.4 as well, in contrast to the URL.toURI() method.

Parameters:
url - the URL to convert into a URI instance
Returns:
the URI instance
Throws:
URISyntaxException - if the URL wasn't a valid URI
See Also:
URL.toURI()

toURI

public static URI toURI(String location)
                 throws URISyntaxException
Create a URI instance for the given location String, replacing spaces with "%20" quotes first.

Parameters:
location - the location String to convert into a URI instance
Returns:
the URI instance
Throws:
URISyntaxException - if the location wasn't a valid URI


Copyright © 2013. All Rights Reserved.