Class OpencsvUtils


  • public final class OpencsvUtils
    extends java.lang.Object
    This class is meant to be a collection of general purpose static methods useful in internal processing for opencsv.
    Since:
    3.9
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private OpencsvUtils()
      This class can't be instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.regex.Pattern compilePattern​(java.lang.String regex, int regexFlags, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
      Compiles a regular expression into a Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid.
      static java.util.regex.Pattern compilePatternAtLeastOneGroup​(java.lang.String regex, int regexFlags, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
      Compiles a regular expression into a Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid, or if it does not have at least one capturing group.
      static <T> MappingStrategy<T> determineMappingStrategy​(java.lang.Class<? extends T> type, java.util.Locale errorLocale, java.lang.String profile)
      Determines which mapping strategy is appropriate for this bean.
      static void handleException​(CsvException e, long lineNumber, CsvExceptionHandler exceptionHandler, java.util.concurrent.BlockingQueue<OrderedObject<CsvException>> queue)
      A function to consolidate code common to handling exceptions thrown during reading or writing of CSV files.
      static <E> void queueRefuseToAcceptDefeat​(java.util.concurrent.BlockingQueue<E> queue, E object)
      I find it annoying that when I want to queue something in a blocking queue, the thread might be interrupted and I have to try again; this method fixes that.
      static void verifyFormatString​(java.lang.String format, java.lang.Class<?> callingClass, java.util.Locale errorLocale)
      Verifies that the given format string works with one string parameter.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OpencsvUtils

        private OpencsvUtils()
        This class can't be instantiated.
    • Method Detail

      • determineMappingStrategy

        public static <T> MappingStrategy<T> determineMappingStrategy​(java.lang.Class<? extends T> type,
                                                                      java.util.Locale errorLocale,
                                                                      java.lang.String profile)
        Determines which mapping strategy is appropriate for this bean. The algorithm is:
        1. If annotations CsvBindByPosition, CsvCustomBindByPosition, CsvBindAndSplitByPosition or CsvBindAndJoinByPosition are present, ColumnPositionMappingStrategy is chosen.
        2. Otherwise, HeaderColumnNameMappingStrategy is chosen. If annotations are present, they will be used, otherwise the field names will be used as the column names.
        Type Parameters:
        T - The type of the bean for which the mapping strategy is sought
        Parameters:
        type - The class of the bean for which the mapping strategy is sought
        errorLocale - The locale to use for all error messages. If null, the default locale is used.
        profile - The profile to use when configuring bean fields
        Returns:
        A functional mapping strategy for the bean in question
      • queueRefuseToAcceptDefeat

        public static <E> void queueRefuseToAcceptDefeat​(java.util.concurrent.BlockingQueue<E> queue,
                                                         E object)
        I find it annoying that when I want to queue something in a blocking queue, the thread might be interrupted and I have to try again; this method fixes that.
        Type Parameters:
        E - The type of the object to be queued
        Parameters:
        queue - The queue the object should be added to
        object - The object to be queued
        Since:
        4.0
      • handleException

        public static void handleException​(CsvException e,
                                           long lineNumber,
                                           CsvExceptionHandler exceptionHandler,
                                           java.util.concurrent.BlockingQueue<OrderedObject<CsvException>> queue)
        A function to consolidate code common to handling exceptions thrown during reading or writing of CSV files. The proper line number is set for the exception, the exception handler is run, and the exception is queued or thrown as necessary.
        Parameters:
        e - The exception originally thrown
        lineNumber - The line or record number that caused the exception
        exceptionHandler - The exception handler
        queue - The queue for captured exceptions
        Since:
        5.2
      • compilePatternAtLeastOneGroup

        public static java.util.regex.Pattern compilePatternAtLeastOneGroup​(java.lang.String regex,
                                                                            int regexFlags,
                                                                            java.lang.Class<?> callingClass,
                                                                            java.util.Locale errorLocale)
                                                                     throws CsvBadConverterException
        Compiles a regular expression into a Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid, or if it does not have at least one capturing group.
        Parameters:
        regex - The regular expression to be compiled. May be null or an empty string, in which case null is returned. Must have at least one capturing group if not null or empty.
        regexFlags - Flags for compiling the regular expression, as in Pattern.compile(String, int).
        callingClass - The class from which this method is being called. Used for generating helpful exceptions.
        errorLocale - The locale to be used for error messages. If null, the default locale is used.
        Returns:
        A compiled pattern, or null if the input was null or empty
        Throws:
        CsvBadConverterException - If the regular expression is not empty but invalid or valid but does not have at least one capturing group
        Since:
        4.3
      • compilePattern

        public static java.util.regex.Pattern compilePattern​(java.lang.String regex,
                                                             int regexFlags,
                                                             java.lang.Class<?> callingClass,
                                                             java.util.Locale errorLocale)
                                                      throws CsvBadConverterException
        Compiles a regular expression into a Pattern, throwing an exception that is proper in the context of opencsv if the regular expression is not valid. This method may be used by custom converters if they are required to compile regular expressions that are unknown at compile time.
        Parameters:
        regex - The regular expression to be compiled. May be null or an empty string, in which case null is returned.
        regexFlags - Flags for compiling the regular expression, as in Pattern.compile(String, int).
        callingClass - The class from which this method is being called. Used for generating helpful exceptions.
        errorLocale - The locale to be used for error messages. If null, the default locale is used.
        Returns:
        A compiled pattern, or null if the input was null or empty
        Throws:
        CsvBadConverterException - If the regular expression is not empty but invalid
        Since:
        4.3
      • verifyFormatString

        public static void verifyFormatString​(java.lang.String format,
                                              java.lang.Class<?> callingClass,
                                              java.util.Locale errorLocale)
        Verifies that the given format string works with one string parameter.
        Parameters:
        format - A format string for String.format(String, Object...)
        callingClass - The class from which this method is being called. Used for generating helpful exceptions.
        errorLocale - The locale to be used for error messages. If null, the default locale is used.