Class Converter<T>
java.lang.Object
utils.Converter<T>
- Type Parameters:
T- Unused generic type parameter on class declaration.
Utility class providing static methods for converting objects to and from string representations,
primarily designed for CSV serialization and deserialization.
Uses reflection to handle object fields, including common types like String, Integer, Boolean,
Enums, and complex types like
List<String>, Map<K, V>, and LocalDate.
Defines specific separators for serializing collection types. Includes special handling
for determining subclasses of Request.
Note: The generic type <T> on the class declaration itself appears unused as all methods are static.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSeparator used within a CSV field to delimit parts of a LocalDate representation.static final StringSeparator used within a CSV field to delimit elements of a List.static final StringSeparator used within a Map entry string to separate the key from the value. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdateToString(LocalDate date) Formats aLocalDateobject into a specific string representation usingDATE_SEPARATOR.static <T> StringgetField(T obj) Generates a comma-separated string of field names for a given object's class, including fields inherited from superclasses (up to two levels).Determines the specific subclass ofRequestbased on a serialized string line.static StringlistToString(List<String> data) Converts a List of Strings into a single string representation usingLIST_SEPARATOR.static <A,B> String mapToString(Map<A, B> mp) Converts aMap<A, B>into a single string representation.static <T> StringobjToString(T obj) Serializes an object into a single comma-separated string representation.static LocalDatestringToDate(String data) Parses a string representation (usingDATE_SEPARATOR) into aLocalDateobject.stringToList(String data) Converts a string representation (usingLIST_SEPARATOR) back into a List of Strings.static <A,B> Map <A, B> stringToMap(String data, Class<A> keyType, Class<B> valueType) Converts a string representation (usingLIST_SEPARATORfor entries andMAP_SEPARATORwithin entries) back into aMap<A, B>.static <T> TstringtoObj(String line, Class<T> clazz) Deserializes a single line of comma-separated string data into an object of the specified class.
-
Field Details
-
LIST_SEPARATOR
Separator used within a CSV field to delimit elements of a List.- See Also:
-
DATE_SEPARATOR
Separator used within a CSV field to delimit parts of a LocalDate representation.- See Also:
-
MAP_SEPARATOR
Separator used within a Map entry string to separate the key from the value.- See Also:
-
-
Constructor Details
-
Converter
public Converter()
-
-
Method Details
-
stringtoObj
Deserializes a single line of comma-separated string data into an object of the specified class. Uses reflection to instantiate the object and set its fields based on the string values. Handles fields inherited from up to two levels of superclasses. Supports conversion for String, Integer, Double, Boolean, Enum, LocalDate, List<String>, and Map<?,?> types. Assumes custom formats for List, Map, and LocalDate using defined separators. Treats the literal string "null" as a null value for fields.- Type Parameters:
T- The type of the object to create.- Parameters:
line- The comma-separated string representing the object's data.clazz- TheClassobject of the typeT.- Returns:
- An object of type
Tpopulated with data from the string, ornullif an error occurs.
-
objToString
Serializes an object into a single comma-separated string representation. Uses reflection to access object fields (including inherited ones up to two levels). Handles List<String>, Map<?,?>, LocalDate, primitives, enums, and Strings using helper methods and defined separators. Null field values are represented by the literal string "null".- Type Parameters:
T- The type of the object to serialize.- Parameters:
obj- The object to convert to a string.- Returns:
- A comma-separated string representing the object's data, or
nullif an error occurs.
-
stringToList
Converts a string representation (usingLIST_SEPARATOR) back into a List of Strings. If the input data is the literal string "null" or empty, returns an empty list.- Parameters:
data- The string data, potentially containing elements separated byLIST_SEPARATOR.- Returns:
- A
Listof strings. Returns an empty list if input is "null" or empty.
-
listToString
Converts a List of Strings into a single string representation usingLIST_SEPARATOR. If the input list is null or empty, returns the literal string "null".- Parameters:
data- TheListof strings to convert.- Returns:
- A single string with elements joined by
LIST_SEPARATOR, or "null" if the list is empty/null.
-
stringToDate
Parses a string representation (usingDATE_SEPARATOR) into aLocalDateobject. Expects the format "M::DATE::dd::DATE::yyyy".- Parameters:
data- The string representation of the date (e.g., "4::DATE::22::DATE::2025").- Returns:
- The parsed
LocalDateobject, or null if parsing fails or input is null/empty.
-
dateToString
-
stringToMap
Converts a string representation (usingLIST_SEPARATORfor entries andMAP_SEPARATORwithin entries) back into aMap<A, B>. Uses theconvert(String, Class)helper method to convert keys and values to their target types. If the input data is the literal string "null" or empty, returns an empty map.- Type Parameters:
A- The type of the keys in the map.B- The type of the values in the map.- Parameters:
data- The serialized string representation of the map.keyType- TheClassobject for the key typeA.valueType- TheClassobject for the value typeB.- Returns:
- A
Mappopulated from the string data. Returns an empty map if input is "null" or empty.
-
mapToString
Converts aMap<A, B>into a single string representation. Entries are separated byLIST_SEPARATOR. Keys and values within an entry are separated byMAP_SEPARATOR. Keys and values are converted using theirtoString()method. If the input map is null or empty, returns the literal string "null".- Type Parameters:
A- The type of the keys in the map.B- The type of the values in the map.- Parameters:
mp- TheMapto convert.- Returns:
- A single string representation of the map, or "null" if the map is empty/null.
-
getField
Generates a comma-separated string of field names for a given object's class, including fields inherited from superclasses (up to two levels). This is typically used to create a header row for a CSV file.- Type Parameters:
T- The type of the object.- Parameters:
obj- An instance of the object whose class's fields are to be retrieved. (Can be null if only class structure is needed, but instance is safer for non-static fields).- Returns:
- A comma-separated string of field names, or
nullif an error occurs.
-
getRequestClass
Determines the specific subclass ofRequestbased on a serialized string line. It assumes the second comma-separated value in the line corresponds to aRequestTypeenum name.- Parameters:
s- The comma-separated string line representing a serialized Request object.- Returns:
- The specific
Classobject extendingRequest(e.g.,Enquiry.class), or the baseRequest.classif the type is NONE. - Throws:
IllegalArgumentException- If the request type string derived from the input is unknown or invalid.
-