Class ZUtilities
public class ZUtilities extends Object
-
Method Summary
Modifier and Type Method Description static ArrayList<String>
arrayToList(String[] array)
Converts a simple array of strings toArrayList<String>
.static int
boolToInt(boolean b)
Returns1
if the given boolean value istrue
,0
if it isfalse
.static String
concatAll(String delimiter, Object... args)
Returns a concatenation of all the string representations of the given arguments with the given delimiter inserted between the concatenated strings.static String
concatAll(String delimiter, Collection c)
Returns a concatenation of all the elements of the given collection converted to strings, with the given delimiter inserted between the concatenated strings.static boolean
contains(String list, String delimiter, String element)
Split stringlist
by delimiterdelimiter
and check if the split list containselement
static void
deleteFolder(String path)
Deletes an entire folder with all sub-folders and contained files.static boolean
find(String string, String regex)
Returns true if the given string contains the given regular expression.static String
getFileExtension(String path)
Extracts the filename extension from a given file path.static File[]
getFolderList(String path)
Returns a list of all files in the given folder, sorted by the lexicographic order of their names, sub folders first.static String
getMyIpAddress()
Returns the external IP address of the calling application or"Unknown"
if the IP cannot be identified.static String[]
intsToStrings(int[] list)
Returns an array of strings representing the numbers contained in the given array of integer numbers.static boolean
isFolderEmpty(Path path)
Check if the given path is of an empty folderstatic boolean
parseBoolean(String s)
Returns the boolean value that the given string represents.static double
parseDouble(String s)
Returns the given string parsed into a double float number, or zero if the string does not represent a valid float number.static float
parseFloat(String s)
Returns the given string parsed into a float number, or zero if the string does not represent a valid float number.static float
parseFloat(String s, float def)
Returns the given string parsed into a float number, or the given default value if the string does not represent a valid float number.static int
parseInt(String s)
Returns the given string parsed into an integer, or zero if the string does not represent a valid integer number.static int
parseInt(String s, int def)
Returns the given string parsed into an integer, or the given default value if the string does not represent a valid integer number.static long
parseLong(String s)
Returns the given string parsed into a long integer, or zero if the string does not represent a valid integer number.static long
parseLong(String s, long def)
Returns the given string parsed into a long integer, or the given default value if the string does not represent a valid integer number.static String
replace(String source, String... pairs)
Replaces a series of substrings of given string by a series of replacement strings.static Set<String>
splitIntoSet(String list, String delimiter)
static String
stackTraceToString(Throwable ex)
Returns the stack trace of the given exception object as a string.static int[]
stringsToInts(String[] list)
Returns an array of integer numbers parsed from the given array of strings.
-
Method Details
-
parseInt
Returns the given string parsed into an integer, or zero if the string does not represent a valid integer number.- Parameters:
s
- the string to parse.- Returns:
- the given string parsed into an integer, or zero if the string does not represent a valid integer number.
-
parseInt
Returns the given string parsed into an integer, or the given default value if the string does not represent a valid integer number.- Parameters:
s
- the string to parse.def
- the default value.- Returns:
- the given string parsed into an integer, or the given default value if the string does not represent a valid integer number.
-
parseBoolean
Returns the boolean value that the given string represents.A string represents the boolean value
true
if it is either "1" or "true" (case insensitive), otherwise it representsfalse
.- Parameters:
s
- the string to interpret.- Returns:
- the boolean value that the given string represents.
-
boolToInt
public static int boolToInt(boolean b)Returns1
if the given boolean value istrue
,0
if it isfalse
.- Parameters:
b
- the value to convert.- Returns:
- 1 if the given value is true, 0 if it is false.
-
parseLong
Returns the given string parsed into a long integer, or zero if the string does not represent a valid integer number.- Parameters:
s
- the string to parse.- Returns:
- the given string parsed into a long integer, or zero if the string does not represent a valid integer number.
-
parseLong
Returns the given string parsed into a long integer, or the given default value if the string does not represent a valid integer number.- Parameters:
s
- the string to parse.def
- the default value.- Returns:
- the given string parsed into a long integer, or the given default value if the string does not represent a valid integer number.
-
parseFloat
Returns the given string parsed into a float number, or zero if the string does not represent a valid float number.- Parameters:
s
- the string to parse.- Returns:
- the given string parsed into a float number, or zero if the string does not represent a valid float number.
-
parseFloat
Returns the given string parsed into a float number, or the given default value if the string does not represent a valid float number.- Parameters:
s
- the string to parse.def
- the default value.- Returns:
- the given string parsed into a float number, or the given default value if the string does not represent a valid float number.
-
parseDouble
Returns the given string parsed into a double float number, or zero if the string does not represent a valid float number.- Parameters:
s
- the string to parse.- Returns:
- the given string parsed into a double float number, or zero if the string does not represent a valid float number.
-
concatAll
Returns a concatenation of all the string representations of the given arguments with the given delimiter inserted between the concatenated strings.The given arguments may include
null
values. Non-trailing nulls are treated as empty strings, while trailing nulls are ignored.- Parameters:
delimiter
- the delimiter to use for concatenation.args
- the arguments to concatenate.- Returns:
- a concatenation of the given arguments with the given delimiter inserted between the concatenated strings.
-
concatAll
Returns a concatenation of all the elements of the given collection converted to strings, with the given delimiter inserted between the concatenated strings.The collection elements may include
null
values. Non-trailing nulls are treated as empty strings, while trailing nulls are ignored.- Parameters:
delimiter
- the delimiter to use for concatenation.c
- the collection- Returns:
- the elements of the given collection concatenated by the given delimiter into one string.
-
intsToStrings
Returns an array of strings representing the numbers contained in the given array of integer numbers.- Parameters:
list
- the integer array.- Returns:
- an array of strings representing the numbers contained in the given array of integer numbers.
-
stringsToInts
Returns an array of integer numbers parsed from the given array of strings.If any string in the given string array does not contain a valid number the corresponding integer array element will contain zero.
- Parameters:
list
- the string array.- Returns:
- an array of integer numbers parsed from the given array of strings.
-
arrayToList
Converts a simple array of strings toArrayList<String>
.- Parameters:
array
- the array to convert.- Returns:
- the given array as an ArrayList<String> object, or an empty list if the given array is null.
-
stackTraceToString
Returns the stack trace of the given exception object as a string.- Parameters:
ex
- the Exception object.- Returns:
- the stack trace of the given exception object as a string.
-
find
Returns true if the given string contains the given regular expression.- Parameters:
string
- the string to search in. If null the method returns false;regex
- the regular expression to look for. If null the method returns false.- Returns:
- true if and only if the given string contains the given regular expression.
-
replace
Replaces a series of substrings of given string by a series of replacement strings.The
pairs
argument list must be of even length. It is considered a series of pairs, where the first element in each pair is a regular expression that may match a substring within thesource
parameter. The second element in each pair is the value by which the substring matched by the first element of the pair, if exists, shall be replaced.Note that the replacement action is recursive, i.e. each search for a replacement candidate is done over the string yielded from the previous replacement.
- Parameters:
source
- The source stringpairs
- A series of pairs regular expression - replacement.- Returns:
- the result string
-
deleteFolder
Deletes an entire folder with all sub-folders and contained files.- Parameters:
path
- the pathname of the folder to delete.- Throws:
IOException
-
getFolderList
Returns a list of all files in the given folder, sorted by the lexicographic order of their names, sub folders first.- Parameters:
path
- the folder pathname.- Returns:
- a list of all files in the folder, sorted by the lexicographic order of their names, sub folders first.
-
getMyIpAddress
Returns the external IP address of the calling application or"Unknown"
if the IP cannot be identified.- Returns:
- the external IP address of the calling application or
"Unknown"
if the IP cannot be identified.
-
getFileExtension
Extracts the filename extension from a given file path.If the path does not have extension at all, i.e. the simple file name does not contain the period (.) character, the method returns
null
. Otherwise it returns the extension, which may be an empty string if the file name ends with a period.- Parameters:
path
- The file path.- Returns:
- filename extension or null if there is no extension.
-
splitIntoSet
- Parameters:
list
- the list to splitdelimiter
- the delimiter to split by- Returns:
- s split by delimiter as a Set
-
contains
Split stringlist
by delimiterdelimiter
and check if the split list containselement
- Parameters:
list
- the list to splitdelimiter
- the delimiter to split byelement
- the element to look for- Returns:
- true if and only if element is contained in list
-
isFolderEmpty
Check if the given path is of an empty folder- Parameters:
path
-- Returns:
- true if and only if the given path is of an empty folder
- Throws:
IOException
-