Package com.acumenvelocity.ath.common
Class AthUtil
- java.lang.Object
-
- com.acumenvelocity.ath.common.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 Summary
Constructors Constructor Description AthUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tclone(T obj, Class<T> cls)Creates a shallow copy of the given object using JSON serialization.static FilecreateTempFile()static StringdateToString(DateFormat dateFormat, Date date)static StringextractLastSection(String path)Extracts the last section after the last slash in a path.static StringextractLastSectionOrDefault(String path, String defaultValue)Extracts the last section after the last slash, with a fallback value.static <T> Tfallback(T val, T defVal)Returns the given value if non-null, otherwise returns a default value.static StringfileAsString(File file)static StringlastChars(String st, int numChars)Returns the lastnumCharscharacters of a string.static <T> List<T>removeNulls(List<T> list)static <E> booleansafeAddToList(List<E> list, E elem)Adds a non-null element to a list.static <T> TsafeFromJsonNode(com.fasterxml.jackson.databind.JsonNode node, Class<T> clazz, T defVal)Safely converts a JsonNode to a specified class type.static DatesafeToDate(Object val, Date defVal)Safely converts a Date object from an object.static DatesafeToDateFromIso(String isoDateStr, Date defVal)Safely parses an ISO-8601 date string from JsonNode.static DoublesafeToDouble(Object val, Double defVal)Safely parses a Float from a string or object.static <E extends Enum<E>>
EsafeToEnum(String val, Class<E> enumClass, E defVal)static FloatsafeToFloat(Object val, Float defVal)Safely parses a Float from a string or object.static IntegersafeToInt(String val, int defVal)Attempts to parse an integer from a string.static LongsafeToLong(String val, Long defVal)Safely parses a Long from a string.static StringsafeToStr(Object val, String defVal)Converts the given object to its string representation, or returns a fallback string if the object is null.static StringsafeToStr(String val, String defVal)Returns the provided string if non-empty, otherwise returns defVal.static UUIDsafeToUuid(String val, UUID defVal)static List<UUID>safeToUuidList(Object val, List<UUID> defVal)static URItoURI(String uriString)Converts a string to URI, encoding the path component if needed Handles any URI scheme (gs://, http://, https://, file://, etc.)
-
-
-
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 valuedefVal- the default value ifvalisnull- Returns:
valif notnull, otherwisedefVal
-
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
defValifvalis 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
defValifvalis 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 parsedefVal- the default value to return on failure- Returns:
- parsed integer or
defValif parsing fails
-
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 toelem- the element to add (ignored if null)- Returns:
- true if the element was added, false if
elemwas null
-
lastChars
public static String lastChars(String st, int numChars)
Returns the lastnumCharscharacters of a string.- Parameters:
st- the stringnumChars- the number of trailing characters to return- Returns:
- substring of last
numChars, or empty string ifstis null/empty or shorter thannumChars
-
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
JacksonUtilfor 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 clonecls- the class type- Returns:
- a shallow copy of
obj
-
fileAsString
public static String fileAsString(File file) throws IOException
- Throws:
IOException
-
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.
-
dateToString
public static String dateToString(DateFormat dateFormat, Date date)
-
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 stringdefaultValue- the value to return if no section can be extracted- Returns:
- the last section after slash, or defaultValue if not found
-
-