Class ZUtilities

java.lang.Object
org.spiderwiz.zutils.ZUtilities

public class ZUtilities
extends Object
Contains various static utility methods.
  • Method Summary

    Modifier and Type Method Description
    static ArrayList<String> arrayToList​(String[] array)
    Converts a simple array of strings to ArrayList<String>.
    static int boolToInt​(boolean b)
    Returns 1 if the given boolean value is true, 0 if it is false.
    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 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 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 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • parseInt

      public 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.
      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

      public 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.
      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

      public static boolean parseBoolean​(String s)
      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 represents false.

      Parameters:
      s - the string to interpret.
      Returns:
      the boolean value that the given string represents.
    • boolToInt

      public static int boolToInt​(boolean b)
      Returns 1 if the given boolean value is true, 0 if it is false.
      Parameters:
      b - the value to convert.
      Returns:
      1 if the given value is true, 0 if it is false.
    • parseLong

      public 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.
      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

      public 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.
      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

      public 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.
      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

      public 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.
      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

      public 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.
      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

      public 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.

      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

      public 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.

      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

      public static String[] intsToStrings​(int[] list)
      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

      public static int[] stringsToInts​(String[] list)
      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

      public static ArrayList<String> arrayToList​(String[] array)
      Converts a simple array of strings to ArrayList<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

      public static String stackTraceToString​(Throwable ex)
      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

      public static boolean find​(String string, String regex)
      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

      public static String replace​(String source, String... pairs)
      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 the source 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 string
      pairs - A series of pairs regular expression - replacement.
      Returns:
      the result string
    • deleteFolder

      public static void deleteFolder​(String path) throws IOException
      Deletes an entire folder with all sub-folders and contained files.
      Parameters:
      path - the pathname of the folder to delete.
      Throws:
      IOException
    • getFolderList

      public 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.
      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

      public static String 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

      public static String getFileExtension​(String path)
      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.