Class Converter<T>

java.lang.Object
utils.Converter<T>
Type Parameters:
T - Unused generic type parameter on class declaration.

public class Converter<T> extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Separator used within a CSV field to delimit parts of a LocalDate representation.
    static final String
    Separator used within a CSV field to delimit elements of a List.
    static final String
    Separator used within a Map entry string to separate the key from the value.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Formats a LocalDate object into a specific string representation using DATE_SEPARATOR.
    static <T> String
    getField(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).
    static Class<? extends Request>
    Determines the specific subclass of Request based on a serialized string line.
    static String
    Converts a List of Strings into a single string representation using LIST_SEPARATOR.
    static <A, B> String
    mapToString(Map<A,B> mp)
    Converts a Map<A, B> into a single string representation.
    static <T> String
    objToString(T obj)
    Serializes an object into a single comma-separated string representation.
    static LocalDate
    Parses a string representation (using DATE_SEPARATOR) into a LocalDate object.
    static List<String>
    Converts a string representation (using LIST_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 (using LIST_SEPARATOR for entries and MAP_SEPARATOR within entries) back into a Map<A, B>.
    static <T> T
    stringtoObj(String line, Class<T> clazz)
    Deserializes a single line of comma-separated string data into an object of the specified class.

    Methods inherited from class java.lang.Object

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

    • LIST_SEPARATOR

      public static final String LIST_SEPARATOR
      Separator used within a CSV field to delimit elements of a List.
      See Also:
    • DATE_SEPARATOR

      public static final String DATE_SEPARATOR
      Separator used within a CSV field to delimit parts of a LocalDate representation.
      See Also:
    • MAP_SEPARATOR

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

      public static <T> T stringtoObj(String line, Class<T> clazz)
      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 - The Class object of the type T.
      Returns:
      An object of type T populated with data from the string, or null if an error occurs.
    • objToString

      public static <T> String objToString(T obj)
      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 null if an error occurs.
    • stringToList

      public static List<String> stringToList(String data)
      Converts a string representation (using LIST_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 by LIST_SEPARATOR.
      Returns:
      A List of strings. Returns an empty list if input is "null" or empty.
    • listToString

      public static String listToString(List<String> data)
      Converts a List of Strings into a single string representation using LIST_SEPARATOR. If the input list is null or empty, returns the literal string "null".
      Parameters:
      data - The List of strings to convert.
      Returns:
      A single string with elements joined by LIST_SEPARATOR, or "null" if the list is empty/null.
    • stringToDate

      public static LocalDate stringToDate(String data)
      Parses a string representation (using DATE_SEPARATOR) into a LocalDate object. 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 LocalDate object, or null if parsing fails or input is null/empty.
    • dateToString

      public static String dateToString(LocalDate date)
      Formats a LocalDate object into a specific string representation using DATE_SEPARATOR. The output format is "M::DATE::dd::DATE::yyyy".
      Parameters:
      date - The LocalDate object to format.
      Returns:
      The formatted date string, or "null" if the input date is null.
    • stringToMap

      public static <A, B> Map<A,B> stringToMap(String data, Class<A> keyType, Class<B> valueType)
      Converts a string representation (using LIST_SEPARATOR for entries and MAP_SEPARATOR within entries) back into a Map<A, B>. Uses the convert(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 - The Class object for the key type A.
      valueType - The Class object for the value type B.
      Returns:
      A Map populated from the string data. Returns an empty map if input is "null" or empty.
    • mapToString

      public static <A, B> String mapToString(Map<A,B> mp)
      Converts a Map<A, B> into a single string representation. Entries are separated by LIST_SEPARATOR. Keys and values within an entry are separated by MAP_SEPARATOR. Keys and values are converted using their toString() 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 - The Map to convert.
      Returns:
      A single string representation of the map, or "null" if the map is empty/null.
    • getField

      public static <T> String getField(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). 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 null if an error occurs.
    • getRequestClass

      public static Class<? extends Request> getRequestClass(String s)
      Determines the specific subclass of Request based on a serialized string line. It assumes the second comma-separated value in the line corresponds to a RequestType enum name.
      Parameters:
      s - The comma-separated string line representing a serialized Request object.
      Returns:
      The specific Class object extending Request (e.g., Enquiry.class), or the base Request.class if the type is NONE.
      Throws:
      IllegalArgumentException - If the request type string derived from the input is unknown or invalid.