Class AthUtil


  • public class AthUtil
    extends Object
    Utility class providing commonly used helper methods for null-safe operations, conversions, and lightweight object handling.
    Since:
    1.0
    Version:
    1.0
    Author:
    Acumen Velocity
    • Constructor Detail

      • AthUtil

        public AthUtil()
    • Method Detail

      • fallback

        public static <T> T fallback​(T val,
                                     T defVal)
        Returns the given value if non-null, otherwise returns a default value.
        Type Parameters:
        T - the type of the values
        Parameters:
        val - the candidate value
        defVal - the default value if val is null
        Returns:
        val if not null, otherwise defVal
      • safeToStr

        public static String safeToStr​(Object val,
                                       String defVal)
        Converts the given object to its string representation, or returns a fallback string if the object is null.
        Parameters:
        val - the object to stringify
        Returns:
        the string representation or defVal if val is null
      • safeToStr

        public static String safeToStr​(String val,
                                       String defVal)
        Returns the provided string if non-empty, otherwise returns defVal.
        Parameters:
        val - the candidate string
        Returns:
        the string representation or defVal if val is null
      • safeToInt

        public static Integer safeToInt​(String val,
                                        int defVal)
        Attempts to parse an integer from a string. Returns the provided default value if parsing fails.
        Parameters:
        val - the string to parse
        defVal - the default value to return on failure
        Returns:
        parsed integer or defVal if parsing fails
      • safeToUuid

        public static UUID safeToUuid​(String val,
                                      UUID defVal)
      • safeToEnum

        public static <E extends Enum<E>> E safeToEnum​(String val,
                                                       Class<E> enumClass,
                                                       E defVal)
      • safeAddToList

        public static <E> boolean safeAddToList​(List<E> list,
                                                E elem)
        Adds a non-null element to a list.
        Type Parameters:
        E - the element type
        Parameters:
        list - the list to add to
        elem - the element to add (ignored if null)
        Returns:
        true if the element was added, false if elem was null
      • lastChars

        public static String lastChars​(String st,
                                       int numChars)
        Returns the last numChars characters of a string.
        Parameters:
        st - the string
        numChars - the number of trailing characters to return
        Returns:
        substring of last numChars, or empty string if st is null/empty or shorter than numChars
      • clone

        public static <T> T clone​(T obj,
                                  Class<T> cls)
        Creates a shallow copy of the given object using JSON serialization.

        This relies on JacksonUtil for serialization and deserialization. Note that it is not a deep clone; nested objects may be shared.

        Type Parameters:
        T - the object type
        Parameters:
        obj - the object to clone
        cls - the class type
        Returns:
        a shallow copy of obj
      • removeNulls

        public static <T> List<T> removeNulls​(List<T> list)
      • safeFromJsonNode

        public static <T> T safeFromJsonNode​(com.fasterxml.jackson.databind.JsonNode node,
                                             Class<T> clazz,
                                             T defVal)
        Safely converts a JsonNode to a specified class type. Returns defVal if conversion fails or node is null.
      • safeToDate

        public static Date safeToDate​(Object val,
                                      Date defVal)
        Safely converts a Date object from an object. Returns defVal if field is null or not a Date.
      • safeToLong

        public static Long safeToLong​(String val,
                                      Long defVal)
        Safely parses a Long from a string. Returns defVal if parsing fails or val is null.
      • safeToFloat

        public static Float safeToFloat​(Object val,
                                        Float defVal)
        Safely parses a Float from a string or object. Returns defVal if parsing fails or val is null.
      • safeToDouble

        public static Double safeToDouble​(Object val,
                                          Double defVal)
        Safely parses a Float from a string or object. Returns defVal if parsing fails or val is null.
      • safeToDateFromIso

        public static Date safeToDateFromIso​(String isoDateStr,
                                             Date defVal)
        Safely parses an ISO-8601 date string from JsonNode. Returns defVal if parsing fails.
      • createTempFile

        public static File createTempFile()
      • toURI

        public static URI toURI​(String uriString)
        Converts a string to URI, encoding the path component if needed Handles any URI scheme (gs://, http://, https://, file://, etc.)
      • extractLastSection

        public static String extractLastSection​(String path)
        Extracts the last section after the last slash in a path.
        Parameters:
        path - the input path string
        Returns:
        the last section after the last slash, or empty string if no slash found
        Throws:
        NullPointerException - if the input path is null
      • extractLastSectionOrDefault

        public static String extractLastSectionOrDefault​(String path,
                                                         String defaultValue)
        Extracts the last section after the last slash, with a fallback value.
        Parameters:
        path - the input path string
        defaultValue - the value to return if no section can be extracted
        Returns:
        the last section after slash, or defaultValue if not found