Class HeaderUtils


  • public abstract class HeaderUtils
    extends Object
    Helper for CSV header management.
    • Constructor Detail

      • HeaderUtils

        public HeaderUtils()
    • Method Detail

      • throwIfHeaderFound

        public static void throwIfHeaderFound​(String[] headers,
                                              String[] notAllowedHeaders)
                                       throws IOException
        Check for invalid headers and throw IOException if one is found.
        Parameters:
        headers - input headers
        notAllowedHeaders - array of unacceptable headers
        Throws:
        IOException - when one unacceptable header is found
      • throwIfHeaderNotFound

        public static void throwIfHeaderNotFound​(String[] headers,
                                                 String[] requiredHeaders)
                                          throws IOException
        Check for required headers and throw IOException if one is missing.
        Parameters:
        headers - input headers
        requiredHeaders - the array of required headers
        Throws:
        IOException - when one unacceptable header is found
      • mapHeaderPositions

        public static List<Integer> mapHeaderPositions​(String[] headers)
        Map CSV headers to their positions.
        Parameters:
        headers - CSV header list
        Returns:
        a new List with header indexes
      • makeSourceMapping

        public static Map<String,​Integer> makeSourceMapping​(String[] headers,
                                                                  String[] supportedHeaders)
        We are mostly interested in only a few supported headers. Get their indexes.
        Parameters:
        headers - the headers
        supportedHeaders - Array of supported headers
        Returns:
        Map of positions of supported headers
      • toOutputLine

        public static String[] toOutputLine​(String[] nextLine,
                                            List<String> outputHeaders,
                                            List<Integer> outputMapping)
        Copy data from input CSV line to output CSV line with appropriate destination mapping.
        Parameters:
        nextLine - source line
        outputHeaders - output headers
        outputMapping - mapping of output columns
        Returns:
        the output CSV line filled with data from input CSV line