Class Util

java.lang.Object
br.net.dd.netherwingcore.common.utilities.Util

public class Util extends Object
Utility class providing various helper methods for commonly used operations. This class includes methods for string manipulation, byte conversion, time formatting, and several other utility functions relevant to most Java applications.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Produces a system beep sound.
    static String
    byteArrayToHexStr(byte[] bytes, boolean reverse)
    Converts a byte array to a hexadecimal string with optional reversal of the byte order.
    static String
    bytesToHex(byte[] bytes)
    Converts a byte array to a hexadecimal string.
    static float
    degToRad(float degrees)
    Converts an angle from degrees to radians.
    static String
    Retrieves the location of the JAR file of the current application.
    static long
    Retrieves the current process ID (PID) of the running application.
    static byte[]
    hexStrToByteArray(String str, boolean reverse)
    Converts a hexadecimal string to a byte array with optional reversal of the byte order.
    static boolean
    isIPAddress(String ipAddress)
    Checks if a given string is a valid IP address.
    static int
    roundingFloatValue(float val)
    Rounds a float value based on a threshold of 0.44444445.
    static String
    secsToTimeString(long timeInSecs, boolean shortText, boolean hoursOnly)
    Converts seconds into a human-readable time string.
    static boolean
    Compares two strings for equality, ignoring case considerations.
    static boolean
    Converts a string to a boolean value, interpreting common representations of "true".
    static void
    Removes invisible characters (e.g., spaces, tabs, etc.) from a StringBuilder, leaving one space as a separator.
    static String
    Converts a UNIX timestamp to a formatted timestamp string.
    static List<String>
    tokenize(String str, char sep, boolean keepEmpty)
    Tokenizes a string based on a given separator and keeps empty tokens if specified.
    static int
    utf8Length(String utf8str)
    Calculates the number of UTF-8 code points in a given string.
    static void
    utf8Truncate(StringBuilder str, int len)
    Truncates a StringBuilder to a specified number of UTF-8 code points.

    Methods inherited from class Object

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

    • Util

      public Util()
  • Method Details

    • getJarLocation

      public static String getJarLocation()
      Retrieves the location of the JAR file of the current application.
      Returns:
      The directory where the JAR file resides.
      Throws:
      RuntimeException - If the URI syntax is invalid.
    • beep

      public static void beep()
      Produces a system beep sound.
    • bytesToHex

      public static String bytesToHex(byte[] bytes)
      Converts a byte array to a hexadecimal string.
      Parameters:
      bytes - The byte array to convert.
      Returns:
      The hexadecimal representation of the byte array.
    • tokenize

      public static List<String> tokenize(String str, char sep, boolean keepEmpty)
      Tokenizes a string based on a given separator and keeps empty tokens if specified.
      Parameters:
      str - The string to tokenize.
      sep - The separator character.
      keepEmpty - Whether to include empty tokens in the result.
      Returns:
      A list of tokens extracted from the input string.
    • stringEqualI

      public static boolean stringEqualI(String a, String b)
      Compares two strings for equality, ignoring case considerations.
      Parameters:
      a - The first string to compare.
      b - The second string to compare.
      Returns:
      true if the strings are equal ignoring case, false otherwise.
    • stripLineInvisibleChars

      public static void stripLineInvisibleChars(StringBuilder str)
      Removes invisible characters (e.g., spaces, tabs, etc.) from a StringBuilder, leaving one space as a separator.
      Parameters:
      str - The StringBuilder to process.
    • roundingFloatValue

      public static int roundingFloatValue(float val)
      Rounds a float value based on a threshold of 0.44444445.
      Parameters:
      val - The float value to round.
      Returns:
      The rounded integer value.
    • secsToTimeString

      public static String secsToTimeString(long timeInSecs, boolean shortText, boolean hoursOnly)
      Converts seconds into a human-readable time string.
      Parameters:
      timeInSecs - The time in seconds.
      shortText - Whether to use short text format (e.g., "d", "h").
      hoursOnly - Whether to display hours only.
      Returns:
      A formatted time string.
    • timeToTimestampStr

      public static String timeToTimestampStr(long t)
      Converts a UNIX timestamp to a formatted timestamp string.
      Parameters:
      t - The UNIX timestamp (in seconds).
      Returns:
      The formatted timestamp string.
    • isIPAddress

      public static boolean isIPAddress(String ipAddress)
      Checks if a given string is a valid IP address.
      Parameters:
      ipAddress - The string to validate.
      Returns:
      true if the string is a valid IP address, false otherwise.
    • getPID

      public static long getPID()
      Retrieves the current process ID (PID) of the running application.
      Returns:
      The process ID.
    • utf8Length

      public static int utf8Length(String utf8str)
      Calculates the number of UTF-8 code points in a given string.
      Parameters:
      utf8str - The input string.
      Returns:
      The number of code points in the string.
    • utf8Truncate

      public static void utf8Truncate(StringBuilder str, int len)
      Truncates a StringBuilder to a specified number of UTF-8 code points.
      Parameters:
      str - The StringBuilder to truncate.
      len - The maximum number of UTF-8 code points to retain.
    • degToRad

      public static float degToRad(float degrees)
      Converts an angle from degrees to radians.
      Parameters:
      degrees - The angle in degrees.
      Returns:
      The angle in radians.
    • byteArrayToHexStr

      public static String byteArrayToHexStr(byte[] bytes, boolean reverse)
      Converts a byte array to a hexadecimal string with optional reversal of the byte order.
      Parameters:
      bytes - The byte array to convert.
      reverse - Whether to reverse the byte order.
      Returns:
      The hexadecimal string.
    • hexStrToByteArray

      public static byte[] hexStrToByteArray(String str, boolean reverse)
      Converts a hexadecimal string to a byte array with optional reversal of the byte order.
      Parameters:
      str - The hexadecimal string to convert.
      reverse - Whether to reverse the byte order.
      Returns:
      The resulting byte array.
      Throws:
      IllegalArgumentException - If the string has an odd number of characters.
    • stringToBool

      public static boolean stringToBool(String str)
      Converts a string to a boolean value, interpreting common representations of "true".
      Parameters:
      str - The string to convert (e.g., "1", "true", "yes").
      Returns:
      true if the string represents a true value, false otherwise.