public class CommonUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
URL_PROTOCOL_FILE
URL protocol for a file in the file system: "file"
|
| Constructor and Description |
|---|
CommonUtils() |
| Modifier and Type | Method and Description |
|---|---|
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. |
static File |
getFile(URI resourceUri,
String description)
Resolve the given resource URI to a
java.io.File,
i.e. |
static File |
getFile(URL resourceUrl)
Resolve the given resource URL to a
java.io.File,
i.e. |
static File |
getFile(URL resourceUrl,
String description)
Resolve the given resource URL to a
java.io.File,
i.e. |
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 <T> List<List<T>> |
partitionList(List<T> originalList,
int partitionSize)
Partitions the list into smaller chunks
|
static String |
replace(String inString,
String oldPattern,
String newPattern)
Replace all occurences of a substring within a string with
another string.
|
static <K extends Comparable,V extends Comparable> |
sortByKeys(Map<K,V> map)
Paramterized method to sort Map e.g.
|
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.
|
public static final String URL_PROTOCOL_FILE
public static Double getRounded(double valueToRound, int numberOfDecimalPlaces)
valueToRound - numberOfDecimalPlaces - public static String createDefaultOutputFolder(String destinationFolder)
destinationFolder - public static String getCurrentFolder()
public static String createFolder(String absoluteLocation)
absoluteLocation - public static String getFormattedDate(Date date)
date - public static String getAbsoluteLocation(String location)
location - public static String cleanPath(String path)
The result is convenient for path comparison. For other uses, notice that Windows separators ("\") are replaced by simple slashes.
path - the original pathpublic static String collectionToDelimitedString(Collection coll, String delim)
toString() implementations.coll - the Collection to displaydelim - the delimiter to use (probably a ",")public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
toString() implementations.coll - the Collection to displaydelim - the delimiter to use (probably a ",")prefix - the String to start each element withsuffix - the String to end each element withpublic static String[] delimitedListToStringArray(String str, String delimiter)
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.
str - the input Stringdelimiter - the delimiter between elements (this is a single delimiter,
rather than a bunch individual delimiter characters)#tokenizeToStringArraypublic static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete)
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.
str - the input Stringdelimiter - 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.#tokenizeToStringArraypublic static String[] toStringArray(Collection collection)
collection - the Collection to copynull if the passed-in
Collection was null)public static String deleteAny(String inString, String charsToDelete)
inString - the original StringcharsToDelete - a set of characters to delete.
E.g. "az\n" will delete 'a's, 'z's and new lines.public static boolean hasLength(String str)
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
str - the String to check (may be null)true if the String is not null and has length#hasText(String)public static String replace(String inString, String oldPattern, String newPattern)
inString - String to examineoldPattern - String to replacenewPattern - String to insertpublic static File getFile(URI resourceUri) throws FileNotFoundException
java.io.File,
i.e. to a file in the file system.resourceUri - the resource URI to resolveFileNotFoundException - if the URL cannot be resolved to
a file in the file systempublic static File getFile(URI resourceUri, String description) throws FileNotFoundException
java.io.File,
i.e. to a file in the file system.resourceUri - the resource URI to resolvedescription - a description of the original resource that
the URI was created for (for example, a class path location)FileNotFoundException - if the URL cannot be resolved to
a file in the file systempublic static File getFile(URL resourceUrl) throws FileNotFoundException
java.io.File,
i.e. to a file in the file system.resourceUrl - the resource URL to resolveFileNotFoundException - if the URL cannot be resolved to
a file in the file systempublic static File getFile(URL resourceUrl, String description) throws FileNotFoundException
java.io.File,
i.e. to a file in the file system.resourceUrl - the resource URL to resolvedescription - a description of the original resource that
the URL was created for (for example, a class path location)FileNotFoundException - if the URL cannot be resolved to
a file in the file systempublic static URI toURI(URL url) throws URISyntaxException
Furthermore, this method works on JDK 1.4 as well,
in contrast to the URL.toURI() method.
url - the URL to convert into a URI instanceURISyntaxException - if the URL wasn't a valid URIURL.toURI()public static URI toURI(String location) throws URISyntaxException
location - the location String to convert into a URI instanceURISyntaxException - if the location wasn't a valid URIpublic static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map)
Copyright © 2014. All rights reserved.